1021. Deepest Root (25)
时间限制1500 ms
内存限制65536 kB
代码长度限制16000 B
判题程序Standard作者CHEN, Yue
Error: 2 componentsA graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<=10000) which is the number of nodes, and hence the nodes are numbered from 1 to N. Then N-1 lines follow, each describes an edge by given the two adjacent nodes' numbers.
Output Specification:
For each test case, print each of the deepest roots in a line. If such a root is not unique, print them in increasing order of their numbers. In case that the given graph is not a tree, print "Error: K components" where K is the number of connected components in the graph.
Sample Input 1:5 1 2 1 3 1 4 2 5Sample Output 1:3 4 5Sample Input 2:5 1 3 1 4 2 5 3 4Sample Output 2:
码了一个多小时,修正了3个小时。在两个DFS的if结束过程中出过错,STAR和id弄混,特殊条件 1 要考虑;这里用到了并查集,DFS,template 函数模版,malloc开空间和free释放,用两个一维数组表示的邻接表
时间 结果 得分 题目 语言 用时(ms) 内存(kB) 用户 7月17日 16:08 答案正确 25 1021 C++ (g++ 4.7.2) 823 692
datrilla
#include<iostream> using namespace std; struct Roads { int c1,c2; }; struct ModelMatrix { int cc; int FrontIndex; }; template<typename TyPE> void StarCr(TyPE *T_T,int sTar,int NUM,TyPE SeT) { for(;sTar<NUM;sTar++) { T_T[sTar]=SeT; } } void StarClear(int * Leaf,int N) { while(N) Leaf[N]=N--; Leaf[N]=N; } int LeafGmap(int*LastIndex,ModelMatrix*GLeaf,int c12,int c22,int Index) { GLeaf[Index].cc=c22; GLeaf[Index].FrontIndex=LastIndex[c12]; LastIndex[c12]=Index++; GLeaf[Index].cc=c12; GLeaf[Index].FrontIndex=LastIndex[c22]; LastIndex[c22]=Index++; return Index; } void InprintRoads(long int N,Roads*Linkc,int*LastIndex,ModelMatrix*GLeaf) { int i,Index; N--; for( i=0,Index=1;i<N;i++) { cin>>Linkc[i].c1>>Linkc[i].c2; Index=LeafGmap(LastIndex,GLeaf,Linkc[i].c1,Linkc[i].c2,Index); } } int Find_set(int*Leaf,int C) { if(C!=Leaf[C]) Leaf[C]=Find_set(Leaf,Leaf[C]); return Leaf[C]; } void Union(Roads*Linkc,int*Leaf,long int N) { long int i; int xx,yy; N--; for(i=0;i<N;i++) { xx=Find_set(Leaf,Linkc[i].c1); yy=Find_set(Leaf,Linkc[i].c2); if(xx!=yy) Leaf[xx]=yy; } } int judge(int*Leaf,int N) { int count=0; while(N) { if(Leaf[N]==N--)count++; } return count; } void FindMaxDFS(bool *Flag,int *Leaf, int STAR, int *LastIndex,ModelMatrix*GLeaf,int*FloorMaxNum,int nowmax,int N) { int id=STAR,index; Flag[STAR]=false; index=LastIndex[STAR]; while(index>0) { id=GLeaf[index].cc; if(true==Flag[id]){ FindMaxDFS(Flag,Leaf,id,LastIndex,GLeaf,FloorMaxNum,nowmax+1,N);} else if((*FloorMaxNum)<nowmax) { (*FloorMaxNum)=nowmax; StarCr(Leaf,0,N+1,0); Leaf[STAR]=1; } else if((*FloorMaxNum)==nowmax) { Leaf[STAR]=1; } index=GLeaf[index].FrontIndex; } Flag[STAR]=true; } void GetRestMaxDFS(bool*Flag,bool *Flag2, int STAR, int *LastIndex,ModelMatrix*GLeaf,int *FloorMaxNum,int nowmax,int N) { int id,index; Flag[STAR]=false; index=LastIndex[STAR]; while(index>0) { id=GLeaf[index].cc; if(Flag[id])GetRestMaxDFS(Flag,Flag2,id, LastIndex,GLeaf,FloorMaxNum,nowmax+1,N); else if((*FloorMaxNum)==nowmax) { Flag2[STAR]=true; } else if((*FloorMaxNum)<nowmax) { (*FloorMaxNum)=nowmax; StarCr(Flag2,0,N+1,false); Flag2[STAR]=true; } index=GLeaf[index].FrontIndex; } Flag[STAR]=true; } void dIsPlaY(int i,int N,bool*Flag2,int*Leaf) { for(;i<=N;i++) { if(Flag2[i]||1==Leaf[i]) cout<<i<<endl; } } int main() { int N,i; int FloorMaxNum; Roads *Linkc; int *Leaf; bool*Flag; bool*Flag2; int *LastIndex; ModelMatrix*GLeaf; cin>>N; if(N==1)cout<<"1"<<endl; else { Flag=(bool*)malloc((N+1)*sizeof(bool)); Flag2=(bool*)malloc((N+1)*sizeof(bool)); Leaf=(int*)malloc((N+1)*sizeof(int)); Linkc=(Roads *)malloc((N-1)*sizeof(Roads)); LastIndex=(int*)malloc((N+1)*sizeof(int)); GLeaf=(ModelMatrix *)malloc(2*N*sizeof(ModelMatrix)); StarCr(LastIndex,0,N+1,0); StarClear(Leaf,N); InprintRoads(N,Linkc,LastIndex,GLeaf); Union(Linkc,Leaf,N); i=judge(Leaf,N); if(i>1||2>N)cout<<"Error: "<<i<<" components"<<endl; else { StarCr(Leaf,0,N+1,0); StarCr(Flag,0,N+1,true); FloorMaxNum=0; FindMaxDFS(Flag,Leaf,1,LastIndex,GLeaf,&FloorMaxNum,0,N); StarCr(Flag2,0,N+1,false); FloorMaxNum=0; for(i=0;i<N;) { i++; if(1==Leaf[i]) { StarCr(Flag,0,N+1,true); GetRestMaxDFS(Flag,Flag2,i,LastIndex,GLeaf,&FloorMaxNum,0,N); } } dIsPlaY(1,N,Flag2,Leaf); } free(GLeaf); free(LastIndex); free(Linkc); free(Leaf); free(Flag2); free(Flag); } return 0; }
1021. Deepest Root (25)
最新推荐文章于 2022-03-06 17:13:01 发布