http://blog.csdn.net/acdreamers/article/details/16905653

题意:给定一棵树,求树的重心的编号以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的.

分析:首先要知道什么是树的重心,树的重心定义为:找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重

心后,生成的多棵树尽可能平衡.  实际上树的重心在树的点分治中有重要的作用, 可以避免N^2的极端复杂度(从退化链的一端出发),保证

NlogN的复杂度, 利用树型dp可以很好地求树的重心.

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define MAXN 20000+5
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue
#define INF 0x3f3f3f3f #define ls (rt<<1)
#define rs (rt<<1|1) int n,m; int ptr = ,head[MAXN],vis[MAXN]; int res,ans,son[MAXN]; struct node
{
int y,val,next;
}tree[MAXN<<]; void add(int fa,int son)
{
tree[ptr].y = son;
tree[ptr].next = head[fa];
head[fa] = ptr++;
} void dfs(int root)
{
vis[root] = ;
son[root] = ;
int tmp = ;
for(int i=head[root];i!=-;i=tree[i].next)
{
int y = tree[i].y;
if(vis[y]) continue;
dfs(y);
son[root] += son[y]+;
tmp = max(son[y]+,tmp);
}
tmp = max(tmp,n-son[root]-);
if(tmp<res || tmp == res && root < ans)
{
ans = root;
res = tmp;
}
} int main()
{
int i,j,t,kase=;
sf("%d",&t);
while(t--)
{
mem(tree,);
mem(head,-);
mem(vis,);
ans = INF,res=INF;
ptr = ;
sf("%d",&n);
int x,y;
for(i=;i<n;i++)
{
sf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
dfs();
pf("%d %d\n",ans,res);
}
return ;
}

在这题里出现了这样一个情况,导致我第一次时TLE了

#define MAXN 20000+5

struct node
{
int y,val,next;
}tree[MAXN*2];

因为是无向边,我习惯性地乘了2,导致结果错误。这里其实算出来的是20000+5*2 = 20010

改正有这样两种方法:

1.移位运算级低,可以直接用

#define MAXN 20000+5

struct node
{
int y,val,next;
}tree[MAXN<<];

2.define加括号

#define MAXN (20000+5)

struct node
{
int y,val,next;
}tree[MAXN*];

poj 1655 树的重心 && define注意事项的更多相关文章

  1. poj 1655 树的重心

    Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13178   Accepted: 5565 De ...

  2. POJ 1655 求树的重心

    POJ 1655 [题目链接]POJ 1655 [题目类型]求树的重心 &题意: 定义平衡数为去掉一个点其最大子树的结点个数,求给定树的最小平衡数和对应要删的点.其实就是求树的重心,找到一个点 ...

  3. POJ 1655 - Balancing Act - [DFS][树的重心]

    链接:http://poj.org/problem?id=1655 Time Limit: 1000MS Memory Limit: 65536K Description Consider a tre ...

  4. poj 1655 Balancing Act(找树的重心)

    Balancing Act POJ - 1655 题意:给定一棵树,求树的重心的编号以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的. /* 找树的重心可以用树形dp或 ...

  5. POJ 1655 Balancing Act (求树的重心)

    求树的重心,直接当模板吧.先看POJ题目就知道重心什么意思了... 重心:删除该节点后最大连通块的节点数目最小 #include<cstdio> #include<cstring&g ...

  6. POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)

    树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...

  7. POJ.1655 Balancing Act POJ.3107 Godfather(树的重心)

    关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ...

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

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

  9. poj 1655 Balancing Act 求树的重心【树形dp】

    poj 1655 Balancing Act 题意:求树的重心且编号数最小 一棵树的重心是指一个结点u,去掉它后剩下的子树结点数最少. (图片来源: PatrickZhou 感谢博主) 看上面的图就好 ...

随机推荐

  1. vue的生命周期钩子函数

    一.vue生命周期图示 二.钩子函数执行时间 beforeCreate      在创建实例之前,data只声明但没有赋值  在实例初始化之后,数据观测 (data observer) 和 event ...

  2. cancelbubble和stoppraopagation区别

    事实上stoppropagation和cancelBubble的作用是一样的,都是用来阻止浏览器默认的事件冒泡行为. 不同之处在于stoppropagation属于W3C标准,试用于Firefox等浏 ...

  3. 二、为什么要选用pytest以及 pytest与unittest比较

    为什么要选择pytest,我看中的如下: 写case,不需要像unittest那样,创建测试类,继承unittest.TestCase pytest中的fixture(类似于setUp.tearDow ...

  4. 洛谷 P1372 又是毕业季I

    可能所有的数论题都是这样玄学.... 题目链接:https://www.luogu.org/problemnew/show/P1372 这道题通过暴力的枚举可以发现是不可做的(当然我也不会做) 然后就 ...

  5. Python循环流程

    1.for循环 计算1+2+3+……+100的和 count = 0 i = 1 for i in range(101): count+=i print(count) 前n项和公式为:Sn=n*a1+ ...

  6. Apache 去掉 www

    1 用phpstudy的网友打开“其他选项菜单”- “配置文件”-httpd-conf.找到 #LoadModule rewrite_module modules/mod_rewrite.so 把这一 ...

  7. 浏览器端 禁止 html 使用后退 或者替换后退功能..

    知乎大佬的代码: 作者:独夜行 链接:https://www.zhihu.com/question/40511430/answer/166467343 来源:知乎 著作权归作者所有.商业转载请联系作者 ...

  8. Hero

    Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description When pl ...

  9. 一款不错的Linux终端颜色设置

    PS1="\[\e[37;40m\][\[\e[32;40m\]\u\[\e[37;40m\]@\h \[\e[36;40m\]\w\[\e[0m\]]\\$ " #步骤# vi ...

  10. Python-删除列表中重复元素的方法

    1.set()方法 x = [1,2,3,4,5,1] y = list(set(x)) print(y) ``` [1, 2, 3, 4, 5] ``` 2. x = ['b','c','d','b ...