树的重心即树上某结点,删除该结点后形成的森林中包含结点最多的树的结点数最少。

一个DFS就OK了。。

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 222222
struct Edge{
int u,v,next;
}edge[MAXN<<];
int NE,head[MAXN];
void addEdge(int u,int v){
edge[NE].u=u; edge[NE].v=v; edge[NE].next=head[u];
head[u]=NE++;
}
int n,size[MAXN],x,y;
void dfs(int u,int fa){
int cnt=,res=;
for(int i=head[u]; i!=-; i=edge[i].next){
int v=edge[i].v;
if(v==fa) continue;
dfs(v,u);
cnt+=size[v];
res=max(res,size[v]);
}
size[u]=cnt;
res=max(res,n-size[u]);
if(y>res) y=res,x=u;
else if(y==res && x>u) x=u;
}
int main(){
int t,a,b;
scanf("%d",&t);
while(t--){
NE=;
memset(head,-,sizeof(head));
scanf("%d",&n);
for(int i=; i<n; ++i){
scanf("%d%d",&a,&b);
addEdge(a,b); addEdge(b,a);
}
y=(<<);
dfs(,);
printf("%d %d\n",x,y);
}
return ;
}

POJ1655 Balancing Act(树的重心)的更多相关文章

  1. POJ1655 Balancing Act(树的重心)

    题目链接 Balancing Act 就是求一棵树的重心,然后统计答案. #include <bits/stdc++.h> using namespace std; #define REP ...

  2. poj-1655 Balancing Act(树的重心+树形dp)

    题目链接: Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11845   Accepted: 4 ...

  3. 『Balancing Act 树的重心』

    树的重心 我们先来认识一下树的重心. 树的重心也叫树的质心.找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡. 根据树的重心的定义,我们可 ...

  4. POJ 1655 Balancing Act 树的重心

    Balancing Act   Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. ...

  5. poj1655 Balancing Act 找树的重心

    http://poj.org/problem? id=1655 Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  6. POJ-1655 Balancing Act(树的重心)

    Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the t ...

  7. POJ1655 Balancing Act (树的重心)

    求树的重心的模板题,size[u]维护以u为根的子树大小,f[u]表示去掉u后的最大子树. 1 #include<cstdio> 2 #include<iostream> 3 ...

  8. poj1655 Balancing Act (dp? dfs?)

    Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14247   Accepted: 6026 De ...

  9. poj3107 求树的重心(&& poj1655 同样求树的重心)

    题目链接:http://poj.org/problem?id=3107 求树的重心,所谓树的重心就是:在无根树转换为有根树的过程中,去掉根节点之后,剩下的树的最大结点最小,该点即为重心. 剩下的数的 ...

随机推荐

  1. codebolocks 中文使用手册1.1

    Code::Blocks手册 使用篇 中文翻译版- 原手册下载:http://www.codeblocks.org/docs/manual_en.pdf 译者:JGood 译者言:工欲善其事,必先利其 ...

  2. hnust py road

    问题 C: Py Road 时间限制: 1 Sec  内存限制: 128 MB提交: 125  解决: 34[提交][状态][讨论版] 题目描述 Life is short,you need Pyth ...

  3. Day3 UI:7种常用控件、4种基本布局

    Android常用控件 TextView <TextView android:id="@+id/text_view" android:layout_width="m ...

  4. React01补充

    使用yarn安装脚手架 npm i -g yarn npm uninstall -g create-react-app yarn global add create-react-app create- ...

  5. MVC3使用Area解耦项目

    源代码 1.增加AreasChildRegistration类,类继承PortableAreaRegistration 2.增加引用MvcContrib 3.主项目中Area文件夹下增加Web.con ...

  6. P4397 [JLOI2014]聪明的燕姿

    P4397 [JLOI2014]聪明的燕姿 题目背景 阴天傍晚车窗外 未来有一个人在等待 向左向右向前看 爱要拐几个弯才来 我遇见谁会有怎样的对白 我等的人他在多远的未来 我听见风来自地铁和人海 我排 ...

  7. 微信 编码要UTF8

    <%@ WebHandler Language="C#" Class="Handler" %> using System; using System ...

  8. NS_AVAILABLE_IOS(6_0)

    http://www.cocoachina.com/bbs/read.php?tid=241951 一个简单的小问题,请诸位大侠帮助给看看 ,新手 ,勿拍砖       本帖属于CocoaChina会 ...

  9. java基础练习 18

    import java.util.Scanner; public class Eightheen { /*判断一个素数能被几个9整除*/ public static void main(String[ ...

  10. 如何使用python发送邮件

    使用python发送邮件,用的是SMTP协议. 因此在qq邮箱中,要设置开启SMTP服务 只要能开启一个就行 在我们执行程序的时候,会发现邮件被发送过来了 在python中还有一个更简单的第三方模块, ...