#20303: 請問這個程式該怎麼改才會ac


superuserjoy@gmail.com (summeruserjoy)

學校 : 國立中興大學
編號 : 98294
來源 : [49.217.239.47]
最後登入時間 :
2022-04-05 21:11:31
b967. 4. 血緣關係 -- 2016年3月apcs | From: [180.204.210.235] | 發表日期 : 2019-12-26 23:27

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5. struct node{
  6.   int parent=-1;
  7.   vector<int> child;
  8.   int height[2];
  9.  
  10. };
  11. vector<node> no;
  12. int re(int num){
  13.   if(no[num].child.size()==0){
  14.     no[num].height[0]=0;
  15.     return(0);
  16.   }
  17.   else{
  18.     int maxre=0,temp3=0;
  19.     for(int i = 0;i < no[num].child.size();i++){
  20.       int hei,temp2;
  21.       temp2=re(no[num].child[i]);
  22.      
  23.       hei=no[no[num].child[i]].height[0]1;
  24.       if(hei>no[num].height[0]){
  25.         swap(no[num].height[0],no[num].height[1]);
  26.         no[num].height[0]=hei;
  27.  
  28.       }
  29.       else if(hei>no[num].height[1]){
  30.         no[num].height[1]=hei;
  31.       }
  32.       if(temp2>hei){
  33.         maxre=temp2;
  34.       }
  35.        
  36.     }
  37.     temp3=no[num].height[0]no[num].height[1];
  38.     if(maxre>temp3){
  39.       return(maxre);
  40.     }
  41.     else{
  42.       return(temp3);
  43.     }
  44.   }
  45. }
  46. int main(){
  47.   cin.tie(0);
  48.   int N;
  49.   while(cin>>N){
  50.     no.resize(0);
  51.     no.resize(N);
  52.  
  53.     for(int i = 0;i<N-1;i++){
  54.       int big,small;
  55.       cin>>big>>small;
  56.       no[big].child.push_back(small);
  57.       no[small].parent=big;      
  58.     }
  59.     int root=0;
  60.     while(no[root].parent!=-1){
  61.       root=no[root].parent;
  62.     }
  63.     if(root==N)
  64.       root-=1;
  65.     cout<<re(root)<<"\n";
  66.   }
  67. }
  68. 本來第18行沒有初始化,會跑奇怪的數字,但初始化後測資2、3都有錯
  69. 超時的部分不知道有沒有解
 
#20304: Re:請問這個程式該怎麼改才會ac


superuserjoy@gmail.com (summeruserjoy)

學校 : 國立中興大學
編號 : 98294
來源 : [49.217.239.47]
最後登入時間 :
2022-04-05 21:11:31
b967. 4. 血緣關係 -- 2016年3月apcs | From: [180.204.210.235] | 發表日期 : 2019-12-26 23:30

  1. #include
  2. #include
  3. #include
  4. using namespace std;
  5. struct node{
  6.   int parent=-1;
  7.   vector<int> child;
  8.   int height[2];
  9.  
  10. };
  11. vector<node> no;
  12. int re(int num){
  13.   if(no[num].child.size()==0){
  14.     no[num].height[0]=0;
  15.     return(0);
  16.   }
  17.   else{
  18.     int maxre=0,temp3=0;
  19.     for(int i = 0;i < no[num].child.size();i++){
  20.       int hei,temp2;
  21.       temp2=re(no[num].child[i]);
  22.      
  23.       hei=no[no[num].child[i]].height[0]1;
  24.       if(hei>no[num].height[0]){
  25.         swap(no[num].height[0],no[num].height[1]);
  26.         no[num].height[0]=hei;
  27.  
  28.       }
  29.       else if(hei>no[num].height[1]){
  30.         no[num].height[1]=hei;
  31.       }
  32.       if(temp2>hei){
  33.         maxre=temp2;
  34.       }
  35.        
  36.     }
  37.     temp3=no[num].height[0]no[num].height[1];
  38.     if(maxre>temp3){
  39.       return(maxre);
  40.     }
  41.     else{
  42.       return(temp3);
  43.     }
  44.   }
  45. }
  46. int main(){
  47.   cin.tie(0);
  48.   int N;
  49.   while(cin>>N){
  50.     no.resize(0);
  51.     no.resize(N);
  52.  
  53.     for(int i = 0;i<N-1;i++){
  54.       int big,small;
  55.       cin>>big>>small;
  56.       no[big].child.push_back(small);
  57.       no[small].parent=big;      
  58.     }
  59.     int root=0;
  60.     while(no[root].parent!=-1){
  61.       root=no[root].parent;
  62.     }
  63.     if(root==N)
  64.       root-=1;
  65.     cout<<re(root)<<"\n";
  66.   }
  67. }
  68. 本來第18行沒有初始化,會跑奇怪的數字,但初始化後測資2、3都有錯
  69. 超時的部分不知道有沒有解


附註:跑奇怪的數字之中有幾次會跑到1、2、3都過

超時是第四部分

 
ZeroJudge Forum