結果

問題 No.277 根掘り葉掘り
ユーザー yaoshimaxyaoshimax
提出日時 2016-05-13 12:30:49
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,219 bytes
コンパイル時間 667 ms
コンパイル使用メモリ 61,288 KB
実行使用メモリ 20,352 KB
最終ジャッジ日時 2024-04-15 15:28:49
合計ジャッジ時間 2,271 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,816 KB
testcase_01 AC 4 ms
6,940 KB
testcase_02 WA -
testcase_03 AC 4 ms
6,944 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 4 ms
6,940 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 4 ms
6,940 KB
testcase_08 AC 4 ms
6,940 KB
testcase_09 AC 74 ms
20,352 KB
testcase_10 AC 51 ms
9,432 KB
testcase_11 AC 65 ms
9,472 KB
testcase_12 AC 72 ms
14,976 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:41:9: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   41 |    scanf("%d",&N);
      |    ~~~~~^~~~~~~~~
main.cpp:44:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   44 |       scanf("%d%d",&a,&b);
      |       ~~~~~^~~~~~~~~~~~~~

ソースコード

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