返回列表 發帖

[測驗紀錄] 2025/03/29

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int n,root;
  4. int res,finalMaxChildDis;
  5. struct Node
  6. {
  7.     int parent=-1,h=0;
  8.     int maxChildDis=-1,max=-1,sec=-1;
  9.     vector<int> child;
  10. };
  11. int main()
  12. {
  13.     cin.tie(0);
  14.     cin.sync_with_stdio(0);

  15.     while(cin>>n)
  16.     {
  17.         finalMaxChildDis=-1;
  18.         Node node[n];
  19.         for(int i=0;i<n-1;i++)
  20.         {
  21.             int p,c;
  22.             cin>>p>>c;
  23.             node[c].parent=p;
  24.             node[p].child.push_back(c);
  25.         }
  26.         for(int i=0;i<n;i++)
  27.         {
  28.             if(node[i].parent==-1)
  29.                 root=i;
  30.             if(node[i].child.size()==0)
  31.             {
  32.                 int h=0;
  33.                 int parent=node[i].parent;
  34.                 while(parent!=-1)
  35.                 {
  36.                     h++;
  37.                     if(h>node[parent].h)
  38.                         node[parent].h=h;
  39.                     else
  40.                         break;
  41.                     parent=node[parent].parent;
  42.                 }
  43.             }
  44.             for(int i=0;i<n;i++)
  45.             {
  46.                 if(node[i].child.size()>=2)
  47.                 {
  48.                     for(int j=0;j<node[i].child.size();j++)
  49.                     {
  50.                         int childH=node[node[i].child[j]].h;
  51.                         if(childH>node[i].max)
  52.                         {
  53.                             node[i].sec=node[i].max;
  54.                             node[i].max=childH;
  55.                         }
  56.                         else if(childH>node[i].sec)
  57.                             node[i].sec=childH;
  58.                     }
  59.                     node[i].maxChildDis=node[i].max+node[i].sec+2;
  60.                     if(node[i].maxChildDis>finalMaxChildDis)
  61.                         finalMaxChildDis=node[i].maxChildDis;
  62.                 }
  63.             }
  64.         }
  65.         if(node[root].h>finalMaxChildDis)
  66.             res=node[root].h;
  67.         else
  68.             res=finalMaxChildDis;
  69.         cout<<res<<endl;
  70.     }
  71.     return 0;
  72. }
複製代碼

TOP

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int n;//人數
  4. int root;//根節點
  5. int res;
  6. int finalMaxChildDis;
  7. struct Node
  8. {
  9.     int parent=-1,h=0;
  10.     int maxChildDis=0,max=-1,sec=-1;
  11.     vector<int> child;
  12. };
  13. int main()
  14. {
  15.         cin>>n;
  16.         int finalMaxChildDis=-1;
  17.         Node node[n];//node資料庫_共n人
  18.         for(int i=0;i<n-1;i++)//n個人有n-1個連結
  19.         {
  20.             int p;//父節點
  21.             int c;//子節點
  22.             cin>>p>>c;
  23.             node[c].parent=p;//c屬於b->p屬於node[c].parent
  24.             node[p].child.push_back(c);//一個父節點可以同時有很多子節點->用vector存
  25.         }//建立關係

  26.         for(int i=0;i<n;i++)//n次
  27.         {
  28.             if(node[i].parent==-1)//該節點沒有設定父節點->最高->根節點
  29.             {
  30.                 root=i;
  31.             }
  32.             if(node[i].child.size()==0)//確認該節點的子節點數量:為0->最底部
  33.             {
  34.                 int h=0;
  35.                 int parent=node[i].parent;//額外定義 不影響node資料
  36.                 while(parent!=-1)//有父節點
  37.                 {
  38.                     h++;
  39.                     if(h>node[parent].h)
  40.                     {
  41.                         node[parent].h=h;//找最深
  42.                     }
  43.                     else
  44.                     {
  45.                         break;
  46.                     }
  47.                     parent=node[parent].parent;//繼續往上找
  48.                 }
  49.             }
  50.         }
  51.         for(int i=0;i<n;i++)//n次
  52.         {
  53.             if(node[i].child.size()>=2)//子節點數量>=2
  54.             {
  55.                 for(int j=0;j<node[i].child.size();j++)//重複'子節點數量'次
  56.                 {
  57.                     int childH=node[node[i].child[j]].h;//取出子節點的深度
  58.                     if(childH>node[i].max)//找最深
  59.                     {
  60.                         node[i].sec=node[i].max;//找第二深(可能介於兩個最大之間)
  61.                         node[i].max=childH;
  62.                     }
  63.                     else if(childH>node[i].sec)//介於兩個最大之間->更新第二大
  64.                     {
  65.                         node[i].sec=childH;
  66.                     }
  67.                 }
  68.                 node[i].maxChildDis=node[i].max+node[i].sec+2;//計算總距離
  69.                 if(node[i].maxChildDis>finalMaxChildDis)//比較並更新
  70.                 {
  71.                     finalMaxChildDis=node[i].maxChildDis;
  72.                 }
  73.             }
  74.         }
  75.         if(node[root].h>finalMaxChildDis)//根節點距離最遠 不用找另外的子節點距離and+2
  76.         {
  77.             res=node[root].h;
  78.         }
  79.         else
  80.         {
  81.             res=finalMaxChildDis;
  82.         }
  83.     cout<<res;
  84. }
複製代碼

TOP

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int root,res, finalmaxchilddis;
  4. int n;
  5. struct Node
  6. {
  7.    int h=0,maxchilddis=-1,parent=-1,max=-1,sec=-1;
  8.    vector<int> child;
  9. };
  10. int main()
  11. {
  12.     while(cin>>n)
  13.     {
  14.         finalmaxchilddis=-1;
  15.         Node node[n];
  16.         for(int i=0;i<n-1;i++)
  17.         {
  18.             int p,c;
  19.             cin>>p>>c;
  20.             node[c].parent=p;
  21.             node[p].child.push_back(c);
  22.         }
  23.         for(int i=0;i<n;i++)
  24.         {
  25.             if(node[i].parent==-1)
  26.             {
  27.                 root=i;
  28.             }
  29.             if(node[i].child.size()==0)
  30.             {
  31.                 int h=0;
  32.                 int parent=node[i].parent;
  33.                 while(parent!=-1)
  34.                 {
  35.                   h++;
  36.                   if(h>node[parent].h)
  37.                     node[parent].h=h;
  38.                   else
  39.                     break;
  40.                     parent=node[parent].parent;
  41.                 }
  42.             }
  43.         }

  44.         for(int i=0;i<n;i++)
  45.         {
  46.              if(node[i].child.size()>=2)
  47.             {

  48.                 for(int j=0;j<node[i].child.size();j++)
  49.                 {
  50.                   int childh=node[node[i].child[j]].h;
  51.                   
  52.                   
  53.                   if(childh>node[i].max)
  54.                   {
  55.                       node[i].sec=node[i].max;
  56.                       node[i].max=childh;
  57.                   }
  58.                   else if(childh>node[i].sec)
  59.                       node[i].sec=childh;
  60.                 }
  61.                 node[i].maxchilddis=node[i].max+node[i].sec+2;
  62.                 if(node[i].maxchilddis>finalmaxchilddis)
  63.                 {
  64.                     finalmaxchilddis=node[i].maxchilddis;
  65.                 }
  66.             }
  67.         }

  68.         if(node[root].h>finalmaxchilddis)
  69.         {
  70.              res=node[root].h;
  71.         }else
  72.         {
  73.              res=finalmaxchilddis;
  74.         }
  75.                         
  76.     cout<<res<<endl;

  77.     }

  78.     return 0;
  79. }
複製代碼

TOP

返回列表