【CodeForces219D】Choosing Capital for Treeland
Choosing Capital for Treeland
题目描述
The country Treeland consists of n cities, some pairs of them are connected with unidirectional roads. Overall there are n−1roads 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.
输入格式
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.
输出格式
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.
样例输入1
3 2 1 2 3
样例输出1
0 2
样例输入2
4 1 4 2 4 3 4
样例输出2
2 1 2 3
题解
题意:有一棵树,边是有向边。
要求选择一个点,满足朝向这个点的边最少。
很经典的一道树上dp题。
我们钦定一个根,dp点i子树下朝向和背向它的边和子树外朝向和背向它的边。
那么我们第一次dfs就可以求出子树下的两个dp,这个转移很简单,就不讲了。
第二次dfs我们就可以转移子树上的两个dp。
upin[u]=upin[fa];
upin[u]+=ssin[fa]-ssin[u]-(!kfa[u]);
upin[u]+=kfa[u];
upout[u]=upout[fa];
upout[u]+=sout[fa]-sout[u]-kfa[u];
upout[u]+=(!kfa[u]);
upin[u]表示u上面朝向u的边数。
upout[u]表示u上面背向u的边数。
ssin[u]表示u下面朝向u的边数。
sout[u]表示u下面背向u的边数。
kfa[u]表示u连向父亲的边的方向。
上代码:
#include<bits/stdc++.h>
using namespace std;
int n;
int u,v;
struct aa{
int to,nxt;
bool k;
}p[400009];
int h[200009],len=1;
void add(int u,int v,bool k){
p[++len].to=v;
p[len].k=k;
p[len].nxt=h[u];
h[u]=len;
}
bool kfa[200009];
int fa[200009];
int ssin[200009],sout[200009],upin[200009],upout[200009];
void dfs(int u,int fa){
for(int j=h[u];j;j=p[j].nxt){
if(p[j].to==fa) continue;
dfs(p[j].to,u);
ssin[u]+=ssin[p[j].to];
sout[u]+=sout[p[j].to];
kfa[p[j].to]=p[j].k;
if(p[j].k) sout[u]++;
else ssin[u]++;
}
}
void dfss(int u,int fa){
if(fa!=0){
upin[u]=upin[fa];
upin[u]+=ssin[fa]-ssin[u]-(!kfa[u]);
upin[u]+=kfa[u];
upout[u]=upout[fa];
upout[u]+=sout[fa]-sout[u]-kfa[u];
upout[u]+=(!kfa[u]);
}
for(int j=h[u];j;j=p[j].nxt){
if(p[j].to==fa) continue;
dfss(p[j].to,u);
}
}
int main(){
scanf("%d",&n);
for(int j=1;j<n;j++){
scanf("%d%d",&u,&v);
add(u,v,1);
add(v,u,0);
}
dfs(1,0);
dfss(1,0);
int mn=n;
for(int j=1;j<=n;j++)
mn=min(mn,ssin[j]+upin[j]);
printf("%d\n",mn);
for(int j=1;j<=n;j++)
if(ssin[j]+upin[j]==mn) printf("%d ",j);
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 内存占用高分析
· .NET Core GC计划阶段(plan_phase)底层原理浅谈
· .NET开发智能桌面机器人:用.NET IoT库编写驱动控制两个屏幕
· 用纯.NET开发并制作一个智能桌面机器人:从.NET IoT入门开始
· 一个超经典 WinForm,WPF 卡死问题的终极反思
· 支付宝事故这事儿,凭什么又是程序员背锅?有没有可能是这样的...
· 在线客服系统 QPS 突破 240/秒,连接数突破 4000,日请求数接近1000万次,.NET 多
· C# 开发工具Visual Studio 介绍
· 在 Windows 10 上实现免密码 SSH 登录
· C#中如何使用异步编程