結果

問題 No.277 根掘り葉掘り
ユーザー yaoshimaxyaoshimax
提出日時 2016-05-13 12:30:49
言語 C++11
(gcc 8.5.0)
結果
WA  
実行時間 -
コード長 1,219 bytes
コンパイル時間 648 ms
コンパイル使用メモリ 59,428 KB
実行使用メモリ 17,192 KB
最終ジャッジ日時 2023-07-28 06:00:05
合計ジャッジ時間 2,572 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,252 KB
testcase_01 AC 4 ms
6,180 KB
testcase_02 WA -
testcase_03 AC 4 ms
6,280 KB
testcase_04 AC 4 ms
6,276 KB
testcase_05 AC 4 ms
6,340 KB
testcase_06 AC 3 ms
6,276 KB
testcase_07 AC 4 ms
6,392 KB
testcase_08 AC 4 ms
6,252 KB
testcase_09 AC 67 ms
17,192 KB
testcase_10 AC 45 ms
9,520 KB
testcase_11 AC 58 ms
9,364 KB
testcase_12 AC 66 ms
13,324 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
vector<int> edge[100001];
int dist[100001];

void update(int cur, int par, int d){
   if( dist[cur] <= d ) return;
   dist[cur]=d;
   for( int ei=0; ei < (int) edge[cur].size(); ei++){
      int nxt = edge[cur][ei];
      if( nxt==par ) continue;
      update(nxt, cur, d+1);
   }

}

int rec(int cur, int par, int d){
   dist[cur]=d;
   int tmpdist = 100001;
   for( int ei=0; ei < (int) edge[cur].size(); ei++){
      int nxt = edge[cur][ei];
      if( nxt==par ) continue;
      tmpdist = min(tmpdist,rec(nxt, cur, d+1));
   }
   if(tmpdist==100001) tmpdist=0;
   if( tmpdist < d ){
      for( int ei=0; ei < (int) edge[cur].size(); ei++){
         int nxt = edge[cur][ei];
         if( nxt==par ) continue;
         update(nxt, cur, d+1);
      }
   }
   dist[cur]=min(dist[cur],tmpdist);
   return tmpdist+1;
}

int main(){
   int N;
   scanf("%d",&N);
   for( int i = 0 ; i < N-1; i++ ){
      int a,b;
      scanf("%d%d",&a,&b);
      a--;b--;
      edge[a].push_back(b);
      edge[b].push_back(a);
   }
   for( int i=0;i<100001; i++) dist[i]=100001;
   rec(0,0,0);
   for( int i=0; i <N; i++ ) printf("%d\n",dist[i]);
   return 0;
}
0