题目链接:http://codeforces.com/contest/219/problem/D

题目大意:

  给定一个n个节点的数和连接n个节点的n - 1条有向边,现在要选定一个节点作为起始节点,从这个点出发需要能走到其余每个节点,途中必然要调整有向边的方向,请求出当选定哪些节点作为初始节点时,所要调整的有向边最少,输出最小调整的边数和这些节点。

分析:

  Hint:城市结构是树型结构。

代码如下:

 #include <bits/stdc++.h>
using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define pii pair<int,int>
#define piii pair<pair<int,int>,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} typedef long long LL;
typedef unsigned long long uLL;
const int inf = 1e9 + ;
const LL mod = 1e9 + ;
const int maxN = 2e5 + ; struct Node{
vector< pii > next;
}; int n, ans;
// dp[i]表示以i号城市为首都所要反转的道路数
int dp[maxN];
int vis[maxN];
Node nodes[maxN];
queue< int > Q; // 求dp[x]
inline int dfs(int x) {
if(nodes[x].next.empty()) return ; int ret = ;
foreach(i, nodes[x].next) {
int y = i->fi, z = i->se;
if(vis[y]) continue;
vis[x] = ;
if(z == -) ++ret;
ret += dfs(y);
}
return ret;
} // 如果dp[i]已知,假设j号城市与i号城市相连,由于整个城市图是树形结构,
// 因此dp[j]与dp[i]除了他们的连线有分歧,其余应该反转的道路数量都是一样的
// 因此可由已算出来的1号城市为中心,向外做BFS
inline void bfs() {
ms0(vis);
vis[] = ;
Q.push();
ans = dp[]; while(!Q.empty()) {
int tmp = Q.front();
Q.pop(); foreach(i, nodes[tmp].next) {
int y = i->fi, z = i->se;
if(vis[y]) continue;
vis[y] = ;
dp[y] = dp[tmp] + z;
ans = min(ans, dp[y]);
Q.push(y);
}
}
} int main(){
while(cin >> n) {
ms0(vis);
rep(i, n + ) nodes[i].next.clear();
rep(i, n-) {
int x, y;
cin >> x >> y;
nodes[x].next.push_back(mp(y, ));
nodes[y].next.push_back(mp(x, -));
}
dp[] = dfs(); bfs(); cout << ans << endl;
For(i, , n) if(ans == dp[i]) cout << i << " ";
cout << endl;
}
return ;
}

CodeForces 219D Choosing Capit的更多相关文章

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

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

  2. Codeforces 219D Choosing Capital for Treeland

    http://codeforces.com/problemset/problem/219/D 题目大意: 给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达 ...

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

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

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

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

  5. Codeforces 219D Choosing Capital for Treeland:Tree dp

    题目链接:http://codeforces.com/problemset/problem/219/D 题意: 给你一棵树,n个节点. 树上的边都是有向边,并且不一定是从父亲指向儿子的. 你可以任意翻 ...

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

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

  7. Codeforces 219D Choosing Capital for Treeland 2次DP

    //选择一个根使得变换最少边的方向使得能够到达所有点#include <map> #include <set> #include <list> #include & ...

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

    题意:给一个树形图,n个节点,n-1条有向边,要求选一个节点作为根,使需要改变方向的边的数目最少.并输出所有可能作为根的点. 思路: 先随便一个点进行DFS,计算将每棵子树的边全部往下时,所需要的费用 ...

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

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

随机推荐

  1. js 浏览器兼容css中webkit、Moz、O、ms...写法封装(es6语法)

    /** *浏览器兼容写法封装 */ let elementStyle = document.createElement('div').style let vendor = (() => { le ...

  2. vis.js 4.21.0 Timeline localization

    from:http://visjs.org/timeline_examples.html https://github.com/almende/vis https://github.com/momen ...

  3. 总结:BGP和静态路由并存,达到故障自动倒换的目的。

    总体结论: 在上云的场景中,客户需要本地数据中心到云上VPC,出现网络故障时做到自动倒换,保证业务不中断. 一.客户需求 1.客户有总厂.分厂.总厂是通过专线和VPN连接上云,分厂是通过专线先连接到总 ...

  4. 开启bin-log日志mysql报错:This function has none of DETERMINISTIC, NO SQL解决办法

    开启bin-log日志mysql报错:This function has none of DETERMINISTIC, NO SQL解决办法: 创建存储过程时 出错信息: ERROR 1418 (HY ...

  5. Android EditText常用属性

    一.EditText介绍 ①EditText是一个输入框,在Android开发中是常用的控件.也是获取用户数据的一种方式. ②EditText是TextView的子类,它继承了TextView的所有属 ...

  6. EventBus中观察者模式的应用

    一 介绍 EventBus是一款安卓的开源消息传递框架,地址:https://github.com/greenrobot/EventBus android系统的消息传递非常复杂,比如activity和 ...

  7. Linux 环境 Java JDK 安装&基本配置

    索引: 目录索引 参看代码 GitHub: jdk.txt 一.Linux (DeepinOS) 环境 1.官网下载 jdk-8u112-linux-x64.tar.gz 2.创建目录 mkdir - ...

  8. CentOS7安装MySQL并配置账户等

    注意: 有的Centos版本默认安装了mariadb, 可以先将其卸载 检查mariadb是否安装 yum list installed | grep mariadb 卸载mariadb( all ) ...

  9. 【原】Java学习笔记020 - 面向对象

    package cn.temptation; public class Sample01 { public static void main(String[] args) { // 成员方法的参数列表 ...

  10. Django REST framework基础:视图和路由

    DRF中的Request 在Django REST Framework中内置的Request类扩展了Django中的Request类,实现了很多方便的功能--如请求数据解析和认证等. 比如,区别于Dj ...