POJ1655 Balancing Act(树的重心)
题目链接 Balancing Act
就是求一棵树的重心,然后统计答案。
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i(0); i < (n); ++i)
#define for_edge(i,x) for(int i = H[x]; i; i = X[i]) const int INF = 1 << 30;
const int N = 100000 + 10; int H[N << 1], E[N << 1], X[N << 1];
int T, et, n, x, y;
int son[N];
int ans_size, ans; inline void addedge(int a, int b){
E[++et] = b, X[et] = H[a], H[a] = et;
E[++et] = a, X[et] = H[b], H[b] = et;
} void dfs(int x, int fa){
int sn = 0;
for_edge(i, x){
int u = E[i];
if (u != fa){
dfs(u, x);
son[x] += son[u];
sn = max(sn, son[u]);
}
}
++son[x];
sn = max(sn, n - son[x]);
if (ans_size > sn || ans_size == sn && ans > x){
ans_size = sn;
ans = x;
} } int main(){ scanf("%d", &T);
for (; T--;){
ans_size = INF; ans = INF;
memset(H, 0, sizeof H);
memset(son, 0, sizeof son);
et = 0;
scanf("%d", &n);
REP(i, n - 1){
scanf("%d%d", &x, &y);
addedge(x, y);
}
dfs(1, 0);
printf("%d %d\n", ans, ans_size);
} return 0; }
POJ1655 Balancing Act(树的重心)的更多相关文章
- 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 (树的重心)
求树的重心的模板题,size[u]维护以u为根的子树大小,f[u]表示去掉u后的最大子树. 1 #include<cstdio> 2 #include<iostream> 3 ...
- 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 求树的重心,所谓树的重心就是:在无根树转换为有根树的过程中,去掉根节点之后,剩下的树的最大结点最小,该点即为重心. 剩下的数的 ...
随机推荐
- mysql-show processlist之writing to net
mysql提示Writing to net解决 最近发现某一个数据库cpu占用比较过.超过200%了. 首先查看数据库慢日志,设定慢日志5秒,基本上没有产生日,没有超过5秒的语句. show proc ...
- django 自定义过滤器中的坑.
今天在创建自定义过滤器的时候,设置已正常.但是在运行后报: 'filter' is not a valid tag library: Template library filter not found ...
- JAVA后端常用框架SSM,redis,dubbo等
JAVA后端常用框架SSM,redis,dubbo等 一.SpringMVC http://blog.csdn.net/evankaka/article/details/45501811 spri ...
- Java并发之(2):线程通信wait/notify(TIJ_21_5)
简介: java中线程间同步的最基本的方式就是使用wait()¬ify()¬ifyAll(),它们是线程间的握手机制.除了上述方法,java5还在java.util.con ...
- Django框架学习-01Django介绍
01-Django介绍 02-HTTP协议介绍 01-Django介绍 1.什么是Web框架? 随着Web最新发展趋势的不断升级,Web项目开发也越来越难,而且需要花费更多的开发时间.所以,Web程序 ...
- 【LeetCode】Excel Sheet Column Number(Excel表列序号)
这道题是LeetCode里的第171道题. 题目描述: 给定一个Excel表格中的列名称,返回其相应的列序号. 例如, A -> 1 B -> 2 C -> 3 ... Z -> ...
- Leetcode 593.有效正方形
有效正方形 给定二维空间中四点的坐标,返回四点是否可以构造一个正方形. 一个点的坐标(x,y)由一个有两个整数的整数数组表示. 示例: 输入: p1 = [0,0], p2 = [1,1], p3 = ...
- leetcode NO.53 最大子序列和 (python实现)
来源 https://leetcode-cn.com/problems/maximum-subarray/description/ 题目描述 给出一个 32 位的有符号整数,你需要将这个整数中每位上的 ...
- outlook同步异常
新装的系统,备份了outlook,还原后发现,outlook还在不停的同步服务端邮件,设置规则,禁止接收今天之前的邮件,但是outloock还是在同步,只是不接收而已,这样导致了莫名其妙的异常错误,o ...
- vNS12 MySQL Performance
vNS12 MySQL Performance vNS12(192.168.195.91): 2vCPU + 2GBMemMySQL1(192.168.185.73): 2vCPU + 1GBMemM ...