CF-219-D-Choosing Capital for Treeland
2017-11-25 21:22
405 查看
D. Choosing Capital for Treeland
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
The country Treeland consists of n cities, some pairs of them are connected withunidirectional roads. Overall there aren - 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 citya 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. Nextn - 1
lines contain the descriptions of the roads, one road per line. A road is described by a pair of integerssi, ti
(1 ≤ si, ti ≤ n; si ≠ ti)
— the numbers of cities, connected by that road. The i-th road is oriented from citysi to cityti.
You can consider cities in Treeland indexed from 1 ton.
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
Output
Input
Output
题意:有n个点,n-1条有向边,然后求如果边能够反转,反转的代价为1的话,输出从这一点到其他n-1个点需要花费的代价最小的点的编号,如果有多个点,从小到大输出。
两遍dfs,第一遍求出从点1到其他点的最小代价,然后第二遍更新第一遍dfs所的到的每个点到其他点的代价,因为第一遍如果是求的a->b的代价,当b->a的时候,代价sum[]就需要根据边是否需要反转来改变值。
代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<math.h>
using namespace std;
int p,head[200005],sum[200005];
struct f
{
int son;
int v;
int next;
}tree[500005];
void csh()
{
memset(head,-1,sizeof(head));
memset(sum,0,sizeof(sum));
p=0;
}
void build(int fa,int son,int val)
{
tree[p].son=son;
tree[p].v=val;
tree[p].next=head[fa];
head[fa]=p++;
}
void dfs1(int now,int before)
{
for(int i=head[now];i!=-1;i=tree[i].next)
{
int to=tree[i].son;
if(to==before)
continue;
if(tree[i].v==1)
sum[now]++;
dfs1(to,now);
sum[now]+=sum[to];
}
}
void dfs2(int now,int before)
{
for(int i=head[now];i!=-1;i=tree[i].next)
{
int to=tree[i].son;
if(to==before)
continue;
if(tree[i].v==1)
sum[to]=sum[now]-1;
else
sum[to]=sum[now]+1;
dfs2(to,now);
}
}
int main()
{
int n,a,b,minn;
while(cin>>n)
{
csh();
for(int i=0;i<n-1;i++)
{
scanf("%d%d",&a,&b);
build(a,b,0);
build(b,a,1);
}
dfs1(1,0);
dfs2(1,0);
minn=sum[1];
for(int i=1;i<=n;i++)
minn=minn<sum[i]?minn:sum[i];
cout<<minn<<endl;
for(int i=1;i<=n;i++)
if(sum[i]==minn)
cout<<i<<" ";
cout<<endl;
}
return 0;
}
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
The country Treeland consists of n cities, some pairs of them are connected withunidirectional roads. Overall there aren - 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 citya 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. Nextn - 1
lines contain the descriptions of the roads, one road per line. A road is described by a pair of integerssi, ti
(1 ≤ si, ti ≤ n; si ≠ ti)
— the numbers of cities, connected by that road. The i-th road is oriented from citysi to cityti.
You can consider cities in Treeland indexed from 1 ton.
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的话,输出从这一点到其他n-1个点需要花费的代价最小的点的编号,如果有多个点,从小到大输出。
两遍dfs,第一遍求出从点1到其他点的最小代价,然后第二遍更新第一遍dfs所的到的每个点到其他点的代价,因为第一遍如果是求的a->b的代价,当b->a的时候,代价sum[]就需要根据边是否需要反转来改变值。
代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<math.h>
using namespace std;
int p,head[200005],sum[200005];
struct f
{
int son;
int v;
int next;
}tree[500005];
void csh()
{
memset(head,-1,sizeof(head));
memset(sum,0,sizeof(sum));
p=0;
}
void build(int fa,int son,int val)
{
tree[p].son=son;
tree[p].v=val;
tree[p].next=head[fa];
head[fa]=p++;
}
void dfs1(int now,int before)
{
for(int i=head[now];i!=-1;i=tree[i].next)
{
int to=tree[i].son;
if(to==before)
continue;
if(tree[i].v==1)
sum[now]++;
dfs1(to,now);
sum[now]+=sum[to];
}
}
void dfs2(int now,int before)
{
for(int i=head[now];i!=-1;i=tree[i].next)
{
int to=tree[i].son;
if(to==before)
continue;
if(tree[i].v==1)
sum[to]=sum[now]-1;
else
sum[to]=sum[now]+1;
dfs2(to,now);
}
}
int main()
{
int n,a,b,minn;
while(cin>>n)
{
csh();
for(int i=0;i<n-1;i++)
{
scanf("%d%d",&a,&b);
build(a,b,0);
build(b,a,1);
}
dfs1(1,0);
dfs2(1,0);
minn=sum[1];
for(int i=1;i<=n;i++)
minn=minn<sum[i]?minn:sum[i];
cout<<minn<<endl;
for(int i=1;i<=n;i++)
if(sum[i]==minn)
cout<<i<<" ";
cout<<endl;
}
return 0;
}
相关文章推荐
- CF 219D (#135div.2)D. Choosing Capital for Treeland
- CF 219 D Choosing Capital for Treeland(树形dp)
- CF 219 D:Choosing Capital for Treeland(树形dp)
- cf 135div2D Choosing Capital for Treeland 【树形dp】
- [CF 219D]Choosing Capital for Treeland[树形DP]
- CF_D. Choosing Capital for Treeland_树形DP
- CF 深搜 Choosing Capital for Treeland
- 【codeforce 219D】 Choosing Capital for Treeland (树形DP)
- Codeforces 219 D Choosing Capital for Treeland
- 【CF】135 Div2 Choosing Capital for Treeland
- codeforces 219D D. Choosing Capital for Treeland(树形dp)
- Choosing Capital for Treeland (树形dp+双向搜索)
- Codeforces Round #135 (Div. 2)-D. Choosing Capital for Treeland
- Codeforces #135 T4 Choosing Capital for Treeland
- Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland dfs
- CodeForces D. Choosing Capital for Treeland (转化为树形DP)
- [CF219D]Choosing Capital for Treeland(树dp)
- uestc oj 1857 Choosing Capital for Treeland
- CodeForces 219D-Choosing Capital for Treeland(树形dp)
- E - Choosing Capital for Treeland (树形dp)