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

题意:

  给你一棵树,n个节点。

  树上的边都是有向边,并且不一定是从父亲指向儿子的。

  你可以任意翻转一些边的方向。

  现在让你找一个节点,使得从这个节点出发能够到达其他所有节点,并保证翻转边的数量最小。

  问你最少翻转多少条边,并输出所有满足此条件的节点编号。

题解:

  本题要解两个dp: dp1 & dp2

  首先考虑dp1:

    表示状态:

      dp1[i]表示使节点i能够到达i的子树中的所有节点,翻转边的最小数量。

    如何转移:

      dp1[i] = ∑ (dp1[son] + 边<i,son>由i指向son ? 0 : 1)

      dfs一遍即可。

  然后求dp2:

    表示状态:

      dp2[i]表示使节点i能够到达这棵树的所有节点,翻转边的最小数量。

    如何转移:

      dp2[i] = dp2[par] + 边<par,i>是否由par指向i ? 1 : -1

      如果边<par,i>由par指向i,那么在dp2[par]中这条边是不会被翻转的,所以此时应该将它翻转,代价+1。

      如果边<par,i>由i指向par,那么在dp2[par]中这条边已经被翻转了一次,然而在dp2[i]中是不需要翻转的,所以将dp2[par]中多余的那次翻转减掉就好。

    边界条件:

      dp2[1] = dp1[1]

      (默认根节点为1)

  所以最终答案为dp2[i]中的最小值,然后将所有dp2[i]等于ans的节点输出就好啦。

AC Code:

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#define MAX_N 200005
#define INF 1000000000 using namespace std; struct Edge
{
int dest;
bool flag;
Edge(int _dest,bool _flag)
{
dest=_dest;
flag=_flag;
}
Edge(){}
}; int n;
int ans=INF;
int dp1[MAX_N];
int dp2[MAX_N];
vector<Edge> edge[MAX_N]; void read()
{
cin>>n;
int x,y;
for(int i=;i<n;i++)
{
cin>>x>>y;
edge[x].push_back(Edge(y,true));
edge[y].push_back(Edge(x,false));
}
} void dfs1(int now,int p)
{
dp1[now]=;
for(int i=;i<edge[now].size();i++)
{
Edge temp=edge[now][i];
if(temp.dest!=p)
{
dfs1(temp.dest,now);
dp1[now]+=dp1[temp.dest]+(!temp.flag);
}
}
} void dfs2(int now,int p,bool d)
{
if(now==) dp2[now]=dp1[now];
else dp2[now]=dp2[p]+(d?:-);
ans=min(ans,dp2[now]);
for(int i=;i<edge[now].size();i++)
{
Edge temp=edge[now][i];
if(temp.dest!=p) dfs2(temp.dest,now,temp.flag);
}
} void work()
{
dfs1(,-);
dfs2(,-,);
cout<<ans<<endl;
for(int i=;i<=n;i++)
{
if(dp2[i]==ans) cout<<i<<" ";
}
cout<<endl;
} int main()
{
read();
work();
}

Codeforces 219D Choosing Capital for Treeland:Tree dp的更多相关文章

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

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

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

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

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

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

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

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

  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. Codeforces 219D. Choosing Capital for Treeland (树dp)

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

  7. Codeforces 219D Choosing Capital for Treeland

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

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

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

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

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

随机推荐

  1. CSDN--字体颜色--markdown

    在写blog时,想高亮某些字,但是发现markdown更改字体颜色不像word里那么方便,于是查了一下,要用一下代码进行更改字体颜色,还可以更改字体大小,还有字体格式 <font 更改语法> ...

  2. 由浅到深理解ROS(2)

    ROS文件系统 用户可以直接参看官网:http://wiki.ros.org/ROS/Tutorials/NavigatingTheFilesystem ROS文件系统中的两个最基本的概念:Packa ...

  3. math课本复习

    第七章 微分方程 第一节 微分方程的基本概念    未知函数.未知函数的倒数与自变量之间的关系的方程,叫做微分方程. 第二节 可分离变量的微分方程 第三节 齐次方程 第四节 一阶线性微分方程 总结:任 ...

  4. HTTP/HLS/RTMP超级负载测试工具(转)

    这个负载测试工具是网游分享的工具,可以在http://blog.csdn.net/win_lin/article/details/11835011 或者https://github.com/winli ...

  5. linux集群管理

    本文以ubuntu-16.04.3-server-amd64为例,搭建服务器集群.同样是依托于虚拟机. 创建第一个节点 创建新的虚拟机参见:创建新的虚拟机,创建之后,编辑虚拟机,选择Ubuntu镜像, ...

  6. SVM vs. Softmax

    http://cs231n.github.io/linear-classify/

  7. js打开新窗口: window.open

    var iWidth = 800; var iHeight = 600; var iLeft = (window.screen.width - 10 - iWidth) / 2; //获得窗口的水平位 ...

  8. Python菜鸟之路:Python基础-内置函数补充

    常用内置函数及用法: 1. callable() def callable(i_e_, some_kind_of_function): # real signature unknown; restor ...

  9. PAT 1058. 选择题(20)

    批改多选题是比较麻烦的事情,本题就请你写个程序帮助老师批改多选题,并且指出哪道题错的人最多. 输入格式: 输入在第一行给出两个正整数N(<=1000)和M(<=100),分别是学生人数和多 ...

  10. 代替print输出的PY调试库:PySnooper

      PySnooper¶ Github:https://github.com/lotapp/PySnooper pip install pysnooper 使用:分析整个代码 @pysnooper.s ...