本文主要是介绍CF-219D Choosing Capital for Treeland,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
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个城市有n-1条单向道路,国家委员会决定在这n个城市中选择一个城市作为首都,作为首都的城市有一个要求,就是能通过道路到达每一个城市。所以当选择一个城市作为首都之后,就有一部分单向道路要被反转,问你选择哪些城市作为首都,需要反转的道路最少。
解题思路:
还是两边dfs。
第一个dfs求出将该结点作为首都,它的子树有多少条道路需要反转。
第二版dfs求出将该结点作为首都,它往上面走有多少条道路需要反转。
两个加起来就可以了。
通过这道题,我突然发现之前的树形DP写的有问题,如果是菊花图就会TLE,第二次的dfs没必要再去遍历兄弟节点,只需要做一个容斥就好了
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<map>
#include<queue>using namespace std;
const long long inf = 0x3f3f3f3f;
const int maxn = 200005;int n, m, k;struct Edge
{int to,nxt,tag;
} edges[maxn<<2];
int head[maxn<<2];
int tot;void addEdge(int u, int v, int tag)
{edges[tot] = Edge{v,head[u],tag};head[u] = tot++;
}int dp1[maxn]; //从该结点往下走有多少条道路需要反转
int dp2[maxn]; //从该节点往上走有多少条道路需要反转void dfs1(int u, int fa)
{dp1[u] = 0;for(int i = head[u]; ~i; i = edges[i].nxt){int v = edges[i].to;int tag = edges[i].tag;if(v==fa)continue;dfs1(v,u);dp1[u]+=tag+dp1[v];}
}
void dfs2(int u, int fa)
{for(int i = head[u]; ~i; i = edges[i].nxt){int v = edges[i].to;int tag = edges[i].tag;if(v == fa)continue;dp2[v] = dp1[u]-dp1[v]-tag+dp2[u]+(tag^1);dfs2(v,u);}
}vector<int>v1;int main()
{int n;scanf("%d", &n);int u, v;tot = 1;memset(head,-1,sizeof(head));for(int i = 1; i < n; i++){scanf("%d%d", &u, &v);addEdge(u,v,0);addEdge(v,u,1);}dfs1(1,-1);dfs2(1,0);int minn = inf;for(int i = 1; i <= n; i++){dp1[i]+=dp2[i];minn = min(minn,dp1[i]);}for(int i = 1; i <= n; i++){if(minn == dp1[i])v1.push_back(i);}printf("%d\n", minn);int len = v1.size();for(int i = 0; i < len; i++){printf("%d ", v1[i]);}printf("\n");return 0;
}
这篇关于CF-219D Choosing Capital for Treeland的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!