日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【CodeForces - 219D 】Choosing Capital for Treeland (树形dp)

發布時間:2023/12/10 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CodeForces - 219D 】Choosing Capital for Treeland (树形dp) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

The country Treeland consists of?n?cities, some pairs of them are connected with?unidirectional?roads. Overall there are?n?-?1?roads in the country. We know that if we don't take the direction of the roads into consideration, we can get from any city to any other one.

The council of the elders has recently decided to choose the capital of Treeland. Of course it should be a city of this country. The council is supposed to meet in the capital and regularly move from the capital to other cities (at this stage nobody is thinking about getting back to the capital from these cities). For that reason if city?a?is chosen a capital, then all roads must be oriented so that if we move along them, we can get from city?a?to any other city. For that some roads may have to be inversed.

Help the elders to choose the capital so that they have to inverse the minimum number of roads in the country.

Input

The first input line contains integer?n?(2?≤?n?≤?2·105) — the number of cities in Treeland. Next?n?-?1?lines contain the descriptions of the roads, one road per line. A road is described by a pair of integers?si,?ti?(1?≤?si,?ti?≤?n;?si?≠?ti) — the numbers of cities, connected by that road. The?i-th road is oriented from city?si?to city?ti. You can consider cities in Treeland indexed from 1 to?n.

Output

In the first line print the minimum number of roads to be inversed if the capital is chosen optimally. In the second line print all possible ways to choose the capital — a sequence of indexes of cities in the increasing order.

Examples

Input

3 2 1 2 3

Output

0 2

Input

4 1 4 2 4 3 4

Output

2 1 2 3

題目大意:

?給出N個點,其中有N-1條有向邊,邊的方向可以改變,問最少改變多少條邊可以從某一個點到達任意一個點,同時求出這些點。

解題報告:

給一段一句話題解:

要改變的邊的權值為1,不需要改變的邊的權值為0,兩次dfs,第一次算出以1點為根節點到所有點要改變的邊數,第二次以1為根節點向下遍歷節點3 算出每一個點到達所有點要改變的邊數,dp[son]+=(dp[root]-dp[son])+((tree[i].val)?-1:1),某一點的值是他父節點4 的值減去他以前的值再考慮他與父節點之間的邊的方向。

? 其實就是兩邊dfs就好了,第一遍是正常的遞歸以cur為根的狀態值,第二遍求出轉移到cur的那一半的狀態值。也就是第一次dfs是用兒子更新父親,第二次dfs是用父親處理一下之后來更新兒子。好題啊精妙的!!

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 2e5 + 5; int n; int a,b; ll dp[MAX];//指向我的有多少條邊 ll ans[MAX]; struct Node {int to;bool f;//1==實邊,0==虛邊 Node(){}Node(int to,int f):to(to),f(f){} }; vector<Node> vv[MAX]; void dfs(int cur,int root) {int up = vv[cur].size();for(int i = 0; i<up; i++) {Node v = vv[cur][i];if(v.to == root) continue;dfs(v.to,cur);dp[cur] += dp[v.to];if(v.f == 0) dp[cur]++; } } void DFS(int cur,int root) {int up = vv[cur].size();for(int i = 0; i<up; i++) {Node v = vv[cur][i];if(v.to == root) continue;dp[v.to] += (dp[cur]-dp[v.to]);if(v.f) dp[v.to]++;else dp[v.to]--;DFS(v.to,cur);} } int main() {cin>>n;for(int i = 1; i<=n-1; i++) {scanf("%d%d",&a,&b);vv[a].pb(Node(b,1));vv[b].pb(Node(a,0));}dfs(1,-1);DFS(1,-1);ll minn = 0x3f3f3f3f3f;for(int i = 1; i<=n; i++) minn = min(minn,dp[i]);printf("%lld\n",minn);int ans = 0;for(int i = 1; i<=n; i++) {if(minn == dp[i]) printf("%d ",i);}return 0 ;}

總結: 代碼中DFS中無意間調用了dfs,TLE8了,,但是沒有wa,,仔細想一下確實是不會wa的,,這里的dp不會再重新算一遍,因為葉子結點的dp永遠沒有改變過,所以不會出現多調用一次dfs就會使每個節點dp的值成倍的增加的現象。所以其實在這里多調用一次dfs頂多算是重新求了一次dp的值,并不會使dp改變成原來的多少倍那樣、、

注意DFS的更新順序,,不然就會出錯!!總之牢牢把握住,更新狀態的前提是所用的值一定是所需的完成值就好了。

總結

以上是生活随笔為你收集整理的【CodeForces - 219D 】Choosing Capital for Treeland (树形dp)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。