题意:求树的重心

题解:先跑一遍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. Android一些网站介绍

    1.开源镜像: Android SDK在线更新镜像服务器 中国科学院开源协会镜像站地址: IPV4/IPV6: http://mirrors.opencas.cn 端口:80 IPV4/IPV6: h ...

  2. go2基本类型

    /* Go基本类型 布尔型:bool - 长度:1字节 - 取值范围:true, false - 注意事项:不可以用数字代表true或false 整型:int/uint - 根据运行平台可能为32或6 ...

  3. 安装Node.js 以及命令行使用

    安装 官方的安装包 安装完成之后,会自动添加到环境变量中 通过visual studio安装 命令行 查看版本 PS C:\Users\clu\Desktop> node --version v ...

  4. CodeForces 632C Grandma Laura and Apples (模拟)

    题意:有n个人买苹果,当苹果剩余偶数时买走一半,当苹果剩余奇数时,先买走一半,再用半价买走一个苹果,最终苹果恰好卖完.农民收入为多少. 析:反向模拟. 代码如下: #pragma comment(li ...

  5. bzoj 1724: [Usaco2006 Nov]Fence Repair 切割木板【堆】

    如果反着看,看成合并木板,就和合并果子一样了,把若干块放进一个小根堆,然后每次取出两个合并,把合并结果加进答案和堆里 代码里小根堆用优先队列实现(懒 #include<iostream> ...

  6. 聊聊LuaJIT

    JIT 什么是JITJIT = Just In Time即时编译,是动态编译的一种形式,是一种优化虚拟机运行的技术. 程序运行通常有两种方式,一种是静态编译,一种是动态解释,即时编译混合了这二者.Ja ...

  7. 状态压缩+枚举 UVA 11464 Even Parity

    题目传送门 /* 题意:求最少改变多少个0成1,使得每一个元素四周的和为偶数 状态压缩+枚举:枚举第一行的所有可能(1<<n),下一行完全能够由上一行递推出来,b数组保存该位置需要填什么 ...

  8. JavaScript Json与Map互转以及Map对象的取值方式

    Json格式(Json字符串) : var json='{"name": "lily","age":"15"}' Map ...

  9. .Net实战之反射操作篇

    1.上一讲中描述了反射中常见的类,仅仅是描述类与反射之间的关系.   但是实际是对数据的操作,  在反射中,数据如何操作? [MyTable("T_UserInfo")] publ ...

  10. cocos2d-x android 环境部署

    1.下载jdk http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 2.下载 and ...