题意:求树的重心

题解:先跑一遍dfs 预处理出这种遍历方式每个节点的儿子(含自己)的数

   再跑一遍 每个点的值就是他所有儿子中取一个最大值 再和它父亲这个方向比较一下

   又被卡常了 vector一直tle 需要用前向星....

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <vector>
#include <string.h>
using namespace std; int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} struct node
{
int no, to, nex;
}E[]; int n, rt;
int du[];
int head[];
int son[];
int dp[]; int dfs1(int x, int fa)
{
son[x] = ;
int c = head[x];
for(int i = c; i; i = E[i].nex)
{
int c = E[i].to;
if(c == fa) continue; son[x] += dfs1(c, x);
}
return son[x];
} void dfs2(int x, int fa)
{
dp[x] = n - son[x];
int c = head[x];
for(int i = c; i; i = E[i].nex)
{
int c = E[i].to;
if(c == fa) continue; dp[x] = max(dp[x], son[c]);
dfs2(c, x);
}
} int main()
{
while(~scanf("%d", &n))
{
for(int i = ; i <= n; i++) du[i] = son[i] = dp[i] = head[i] = ; int cn = ;
for(int i = ; i < n; i++)
{
int u, v;
u = read(); v = read();
E[++cn].no = u;
E[cn].to = v;
E[cn].nex = head[u];
head[u] = cn; E[++cn].no = v;
E[cn].to = u;
E[cn].nex = head[v];
head[v] = cn;
du[u]++;
du[v]++;
} for(int i = ; i <= n; i++)
if(du[i] == )
{
rt = i;
break;
} int o = dfs1(rt, -);
dfs2(rt, -); int ans = n + ;
int cnt = ;
for(int i = ; i <= n; i++) ans = min(ans, dp[i]);
for(int i = ; i <= n; i++)
if(dp[i] == ans) cnt++; for(int i = ; i <= n; i++)
{
if(dp[i] == ans)
{
if(cnt == )
{
printf("%d\n", i);
break;
}
else printf("%d ", i);
cnt--;
}
}
}
return ;
}

POJ3107 Godfather (树形DP)的更多相关文章

  1. POJ 3107.Godfather 树形dp

    Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7536   Accepted: 2659 Descrip ...

  2. [poj3107/poj2378]Godfather/Tree Cutting树形dp

    题意:求树的重心(删除该点后子树最大的最小) 解题关键:想树的结构,删去某个点后只剩下它的子树和原树-此树所形成的数,然后第一次dp求每个子树的节点个数,第二次dp求解答案即可. 此题一开始一直T,后 ...

  3. POJ 1655 BalanceAct 3107 Godfather (树的重心)(树形DP)

    参考网址:http://blog.csdn.net/acdreamers/article/details/16905653   树的重心的定义: 树的重心也叫树的质心.找到一个点,其所有的子树中最大的 ...

  4. poj3107(树的重心,树形dp)

    题目链接:https://vjudge.net/problem/POJ-3107 题意:求树的可能的重心,升序输出. 思路:因为学树形dp之前学过点分治了,而点分治的前提是求树的重心,所以这题就简单水 ...

  5. 【树形dp】Godfather

    [POJ3107]Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7212   Accepted: 253 ...

  6. [poj3107]Godfather_树形dp_树的重心

    Godfather poj-3107 题目大意:求树的重心裸题. 注释:n<=50000. 想法:我们尝试用树形dp求树的重心,关于树的重心的定义在题目中给的很明确.关于这道题,我们邻接矩阵存不 ...

  7. 树形 DP 总结

    树形 DP 总结 本文转自:http://blog.csdn.net/angon823/article/details/52334548 介绍 1.什么是树型动态规划 顾名思义,树型动态规划就是在“树 ...

  8. 树形DP

    切题ing!!!!! HDU  2196 Anniversary party 经典树形DP,以前写的太搓了,终于学会简单写法了.... #include <iostream> #inclu ...

  9. 【转】【DP_树形DP专辑】【9月9最新更新】【from zeroclock's blog】

    树,一种十分优美的数据结构,因为它本身就具有的递归性,所以它和子树见能相互传递很多信息,还因为它作为被限制的图在上面可进行的操作更多,所以各种用于不同地方的树都出现了,二叉树.三叉树.静态搜索树.AV ...

随机推荐

  1. SPOJ 15. The Shortest Path 最短路径题解

    本题就是给出一组cities.然后以下会询问,两个cities之间的最短路径. 属于反复询问的问题,临时我仅仅想到使用Dijsktra+heap实现了. 由于本题反复查询次数也不多,故此假设保存全部最 ...

  2. 第二步:将LAD结果的属性值二(多)值化,投入计算模型

    一文详解LDA主题模型 - 达观数据 - SegmentFault 思否 https://segmentfault.com/a/1190000012215533 SELECT COUNT(1) FRO ...

  3. 危险的input 微博的过去

    更改uid post地址不变

  4. 容器HashSet原理(学习)

    一.概述 使用HashMap存储,非线程安全: 二.实现 HashSet 底层使用 HashMap 来保存所有元素,因此 HashSet 的实现比较简单,相关 HashSet 的操作,基本上都是直接调 ...

  5. XML解析方式汇总

    XML解析方式汇总 分类: XML2011-08-23 19:19 167人阅读 评论(0) 收藏 举报 xmlstringexceptionattributesclassiterator DOM解析 ...

  6. 4.3 Writing a Grammar

    4.3 Writing a Grammar Grammars are capable of describing most, but not all, of the syntax of program ...

  7. openStack Aio 环境的neutron agent-list和cluster 环境 CLI结果对比

  8. java静态方法和实例化方法的区别(copy)

    [资料来源] http://blog.csdn.net/biaobiaoqi/article/details/6732117 方法是我们每天都在写得,很多程序员大多都使用实例化方法,而很少使用静态方法 ...

  9. SQL Server 方言类型映射问题

    关于SQL Server的类型映射问题,例如,nvarchar无法进行hibernate类型映射,需要通过convert进行类型转换方可进行获取

  10. bzoj 1644: [Usaco2007 Oct]Obstacle Course 障碍训练课【spfa】

    洛谷的数据毒啊 把(i,j,k)作为一个点spfa,表示点(i,j)朝向k方向,然后向四个方向转移即可 #include<iostream> #include<cstdio> ...