Codeforces 219D. Choosing Capital for Treeland (tree DP)

Source: Internet
Author: User

Topic Links:

Http://codeforces.com/contest/219/problem/D

Test instructions

to an N-node directed non-circular graph, to find a point: the point to the other n-1 to reverse the least road, (edge <u,v> if v to go to u, then to reverse the side direction) if there are more than one such point, then the output of all

Ideas:

Read three blog, very good

http://blog.csdn.net/chl_3205/article/details/9284747

http://m.blog.csdn.net/qq_32570675/article/details/53691814 once again Dfs

http://blog.csdn.net/angon823/article/details/52316220

The forward Benquan value is 0, and the reverse is 1.

The first time DFS records the number of edges that need to be changed for each point to all subtrees. (bottom-up push) (optimize the number of edges that need to change only the root node to all points)

The second time DFS consists of the parent node, the number of edges that need to be changed by the child node to all points. (from top to bottom)

Turn the direction of the edge into weights, positive to 1, reverse to 0.

The problem is converted to what points to find the total weight value after traversing the full map.

This is the tree DP, consider each node, it can harvest value from the subtree, can also be harvested from the father. So Dfs two times, while the value of the tree to the dps[i], and then the value of the father to save the dpf[i]. Ans[i] = Dps[i] + dpf[i]. This is all the old routine!

For the shadow that point, the first time DFS has found all of the following sub-nodes (subtree) contributed to him, then only the red line is not counted, and the second time Dfs is calculating the father's contribution to him

Code:

Code One:

#include <bits/stdc++.h>using namespaceStd;typedefLong Longll;#defineMS (a) memset (A,0,sizeof (a))#defineMP Make_pair#definePB push_backConst intINF =0x3f3f3f3f;Constll infll =0x3f3f3f3f3f3f3f3fll;inline ll Read () {ll x=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}//////////////////////////////////////////////////////////////////////////Const intMAXN = 2e5+Ten;intN,RES,DP[MAXN];BOOLVis[maxn];vector<pair<int,int> >G[MAXN];voidDFS1 (intu) {Vis[u]=1;  for(intI=0; i< (int) G[u].size (); i++){        intv =G[u][i].first; if(Vis[v])Continue;        DFS1 (v); Res+=G[u][i].second; }}voidDFS2 (intu) {Vis[u]=1;  for(intI=0; i< (int) G[u].size (); i++){        intv = g[u][i].first, w =G[u][i].second; if(Vis[v])Continue; if(w) dp[v] = dp[u]-1; ElseDP[V] = dp[u]+1;    DFS2 (v); }}intMain () {CIN>>N;  for(intI=1; i<n; i++){        intU,v; scanf"%d%d",&u,&v); G[u].push_back (MP (V,0)); G[v].push_back (MP (u),1)); } DFS1 (1); dp[1] =Res;    MS (VIS); DFS2 (1); intMi =INF, last;  for(intI=1; i<=n; i++)        if(Dp[i] <=mi) Mi=Dp[i]; cout<< mi <<Endl;  for(intI=1; i<=n; i++){        if(Dp[i] = =mi) {cout<< I <<" "; }} puts (""); return 0;}

Code two:

#include <bits/stdc++.h>using namespaceStd;typedefLong Longll;#defineMS (a) memset (A,0,sizeof (a))#defineMP Make_pair#definePB push_backConst intINF =0x3f3f3f3f;Constll infll =0x3f3f3f3f3f3f3f3fll;inline ll Read () {ll x=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}//////////////////////////////////////////////////////////////////////////Const intMAXN = 1e6+Ten;intN,dps[maxn],dpf[maxn];vector<pair<int,int> >G[MAXN];voidDFS1 (intUintFA) {     for(intI=0; i< (int) G[u].size (); i++){        intv = g[u][i].first, w =G[u][i].second; if(v = = FA)Continue;        DFS1 (V,u); Dps[u]+=W; Dps[u]+=Dps[v]; }}voidDFS2 (intUintFA) {     for(intI=0; i< (int) G[u].size (); i++){        intv = g[u][i].first, w =G[u][i].second; if(v = = FA)Continue; DPF[V]= Dpf[u]+dps[u]-dps[v]-w + (w?)0:1);    DFS2 (V,u); }}intMain () {CIN>>N;  for(intI=1; i<n; i++){        intU,v; scanf"%d%d",&u,&v); G[u].push_back (MP (V,0)); G[v].push_back (MP (u),1)); } DFS1 (1,-1); DFS2 (1,-1); intMi =INF;  for(intI=1; i<=n; i++) Mi= Min (mi,dps[i]+Dpf[i]); cout<< mi <<Endl;  for(intI=1; i<=n; i++)        if(Dps[i]+dpf[i] = =mi) cout<< I <<" "; Puts (""); return 0;}

Codeforces 219D. Choosing Capital for Treeland (tree DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.