COJ 2003 选根 (树的重心)

我们可以用树形DP在线性复杂度内搞定重心。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<cstring>
 7 #define PAU putchar(‘ ‘)
 8 #define ENT putchar(‘\n‘)
 9 using namespace std;
10 const int maxn=100000+10;
11 struct tedge{int x,y,next;}adj[maxn<<1];int ms=0,fch[maxn];
12 void addedge(int u,int v){
13     adj[++ms]=(tedge){u,v,fch[u]};fch[u]=ms;
14     adj[++ms]=(tedge){v,u,fch[v]};fch[v]=ms;
15     return;
16 }
17 int s[maxn],f[maxn],cg,n;
18 void findcg(int u,int fa){
19     s[u]=1;int mxs=0;
20     for(int i=fch[u];i;i=adj[i].next){
21         int v=adj[i].y;if(v!=fa){
22             findcg(v,u);
23             s[u]+=s[v];
24             mxs=max(mxs,s[v]);
25         }
26     } f[u]=max(n-s[u],mxs);
27     if(f[u]<f[cg]) cg=u;
28     else if(f[u]==f[cg]) cg=min(u,cg);return;//编号小的
29 }
30 inline int read(){
31     int x=0,sig=1;char ch=getchar();
32     while(!isdigit(ch)){if(ch==‘-‘)sig=-1;ch=getchar();}
33     while(isdigit(ch))x=10*x+ch-‘0‘,ch=getchar();
34     return x*=sig;
35 }
36 inline void write(int x){
37     if(x==0){putchar(‘0‘);return;}if(x<0)putchar(‘-‘),x=-x;
38     int len=0,buf[15];while(x)buf[len++]=x%10,x/=10;
39     for(int i=len-1;i>=0;i--)putchar(buf[i]+‘0‘);return;
40 }
41 void init(){
42     n=read();f[cg=0]=n;
43     for(int i=1;i<n;i++) addedge(read(),read());
44     findcg(1,0);
45     write(cg);
46     return;
47 }
48 void work(){
49     return;
50 }
51 void print(){
52     return;
53 }
54 int main(){init();work();print();return 0;}

搜索

复制

时间: 2024-10-26 19:03:16

COJ 2003 选根 (树的重心)的相关文章

poj1655 Balancing Act 求树的重心

http://poj.org/problem?id=1655 Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9072   Accepted: 3765 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a fo

POJ 1655 Balancing Act 树的重心 基础题

Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10347   Accepted: 4285 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or m

POJ 1655 Balancing Act(求树的重心--树形DP)

题意:求树的重心的编号以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的. 思路:随便选一个点把无根图转化成有根图,dfs一遍即可dp出答案 //1348K 125MS C++ 1127B #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<vector> using namespace std; int

【POJ 1655】Balancing Act 【树的重心】

Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9241   Accepted: 3846 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or mo

POJ 1655 Balancing Act (树形dp 树的重心)

Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10596   Accepted: 4398 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or m

poj 1655 树的重心

Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13178   Accepted: 5565 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or m

树分治基础模板以及树的重心(poj1741 tree)

好久没有更新博文了,这里更新一发~~ Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist(u,v)=The min distance between node u and v. Give an integer k,for every pair (u,v) of vertices is called valid if and only if dist(u,v

codeforces 685B Kay and Snowflake 树的重心

分析:就是找到以每个节点为根节点的树的重心 树的重心可以看这三篇文章: 1:http://wenku.baidu.com/link?url=yc-3QD55hbCaRYEGsF2fPpXYg-iO63WtCFbg4RXHjERwk8piK3dgeKKvUBprOW8hJ7aN7h4ZC09QE9x6hYV3lD7bEvyOv_l1E-ucxjHJzqi 2:http://fanhq666.blog.163.com/blog/static/81943426201172472943638/ 3:ht

POJ 1655 Balancing Act (树的重心,常规)

题意:求树的重心,若有多个重心,则输出编号较小者,及其子树中节点最多的数量. 思路: 树的重心:指的是一个点v,在删除点v后,其子树的节点数分别为:u1,u2....,设max(v)为其中的最大值,点v的max(v)是所有点里面最小的,称v为树的重心. 如何求任一重心?按树形来看,max(v)可以由其父亲贡献,也可以由其任一孩子贡献.孩子比较好解决,不就是深搜一遍,然后回溯时统计下数量就行了?而父亲的怎么办?可以知道,点v到其父亲这一叉就是n-sum(v)了,sum(v)指的是以v为根的子树的节