結果

問題 No.277 根掘り葉掘り
ユーザー beetbeet
提出日時 2018-12-04 19:45:46
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 227 ms / 3,000 ms
コード長 783 bytes
コンパイル時間 2,490 ms
コンパイル使用メモリ 207,100 KB
実行使用メモリ 9,740 KB
最終ジャッジ日時 2023-09-22 04:52:33
合計ジャッジ時間 6,510 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 222 ms
8,732 KB
testcase_10 AC 196 ms
9,740 KB
testcase_11 AC 219 ms
9,176 KB
testcase_12 AC 216 ms
8,928 KB
testcase_13 AC 227 ms
9,356 KB
testcase_14 AC 217 ms
9,300 KB
testcase_15 AC 217 ms
9,044 KB
testcase_16 AC 218 ms
9,252 KB
testcase_17 AC 216 ms
9,168 KB
testcase_18 AC 216 ms
9,164 KB
testcase_19 AC 215 ms
9,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

//INSERT ABOVE HERE
signed main(){
  int n;
  cin>>n;
  vector<vector<int> > G(n);
  for(int i=1;i<n;i++){
    int x,y;
    cin>>x>>y;
    x--;y--;
    G[x].emplace_back(y);
    G[y].emplace_back(x);
  }
  
  vector<int> dp(n,n);
  queue<int> q;
  for(int i=0;i<n;i++){
    if(!i||G[i].size()==1u){      
      dp[i]=0;
      q.emplace(i);
    }
  }
  
  while(!q.empty()){
    int v=q.front();q.pop();
    for(int u:G[v]){
      if(dp[u]<=dp[v]+1) continue;
      dp[u]=dp[v]+1;
      q.emplace(u);
    }
  }
  
  for(int x:dp) cout<<x<<endl;
  return 0;
}
0