题意:给一个树形图,n个节点,n-1条有向边,要求选一个节点作为根,使需要改变方向的边的数目最少。并输出所有可能作为根的点。

思路:

  先随便一个点进行DFS,计算将每棵子树的边全部往下时,所需要的费用down[i]。还是那个点进行DFS,这次就要求答案了,尝试将每个点t作为根,那么以t作为根的总费用=down[t]+父亲这棵子树。down[t]已经在第一次DFS中求出,而父亲这棵子树就不是down[父亲]了,而是down[父亲]-down[t]+w(父亲,t)。注:w为边权。

 #include <bits/stdc++.h>
#define pii pair<int,int>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int N=2e5+; struct node
{
int from,to,rev,next;
node(){};
node(int from,int to,int rev,int next):from(from),to(to),rev(rev),next(next){};
}edge[N*];
int head[N], n, edge_cnt;
void add_node(int from,int to,int rev)
{
edge[edge_cnt]=node(from,to,rev,head[from]);
head[from]=edge_cnt++;
} int down[N]; //down[i]表示是往下到点i
void DFS(int t,int far)
{
node e;
for(int i=head[t]; i!=-; i=e.next) //递归先算子树的
{
e=edge[i];
if(e.to^far) DFS(e.to, t);
}
int sum=;
for(int i=head[t]; i!=-; i=e.next) //再算自己的
{
e=edge[i];
if(e.to^far) sum+=down[e.to]+e.rev;
}
down[t]=sum; //只有1种情况:所有子树全部向下
} int ans[N], big;
void DFS2(int t,int far,int val)
{
node e;
ans[t]=down[t]+val; //以本节点为根
big=min(big, ans[t]);
for(int i=head[t]; i!=-; i=e.next)
{
e=edge[i];
if(e.to^far)
{
int r=ans[t]-down[e.to]-e.rev+(e.rev^);
DFS2(e.to, t, r);
}
}
} void init()
{
memset(head, -, sizeof(head));
memset(down, 0x3f, sizeof(down));
edge_cnt=;
big=INF;
} int main()
{
//freopen("input.txt", "r", stdin);
init();
scanf("%d",&n);
for(int i=,a,b; i<n; i++)
{
scanf("%d%d",&a,&b);
add_node(a,b,); //正向
add_node(b,a,);
}
DFS(,-);
DFS2(,-,);
printf("%d\n",big);
for(int i=; i<=n; i++) //输出解
if(big==ans[i])
printf("%d ",i);
return ;
}

AC代码

CodeForces 219D Choosing Capital for Treeland (树形DP)的更多相关文章

  1. Codeforces 219D - Choosing Capital for Treeland(树形dp)

    http://codeforces.com/problemset/problem/219/D 题意 给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个 ...

  2. CodeForces 219D Choosing Capital for Treeland (树形DP)经典

    <题目链接> 题目大意: 给定一个有向树,现在要你从这颗树上选一个点,使得从这个点出发,到达树上其它所有点所需翻转的边数最小,输出最少需要翻转的边数,并且将这些符合条件的点输出. 解题分析 ...

  3. CF 219D Choosing Capital for Treeland 树形DP 好题

    一个国家,有n座城市,编号为1~n,有n-1条有向边 如果不考虑边的有向性,这n个城市刚好构成一棵树 现在国王要在这n个城市中选择一个作为首都 要求:从首都可以到达这个国家的任何一个城市(边是有向的) ...

  4. Codeforces 219D. Choosing Capital for Treeland (树dp)

    题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...

  5. (纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland

    Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes inpu ...

  6. CF#135 D. Choosing Capital for Treeland 树形DP

    D. Choosing Capital for Treeland 题意 给出一颗有方向的n个节点的树,现在要选择一个点作为首都. 问最少需要翻转多少条边,使得首都可以到所有其他的城市去,以及相应的首都 ...

  7. CF219D. Choosing Capital for Treeland [树形DP]

    D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...

  8. Codeforces 219D Choosing Capital for Treeland(树形DP)

    题目是给一张边有向的树形图.要选出首都的点,首都要都能走到其他点,因此要反转一些边的方向.问可以选哪几个点作为首都,使它们所需反转边的数量最少. 这题挺好想的,因为做过HDU2196. 首先就不妨设正 ...

  9. 【题解】codeforces 219D Choosing Capital for Treeland 树型dp

    题目描述 Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市.每条道路只能单向通行.现在政府需要决定选择哪个城市为首都.假如城市i成为了首都,那么为了使首都能到达任意一 ...

  10. [codeforces219D]Choosing Capital for Treeland树形dp

    题意:给出一棵树,带有向边,找出某个点到达所有点需要反转的最少的边. 解题关键:和求树的直径的思路差不多,将求(父树-子树)的最大值改为求特定值.依然是两次dfs,套路解法. 对树形dp的理解:树形d ...

随机推荐

  1. html5 滚动小球

    <html> <head> <meta charset="utf-8"/> </head> <body onkeydown=& ...

  2. JAVA中的BIO,NIO,AIO

    在了解BIO,NIO,AIO之前先了解一下IO的几个概念: 1.同步 用户进程触发IO操作并等待或者轮询的去查看IO操作是否就绪, 例如自己亲自出马持银行卡到银行取钱 2.异步 用户触发IO操作以后, ...

  3. C++11: Multi-core Programming – PPL Parallel Aggregation Explained

    https://katyscode.wordpress.com/2013/08/17/c11-multi-core-programming-ppl-parallel-aggregation-expla ...

  4. java mysql编码问题

    今天使用jdbc连接数据库,sql语句明明是正确的,可就是查不到数据,问题是编码问题,好大的坑啊!!! 我的问题:where语句带汉字找不到信息,如果是英文却可以 第一步:在url后面加上如下的utf ...

  5. 【HBase】HBase笔记:HBase的Region机制

    HBase 的机制里包含了许多优秀的算法,如 Region 定位.Region 分配.Region Server的上线和下线.Master 的上线和下线.在谈到这些之前,先把 HBase 的基本架构里 ...

  6. PHP文件操作的经典案例

    <?php /* 遍历目录函数,只读取目录的最外层的内容 */ function readDirectory($path){ $handle = opendir($path); while(($ ...

  7. sql查询的时候,等于这两个的值得全部取出来

    sql查询的时候  用or连接 ad.jqtype='人文历史' or  ad.jqtype='名胜古迹'

  8. Codeforces Round #269 (Div. 2) A,B,C,D

    CodeForces - 471A 首先要有四个数相等,然后剩下两个数不同就是Bear,否则就是Elephant. #include <bits/stdc++.h> using names ...

  9. UVA12504【C++STL运用】

    雨巨的UVA的C++题集英文真长- 题意: 有两本字典,第一行是旧字典,第二行是新字典. 每行不超过100个字符,没有空格,两本字典都可以是空的: 新key:+ 缺key:- 值变 :* 思路: 具体 ...

  10. hoj2188 WordStack

    WordStack My Tags   (Edit)   Source : Mid-Atlantic 2005   Time limit : 5 sec   Memory limit : 32 M S ...