POJ1655 Balancing Act (树的重心)
求树的重心的模板题,size[u]维护以u为根的子树大小,f[u]表示去掉u后的最大子树。
1 #include<cstdio>
2 #include<iostream>
3 #include<cstring>
4 using namespace std;
5 const int INF=0x7f7f7f7f;
6 const int N=20005;
7 int head[N],to[N*2],nxt[N*2],f[N],size[N];
8 int tot,n,T,center,num;
9
10 void add(int x,int y){
11 nxt[++tot]=head[x];
12 head[x]=tot;
13 to[tot]=y;
14 }
15
16 void dfs(int u,int fa){
17 size[u]=1,f[u]=0;
18 for(int i=head[u];i;i=nxt[i]){
19 int v=to[i];
20 if(v==fa) continue;
21 dfs(v,u);
22 size[u]+=size[v];
23 f[u]=max(f[u],size[v]);
24 }
25 f[u]=max(f[u],n-size[u]);
26 if(f[u]<f[center]||f[u]==f[center]&&u<center){
27 center=u;
28 num=f[u];
29 }
30 }
31
32 void init(){
33 memset(head,0,sizeof(head));
34 tot=0;
35 center=num=0;
36 }
37
38 int main(){
39 scanf("%d",&T);
40 while(T--){
41 init();
42 scanf("%d",&n);
43 int x,y;
44 for(int i=1;i<n;i++){
45 scanf("%d%d",&x,&y);
46 add(x,y);add(y,x);
47 }
48 f[0]=INF;
49 dfs(1,0);
50 printf("%d %d\n",center,num);
51 }
52 return 0;
53 }
POJ1655 Balancing Act (树的重心)的更多相关文章
- POJ1655 Balancing Act(树的重心)
题目链接 Balancing Act 就是求一棵树的重心,然后统计答案. #include <bits/stdc++.h> using namespace std; #define REP ...
- poj-1655 Balancing Act(树的重心+树形dp)
题目链接: Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11845 Accepted: 4 ...
- 『Balancing Act 树的重心』
树的重心 我们先来认识一下树的重心. 树的重心也叫树的质心.找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡. 根据树的重心的定义,我们可 ...
- POJ 1655 Balancing Act 树的重心
Balancing Act Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. ...
- poj1655 Balancing Act 找树的重心
http://poj.org/problem? id=1655 Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- POJ1655 Balancing Act(树的重心)
树的重心即树上某结点,删除该结点后形成的森林中包含结点最多的树的结点数最少. 一个DFS就OK了.. #include<cstdio> #include<cstring> #i ...
- POJ-1655 Balancing Act(树的重心)
Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the t ...
- poj1655 Balancing Act (dp? dfs?)
Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14247 Accepted: 6026 De ...
- poj3107 求树的重心(&& poj1655 同样求树的重心)
题目链接:http://poj.org/problem?id=3107 求树的重心,所谓树的重心就是:在无根树转换为有根树的过程中,去掉根节点之后,剩下的树的最大结点最小,该点即为重心. 剩下的数的 ...
随机推荐
- 使用Properties集合存储数据,遍历取出Properties集合中的数据和Properties集合中的方法store和load
package com.yang.Test.PropertiesStudy; import java.io.FileWriter; import java.io.IOException; import ...
- 华为分析&联运活动,助您提升游戏总体付费
ARPU如何提升?付费率如何提升?活动ROI如何提升?这些都是游戏运营人员较常遇到的难题.华为分析与联运活动可以帮助运营提升这些用户付费指标,通过对玩家打标签和用户画像,对目标群体的进行精准推送,实现 ...
- PureRandom采样类定义和测试
此是随机采样算法,效果感觉一般般. 类声明: #pragma once #ifndef __PURERANDOM_HEADER__ #define __PURERANDOM_HEADER__ #inc ...
- 深入理解Aarch64内存管理
本文是对learn_the_architecture_-_aarch64_memory_management的部分翻译和个人注解.个人英文水平有限,若有翻译不当,欢迎加我私人微信LinuxDriver ...
- Spring MVC组件之HandlerMapping
Spring MVC组件之HandlerMapping HandlerMapping概述 HandlerMapping组件的作用解析一个个Request请求,并找到相应处理这个Request的Hand ...
- BZOJ3295/Luogu3157 [CQOI2011]动态逆序对 (CDQ or 树套树 )
/* Dear friend, wanna learn CDQ? As a surprice, this code is totally wrong. You may ask, then why yo ...
- rcu stall 导致的hung 记录
synchronize_sched 也会在wait_rcu_gp 的长时间等待导致进入hung ,假设rcu没有及时执行的话, 另外,如果rcu积累到一定程度,内存自然就不足了,可能会oom. rcu ...
- 从零开始实现一个MyBatis加解密插件
作者:vivo 互联网服务器团队- Li Gang 本篇文章介绍使用MyBatis插件来实现数据库字段加解密的过程. 一.需求背景 公司出于安全合规的考虑,需要对明文存储在数据库中的部分字段进行加密, ...
- jQuery使用case记录
添加元素/内容追加等 元素内: append() - 在被选元素的结尾插入内容 prepend() - 在被选元素的开头插入内容 元素外: after() - 在被选元素之后插入内容 before() ...
- 【java】学习路线12-内部类的使用事项
//内部类只能在其外部类当中使用//局部内部类:定义在方法里面//如果内部类和外部类有重名,就近原则在内部类中优先访问内部类.//如果想访问宿主类的同名成员,使用OuterClass.this.xxx ...