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 求树的重心,所谓树的重心就是:在无根树转换为有根树的过程中,去掉根节点之后,剩下的树的最大结点最小,该点即为重心. 剩下的数的 ...
随机推荐
- [spring]spring的bean自动装配机制
7.bean的自动装配 是spring满足bean依赖的一种方式 spring会在上下文中自动寻找,并自动给bean装配属性 spring的装配方式: (1)手动装配 在people类中依赖了cat和 ...
- Go语言基础一:环境配置与基础语法
配置开发环境 开始使用Go,首先需要一个完善的开发环境. 下载并安装Go 安装包的下载地址为:https://golang.org/dl/ go语言中文社区下载:https://studygolang ...
- 获取某个html元素相对于视窗的位置集合
getBoundingClientRect() getBoundingClientRect()获取元素位置,这个方法没有参数 getBoundingClientRect()用于获得页面中某个元素的左, ...
- mysql Insert强化
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name [(col_name,...)] VALUES ({e ...
- CentOS Docker安装 && docker 基础指令
1 # 直接从官网下载docker的安装命令包(docker已经很贴心将安装shell脚本帮我们准备好了) 2 curl -fsSL get.docker.com -o get-docker.sh 3 ...
- React报错之无法在未挂载的组件上执行React状态更新
正文从这开始~ 总览 为了解决"Warning: Can't perform a React state update on an unmounted component" ,可以 ...
- Redis 15 主从复制
参考源 https://www.bilibili.com/video/BV1S54y1R7SB?spm_id_from=333.999.0.0 版本 本文章基于 Redis 6.2.6 概述 主从复制 ...
- 理解 Flutter 的基础概念:Widget
Widget 的本意是组件的意思,熟悉 Web 应用开发的人在后期必定会接触到 Vue.React 等框架,这些框架都有一个核心的概念 -- 组件.组件的目的也很简单,那就是重复率用一段代码,并且能够 ...
- HEXO-admin安装和使用(汉化版)
hi,大家好,我是KINGWDY,众所周知我用的是hexo,写博文首先要在终端输入hexo n xxxxx,然后打开MWeb PRO开始写md,但是,这很麻烦,就在我一筹莫展之际,我看到了这篇博文-- ...
- 「雅礼集训 2017 Day7」跳蚤王国的宰相(树的重心)
题面 来源 「 雅 礼 集 训 2017 D a y 7 」 跳 蚤 王 国 的 宰 相 传 统 2000 m s 1024 M i B {\tt「雅礼集训 2017 Day7」跳蚤王国的 ...