一个国家,有n座城市,编号为1~n,有n-1条有向边

如果不考虑边的有向性,这n个城市刚好构成一棵树

现在国王要在这n个城市中选择一个作为首都

要求:从首都可以到达这个国家的任何一个城市(边是有向的)

所以一个城市作为首都,可能会有若干边需要改变方向

现在问,选择哪些城市作为首都,需要改变方向的边最少。

输出最少需要改变方向的边数

输出可以作为首都的编号

树形DP

先假定城市1作为首都

令tree(i)表示以i为根的子树

dp[i]表示在tree(i)中,若以i为首都的话,需要改变的边数

第一次dfs,求出dp数组

ans[i]表示在整棵树中,若以i为首都的话,需要改变的边数(注意和dp数组的意义的区别)

明显:ans[1]=dp[1]

明显有:

在一棵有向树中,若首都为u,现在我们要让u的儿子节点v为首都,只需要改变1条边的方向

假设节点u是节点v的父节点,e=(u,v),ans[u]已知

若e的方向指向v,则:ans[v]=ans[u]+1

否则:ans[v]=ans[u]-1

所以第二次dfs,递推求出ans数组

接着,找出min=min(ans[i]),并输出min

输出所有使得ans[i]==min的i

 #include<cstdio>
#include<cstring> using namespace std; const int maxn=+; struct Edge
{
int to,next;
bool flag;
};
Edge edge[maxn<<];
int head[maxn];
int tot;
int ans[maxn];
int dp[maxn];
int print[maxn]; void init()
{
memset(head,-,sizeof head);
tot=;
memset(dp,,sizeof dp);
} void addedge(int u,int v,bool flag)
{
edge[tot].to=v;
edge[tot].flag=flag;
edge[tot].next=head[u];
head[u]=tot++;
} void dfs0(int ,int );
void dfs1(int ,int ); int main()
{
int n;
init();
bool cnt=true;
scanf("%d",&n);
for(int i=;i<n;i++)
{
int u,v;
scanf("%d %d",&u,&v);
addedge(u,v,cnt);
addedge(v,u,!cnt);
}
dfs0(,-);
ans[]=dp[];
dfs1(,-); int min=ans[];
for(int i=;i<=n;i++)
{
if(ans[i]<min)
min=ans[i];
}
tot=;
for(int i=;i<=n;i++)
{
if(ans[i]==min)
{
print[tot++]=i;
}
} printf("%d\n",min);
for(int i=;i<tot-;i++)
{
printf("%d ",print[i]);
}
printf("%d\n",print[tot-]); return ;
} void dfs0(int u,int pre)
{
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].to;
bool flag=edge[i].flag;
if(v==pre)
continue;
dfs0(v,u);
if(flag)
dp[u]+=dp[v];
else
dp[u]+=(dp[v]+);
}
return ;
} void dfs1(int u,int pre)
{
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].to;
int flag=edge[i].flag;
if(v==pre)
continue;
if(flag)
ans[v]=ans[u]+;
else
ans[v]=ans[u]-;
dfs1(v,u);
}
return ;
}

CF 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#135 D. Choosing Capital for Treeland 树形DP

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

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

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

  5. [CF] 219D Choosing Capital for Treeland

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

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

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

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

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

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

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

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

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

随机推荐

  1. spark1.5引进内置函数

    在Spark 1.5.x版本,增加了一系列内置函数到DataFrame API中,并且实现了code-generation的优化.与普通的函数不同,DataFrame的函数并不会执行后立即返回一个结果 ...

  2. URAL 2034 Caravans(变态最短路)

    Caravans Time limit: 1.0 secondMemory limit: 64 MB Student Ilya often skips his classes at the unive ...

  3. codevs3872 邮递员送信(SPFA)

    邮递员送信 时间限制: 1 Sec  内存限制: 64 MB提交: 10  解决: 5[提交][状态][讨论版] 题目描述 有一个邮递员要送东西,邮局在节点1.他总共要送N-1样东西,其目的地分别是2 ...

  4. 课堂所讲整理:HTML--7JavaScript的DOM操作

    1.DOM的基本概念 DOM是文档对象模型,这种模型为树模型:文档是指标签文档:对象是指文档中每个元素:模型是指抽象化的东西. 2.Window对象操作 一.属性和方法: 属性(值或者子对象): op ...

  5. 工厂方法模式(FACTORY METHOD)

    核心精神是封装类中不变的部分,提取其中个性化善变的部分为独立类,通过依赖注入以达到解耦.复用和方便后期维护拓展的目的. 工厂方法(Factory Method)模式的意义是定义一个创建产品对象的工厂接 ...

  6. Java并发编程-并发工具包(java.util.concurrent)使用指南(全)

    1. java.util.concurrent - Java 并发工具包 Java 5 添加了一个新的包到 Java 平台,java.util.concurrent 包.这个包包含有一系列能够让 Ja ...

  7. Java static block static constructor , static field

    http://stackoverflow.com/questions/7121213/singleton-instantiation http://docs.oracle.com/javase/spe ...

  8. Spark 优化器 ML的论文

    http://people.csail.mit.edu/matei/papers/2015/sigmod_spark_sql.pdf http://www.vldb.org/pvldb/vol4/p5 ...

  9. Git-仓库迁移

    如果你想从别的 Git 托管服务那里复制一份源代码到新的 Git 托管服务器上的话,可以通过以下步骤来操作.1). 从原地址克隆一份裸版本库,比如原本托管于 GitHub. git clone --b ...

  10. Nginx-SSI

    <a href="<!--#include file="/$SERVER_NAME.shtml"-->">点击</a> a