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 求树的重心,所谓树的重心就是:在无根树转换为有根树的过程中,去掉根节点之后,剩下的树的最大结点最小,该点即为重心. 剩下的数的 ...
随机推荐
- callback回调函数【转】
请给作者点赞--> 原文链接 什么是回调函数? 我们绕点远路来回答这个问题. 编程分为两类:系统编程(system programming)和应用编程(application programmi ...
- laravel5.2总结--邮件
laravel自带SwiftMailer库,集成了多种邮件API,支持多种邮件驱动方式,包括smtp.Mailgun.Maildrill.Amazon SES.mail和sendmail,Mailgu ...
- Python 拓展之详解深拷贝和浅拷贝
正式开始 首先我在这介绍两个新的小知识,要在下面用到.一个是函数 id() ,另一个是运算符 is.id() 函数就是返回对象的内存地址:is 是比较两个变量的对象引用是否指向同一个对象,在这里请不要 ...
- [19/02/23]ToolsShare 工具分享 VPNTethering Android (Root Required)
To be short, VPN Tethering is a quite useful tool when you want to share your private network with s ...
- Leetcode 594.最长和谐子序列
最长和谐子序列 和谐数组是指一个数组里元素的最大值和最小值之间的差别正好是1. 现在,给定一个整数数组,你需要在所有可能的子序列中找到最长的和谐子序列的长度. 示例 1: 输入: [1,3,2,2,5 ...
- gcc学习记录2——多输入文件
首先有两个.c文件:circle.c和circulararea.c. 分别对两个源文件生成目标文件,circle.o和circulararea.o. gcc -c circle.c circularr ...
- [译]从列表或字典创建Pandas的DataFrame对象
原文来源:http://pbpython.com/pandas-list-dict.html 介绍 每当我使用pandas进行分析时,我的第一个目标是使用众多可用选项中的一个将数据导入Pandas的D ...
- Zookeeper CreateMode
通过CreateMode 可以设置在zookeeper中创建节点的类型,节点类型共有4种: EPHEMERAL:临时节点 EPHEMERAL_SEQUENTIAL:有序的临时节点 PERSISTENT ...
- Nginx概述与安装
什么是Nginx 一款服务器软件,类似于Apache.Tomcat Nginx还是一款HTTP和反向代理服务器,同时也是一个代理邮件服务器.除了实现网站发布的功能外,还可以提供负载均衡. 所谓负载均衡 ...
- hdu6134[莫比乌斯反演] 2017多校8
/*hdu6134[莫比乌斯反演] 2017多校8*/ #include <bits/stdc++.h> using namespace std; typedef long long LL ...