1021. Deepest Root (25)

102 篇文章 0 订阅
13 篇文章 0 订阅
  

1021. Deepest Root (25)

时间限制
1500 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

A 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 5
Sample Output 1:
3
4
5
Sample Input 2:
5
1 3
1 4
2 5
3 4
Sample Output 2:
Error: 2 components


码了一个多小时,修正了3个小时。在两个DFS的if结束过程中出过错,STAR和id弄混,特殊条件 1 要考虑;
这里用到了  
并查集,DFS,template 函数模版,malloc开空间和free释放,用两个一维数组表示的邻接表

   
   
时间结果得分题目语言用时(ms)内存(kB)用户
7月17日 16:08答案正确251021C++ (g++ 4.7.2)823692
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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值