【树形DP】codeforces 219D Choosing Capital for Treeland
来源:互联网 发布:淘宝大熊哥佛牌怎么样 编辑:程序博客网 时间:2024/05/20 19:28
Choosing Capital for Treeland
time limit per test:3 seconds
memory limit per test: 256 megabytes
Description
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.
I think
树形DP
题目大意:有向图,选择一个节点遍历需要翻转的边数最小。
用f[i]表示顺着遍历方向i的子节点连边中需要翻转的边数,g[i]表示顺着父节点方向那颗树中需要翻转的边数,第一遍dfs,先向下搜再回溯计算
第二遍dfs
在存边的时候,仍然存双向边,但用标记区分一下原有边和翻转边就好了。
论树形dp的水题套路……
Code
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int sm = 2e5+20;int n,s,minn=sm,f[sm],g[sm],hd[sm];struct edge{ int to,nxt,p;}e[sm*2];int Min(int x,int y) {return x<y?x:y;}void add(int f,int t,int a) { e[++s].to=t; e[s].p=a; e[s].nxt=hd[f]; hd[f]=s;}void dfsa(int x,int fa) { for(int i=hd[x];i;i=e[i].nxt) { if(e[i].to==fa)continue; dfsa(e[i].to,x); f[x]+=f[e[i].to]; if(!e[i].p)++f[x]; }}char ch;void read(int &x) { x=0;ch=getchar(); while(ch>'9'||ch<'0')ch=getchar(); while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();}void dfsb(int x,int fa,int p) { if(fa<0)g[x]=0; else if(p)g[x]=f[fa]-f[x]+1+g[fa]; else if(!p)g[x]=f[fa]-1-f[x]+g[fa]; for(int i=hd[x];i;i=e[i].nxt) { if(e[i].to==fa)continue; dfsb(e[i].to,x,e[i].p); }}int main() { read(n); for(int i=1,x,y;i<n;++i) { read(x);read(y); add(x,y,1); add(y,x,0); } dfsa(1,-1); dfsb(1,-1,0); for(int i=1;i<=n;++i) minn=Min(minn,f[i]+g[i]); printf("%d\n",minn); for(int i=1;i<=n;++i) if(f[i]+g[i]==minn)printf("%d ",i); printf("\n"); return 0;}
- codeforces 219D D. Choosing Capital for Treeland(树形dp)
- CodeForces 219D Choosing Capital for Treeland (树形dp)
- CodeForces 219D Choosing Capital for Treeland | 树形dp
- Codeforces 219D - Choosing Capital for Treeland(树形dp)
- codeforces 219D Choosing Capital for Treeland(树形DP)
- CodeForces 219D Choosing Capital for Treeland (树形DP)
- CodeForces 219D Choosing Capital for Treeland(树形DP)
- codeforces 219D Choosing Capital for Treeland (树形dp)
- 【树形DP】codeforces 219D Choosing Capital for Treeland
- codeforces 218D Choosing Capital for Treeland 树形DP
- 树形dp 219D Choosing Capital for Treeland
- [CF 219D]Choosing Capital for Treeland[树形DP]
- cf 219D Choosing Capital for Treeland 树形dp
- CF 219 D Choosing Capital for Treeland(树形dp)
- CF 219D. Choosing Capital for Treeland 树形DP
- CF 219D Choosing Capital for Treeland (树形dp)
- CF 219D Choosing Capital for Treeland (树形dp)
- CF#219 D. Choosing Capital for Treeland(树形DP)
- Tizen
- CodeM资格赛第一题
- centos 6.5 安装shadowsock
- ARP协议的工作过程和局域网使用
- Java世界产品的Maven包命名展示
- 【树形DP】codeforces 219D Choosing Capital for Treeland
- SharedPreferences初识
- 算法导论 思考题 16-1 d小题 找零问题
- VMware: ubuntu 与Windows设置共享文件夹
- 视音频数据处理入门:UDP-RTP协议解析
- ARKit从入门到精通(3)-ARKit自定义实现
- propTypes的使用
- Properties的工具类,用于读取外部文件Properties
- 【实战】移植QT5.6.0到嵌入式linuxS3C2440单板