POJ3107 Godfather (树形DP)
题意:求树的重心
题解:先跑一遍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)的更多相关文章
- POJ 3107.Godfather 树形dp
Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7536 Accepted: 2659 Descrip ...
- [poj3107/poj2378]Godfather/Tree Cutting树形dp
题意:求树的重心(删除该点后子树最大的最小) 解题关键:想树的结构,删去某个点后只剩下它的子树和原树-此树所形成的数,然后第一次dp求每个子树的节点个数,第二次dp求解答案即可. 此题一开始一直T,后 ...
- POJ 1655 BalanceAct 3107 Godfather (树的重心)(树形DP)
参考网址:http://blog.csdn.net/acdreamers/article/details/16905653 树的重心的定义: 树的重心也叫树的质心.找到一个点,其所有的子树中最大的 ...
- poj3107(树的重心,树形dp)
题目链接:https://vjudge.net/problem/POJ-3107 题意:求树的可能的重心,升序输出. 思路:因为学树形dp之前学过点分治了,而点分治的前提是求树的重心,所以这题就简单水 ...
- 【树形dp】Godfather
[POJ3107]Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7212 Accepted: 253 ...
- [poj3107]Godfather_树形dp_树的重心
Godfather poj-3107 题目大意:求树的重心裸题. 注释:n<=50000. 想法:我们尝试用树形dp求树的重心,关于树的重心的定义在题目中给的很明确.关于这道题,我们邻接矩阵存不 ...
- 树形 DP 总结
树形 DP 总结 本文转自:http://blog.csdn.net/angon823/article/details/52334548 介绍 1.什么是树型动态规划 顾名思义,树型动态规划就是在“树 ...
- 树形DP
切题ing!!!!! HDU 2196 Anniversary party 经典树形DP,以前写的太搓了,终于学会简单写法了.... #include <iostream> #inclu ...
- 【转】【DP_树形DP专辑】【9月9最新更新】【from zeroclock's blog】
树,一种十分优美的数据结构,因为它本身就具有的递归性,所以它和子树见能相互传递很多信息,还因为它作为被限制的图在上面可进行的操作更多,所以各种用于不同地方的树都出现了,二叉树.三叉树.静态搜索树.AV ...
随机推荐
- mac 终端经常使用命令(三)
基本命令 1.列出文件 ls 參数 文件夹名 例: 看看驱动文件夹下有什么:ls /System/Library/Extensions 參数 -w 显示中文,-l 具体信息. -a 包含 ...
- 程序中使用cocostudio移植到android手机须要的若干配置过程
首先在解决方式下加入现有项: libCocosStudio.vcxproj E$uVS5Sbv! WL:0n"BExtensions.vcxproj libGUI.vcxproj 然后在pr ...
- python 【第一篇】初识python
人生苦短,我用python Python是我喜欢的语言,简洁.优美.容易使用.所以我总是很激昂的向朋友宣传Python的好处. python起源 1989年,为了打发圣诞节假期,Guido开始写Pyt ...
- Spring中定时器实现
在Spring 中使用Quartz,本文介绍Spring3.0以后自主开发的定时任务工具,spring task,可以将它比作一个轻量级的Quartz,而且使用起来很简单,除spring相关的包外不需 ...
- ios31--NSThread
// // ViewController.m // 03-掌握-NSThread基本使用 #import "ViewController.h" #import "XMGT ...
- Chrome查看JavaScript函数
在页面上右键view page source(Ctrl+U),然后在弹出来的界面可以查找JavaScript函数 注意:这个只能看到内嵌在网页上的JavaScript函数 一般来讲,JavaScrip ...
- WinForm里面连接Oracle数据库
WinForm里面连接Oracle数据库 string oradb = "Data Source=(DESCRIPTION=" + "(ADDRE ...
- [luogu2620]虫洞
https://www.zybuluo.com/ysner/note/1284536 题面 给一个一维坐标系,出发点为\(0\),目标点为\(w\). 每\(1\)秒可以往后移不超过\(s\)个单位距 ...
- Python实现用户交互,显示省市县三级联动的选择
题目:Python实现用户交互,显示省市县三级联动的选择 定义的字典为: dic = { "江西": { "萍乡": ["安源", &quo ...
- Linux压缩命令(zip/gz/bz2/tar/tar.gz/tar.bz2)
一.Linux的压缩格式 .zip . gz . bz2 .tar 1..zip格式(Linux和Windows是可以互传的) 压缩命令 语法:zip 文件名.zip 文件名 ------压缩 ...