POJ.1655 Balancing Act POJ.3107 Godfather(树的重心)
关于树的重心:百度百科
有关博客:http://blog.csdn.net/acdreamers/article/details/16905653
1.Balancing Act
题目大意:
有t组数据。每组数据给出n个点和n-1条边,构成一棵树,求该树的重心及删掉该点后形成的每棵子树的节点数。
代码:
1 #include<cctype> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int N=20005; 7 8 int n,cnt,H[N<<1],Ans,size,son[N]; 9 bool vis[N]; 10 struct Edge 11 { 12 int to,nxt; 13 }e[N<<1]; 14 15 void read(int &now) 16 { 17 now=0;char c=getchar(); 18 while(!isdigit(c))c=getchar(); 19 while(isdigit(c))now=(now<<3)+(now<<1)+c-'0',c=getchar(); 20 } 21 22 void AddEdge(int u,int v) 23 { 24 e[++cnt].to = v; 25 e[cnt].nxt = H[u]; 26 H[u] = cnt; 27 } 28 29 void Init() 30 { 31 Ans=size=0x7fffffff; 32 for(int i=1;i<=n;++i) 33 vis[i]=son[i]=H[i]=0; 34 cnt=0; 35 read(n); 36 int a,b; 37 for(int i=1;i<n;++i) 38 read(a),read(b),AddEdge(a,b),AddEdge(b,a); 39 } 40 41 void DFS(int cur) 42 { 43 vis[cur]=1; 44 son[cur]=0; 45 int tmp=0; 46 for(int i=H[cur];i;i=e[i].nxt) 47 { 48 int to=e[i].to; 49 if(!vis[to]) 50 { 51 DFS(to); 52 son[cur]+=son[to]+1; 53 tmp=max(tmp,son[to]+1); 54 } 55 } 56 tmp=max(tmp,n-son[cur]-1); 57 if(size>tmp ||size==tmp&&Ans>cur) 58 { 59 Ans=cur; 60 size=tmp; 61 } 62 } 63 64 int main() 65 { 66 int t; 67 read(t); 68 while(t--) 69 { 70 Init(); 71 DFS(1); 72 printf("%d %d\n",Ans,size); 73 } 74 return 0; 75 }
2.Godfather
题目大意:
按升序输出该树的重心。
代码:
1 #include<cstdio> 2 #include<cctype> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int N=50005; 7 8 int n,cnt,tot,size,H[N<<1],Ans[N],son[N]; 9 bool vis[N]; 10 struct Edge 11 { 12 int to,nxt; 13 }e[N<<1]; 14 15 void read(int &now) 16 { 17 now=0;char c=getchar(); 18 while(!isdigit(c))c=getchar(); 19 while(isdigit(c))now=(now<<3)+(now<<1)+c-'0',c=getchar(); 20 } 21 22 void AddEdge(int u,int v) 23 { 24 e[++cnt].to = v; 25 e[cnt].nxt = H[u]; 26 H[u] = cnt; 27 } 28 29 void Init() 30 { 31 memset(H,0,sizeof H); 32 memset(vis,0,sizeof vis); 33 memset(Ans,0x3f,sizeof Ans); 34 size=0x7fffffff; 35 cnt=tot=0; 36 read(n); 37 int x,y; 38 for(int i=1;i<n;++i) 39 read(x),read(y),AddEdge(x,y),AddEdge(y,x); 40 } 41 42 void DFS(int cur) 43 { 44 vis[cur]=1; 45 son[cur]=0; 46 int tmp=0; 47 for(int i=H[cur];i;i=e[i].nxt) 48 { 49 int to=e[i].to; 50 if(!vis[to]) 51 { 52 DFS(to); 53 son[cur]+=son[to]+1; 54 tmp=max(tmp,son[to]+1); 55 } 56 } 57 tmp=max(tmp,n-son[cur]-1); 58 if(size>tmp) 59 { 60 size=tmp; 61 tot=1; 62 Ans[tot]=cur; 63 } 64 else if(size==tmp) 65 { 66 Ans[++tot]=cur; 67 } 68 } 69 70 int main() 71 { 72 Init(); 73 DFS(1); 74 sort(Ans+1,Ans+1+tot); 75 for(int i=1;i<=tot;++i) 76 printf("%d ",Ans[i]); 77 return 0; 78 }
------------------------------------------------------------------------------------------------------------------------
很久以前的奇怪但现在依旧成立的签名
attack is our red sun $$\color{red}{\boxed{\color{red}{attack\ is\ our\ red\ sun}}}$$ ------------------------------------------------------------------------------------------------------------------------
很久以前的奇怪但现在依旧成立的签名
attack is our red sun $$\color{red}{\boxed{\color{red}{attack\ is\ our\ red\ sun}}}$$ ------------------------------------------------------------------------------------------------------------------------