CodeForces 219D Choosing Capit

时间:2022-09-20 21:24:02

题目链接: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&period; Choosing Capital for Treeland &lpar;树dp&rpar;

    题目链接: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&lpar;树形dp&rpar;

    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 &lpar;树形DP&rpar;经典

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

随机推荐

  1. shell中的条件判断、参数以及变量替换

    文章转自: http://www.cnblogs.com/maxupeng/archive/2011/07/02/2096551.html 一.test命令 test命令是shell内部命令,它计算作 ...

  2. CDHtmlDialog的基本使用

    转自:http://blog.csdn.net/sky04/article/details/7587406 因为我的部门只有我一个人(无奈之极,只有我一个做C++的,其他的都在做C#),所以我去跟技术 ...

  3. JavaScript Window Screen

    window.screen 对象包含有关用户屏幕的信息. Window Screen window.screen对象在编写时可以不使用 window 这个前缀. 一些属性: screen.availW ...

  4. java转发和重定向

    1,请求重定向:客户端行为,response.sendRedirect(),从本质上讲等同于两次请求,前一次的请求对象不会保持,地址栏的URL地址会改变.2,请求转发:服务器行为,request.ge ...

  5. Powerdesigner&plus;Execel

    1.将Powerdesigner中的表(PDM)导入到execel中 Ctrl+Shift+X/tool->Execute commands ->Edit/Run script 粘贴如下v ...

  6. mongodb 数据库中 的聚合操作

  7. 黄聪:AngularJS中的&dollar;resource使用与Restful资源交互(转)

    原文:http://blog.csdn.net/he90227/article/details/50525836 1.AngularJS中的 $resource 这个服务可以创建一个资源对象,我们可以 ...

  8. 深入理解mysql的隔离级别

    建表插入测试数据A> create table test(id int ,num int) ;Query OK, 0 rows affected (0.53 sec) A> insert ...

  9. 《深入浅出MyBatis技术原理与实战》——6&period; MyBatis的解析和运行原理

    MyBatis的运行分为两大部分,第一部分是读取配置文件缓存到Configuration对象,用以创建SqlSessionFactory,第二部分是SqlSession的执行过程. 6.1 涉及的技术 ...

  10. red hat 7 启动过程(EFI)

    不同版本的Linux系统的启动过程在某些地方是不一样的,现在先来介绍一下red hat 7 的启动过程(EFI). (加电→图形登录界面) 接通电源 按下电源键 EFI固件启动 初始化硬件 从EFI启 ...