Choosing Capital for Treeland (树形dp)
2017-11-19 23:52
567 查看
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.
Example
Input
Output
Input
Output
题目大概:
在一颗树中,边都是单项的,找到一个节点,使得它如果能到达任何一个结点的话,反转的边最少。
思路:
可以先把结点能去的结点dfs统计一下。
然后再dfs一遍,能去的地方把点减去,不能去的地方再加上1,最后的答案就是需要反转的数量。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int f=1000005;
int n,m;
int dp[200005][2];
int head[200005];
int minshu;
int ans;
struct shu
{
int v;
int next;
int va;
}tr[400010];
void add(int q,int w,int v)
{
tr[ans].v=w;
tr[ans].va=v;
tr[ans].next=head[q];
head[q]=ans++;
}
void down(int x,int pa)
{
dp[x][0]=0;
for(int i=head[x];i!=-1;i=tr[i].next)
{
int son=tr[i].v;
if(son!=pa)
{
int w=tr[i].va;
down(son,x);
dp[x][0]+=dp[son][0]+w;
}
}
}
void dfs(int x,int pa,int v)
{
if(x==pa)
{
dp[x][1]=dp[x][0];
}
else
{
if(v==0)dp[x][1]=dp[pa][1]+1;
else dp[x][1]=dp[pa][1]-1;
}
for(int i=head[x];i!=-1;i=tr[i].next)
{
int son=tr[i].v;
if(son!=pa)
{
int w=tr[i].va;
dfs(son,x,w);
}
}
}
int main()
{
while(~scanf("%d",&n))
{
memset(head,-1,sizeof(head));
ans=0;
for(int i=1;i<n;i++)
{
int q,w;
scanf("%d%d",&q,&w);
add(q,w,0);
add(w,q,1);
}
down(1,-1);
dfs(1,1,0);
int ans=f;
for(int i=1;i<=n;i++)ans=min(dp[i][1],ans);
printf("%d\n",ans);
for(int i=1;i<=n;i++)
{
if(dp[i][1]==ans)
{
printf("%d ",i);
}
}
}
return 0;
}
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.
Example
Input
3 2 1 2 3
Output
0 2
Input
4 1 4 2 4 3 4
Output
2 1 2 3
题目大概:
在一颗树中,边都是单项的,找到一个节点,使得它如果能到达任何一个结点的话,反转的边最少。
思路:
可以先把结点能去的结点dfs统计一下。
然后再dfs一遍,能去的地方把点减去,不能去的地方再加上1,最后的答案就是需要反转的数量。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int f=1000005;
int n,m;
int dp[200005][2];
int head[200005];
int minshu;
int ans;
struct shu
{
int v;
int next;
int va;
}tr[400010];
void add(int q,int w,int v)
{
tr[ans].v=w;
tr[ans].va=v;
tr[ans].next=head[q];
head[q]=ans++;
}
void down(int x,int pa)
{
dp[x][0]=0;
for(int i=head[x];i!=-1;i=tr[i].next)
{
int son=tr[i].v;
if(son!=pa)
{
int w=tr[i].va;
down(son,x);
dp[x][0]+=dp[son][0]+w;
}
}
}
void dfs(int x,int pa,int v)
{
if(x==pa)
{
dp[x][1]=dp[x][0];
}
else
{
if(v==0)dp[x][1]=dp[pa][1]+1;
else dp[x][1]=dp[pa][1]-1;
}
for(int i=head[x];i!=-1;i=tr[i].next)
{
int son=tr[i].v;
if(son!=pa)
{
int w=tr[i].va;
dfs(son,x,w);
}
}
}
int main()
{
while(~scanf("%d",&n))
{
memset(head,-1,sizeof(head));
ans=0;
for(int i=1;i<n;i++)
{
int q,w;
scanf("%d%d",&q,&w);
add(q,w,0);
add(w,q,1);
}
down(1,-1);
dfs(1,1,0);
int ans=f;
for(int i=1;i<=n;i++)ans=min(dp[i][1],ans);
printf("%d\n",ans);
for(int i=1;i<=n;i++)
{
if(dp[i][1]==ans)
{
printf("%d ",i);
}
}
}
return 0;
}
相关文章推荐
- CodeForces D. Choosing Capital for Treeland (转化为树形DP)
- CF#219 D. Choosing Capital for Treeland(树形DP)
- [codeforces 219D] Choosing Capital for Treeland (树形dp)
- 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
- Codeforces 219D - Choosing Capital for Treeland(树形dp)
- Choosing Capital for Treeland (树形dp+双向搜索)
- codeforces 219D D. Choosing Capital for Treeland(树形dp)
- CodeForces 219D-Choosing Capital for Treeland(树形dp)
- [CF 219D]Choosing Capital for Treeland[树形DP]
- CF 219 D Choosing Capital for Treeland(树形dp)
- E - Choosing Capital for Treeland (树形dp)
- CF_D. Choosing Capital for Treeland_树形DP
- 【codeforce 219D】 Choosing Capital for Treeland (树形DP)
- Codeforces Round #135 (Div. 2) - D. Choosing Capital for Treeland(dfs / 树形DP)
- CF 219 D:Choosing Capital for Treeland(树形dp)
- cf 135div2D Choosing Capital for Treeland 【树形dp】
- [CF219D]Choosing Capital for Treeland(树dp)
- CodeForces 219D-H - Choosing Capital for Treeland-树DP
- Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland(从每个点出发最小需要修改几条边的方向可以到达其它所有点,输出最小改变几)
- Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland dfs