結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,816 KB
testcase_01 AC 4 ms
6,944 KB
testcase_02 AC 4 ms
6,940 KB
testcase_03 AC 4 ms
6,940 KB
testcase_04 AC 3 ms
6,944 KB
testcase_05 AC 3 ms
6,944 KB
testcase_06 AC 4 ms
6,940 KB
testcase_07 AC 4 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 72 ms
20,352 KB
testcase_10 AC 48 ms
9,636 KB
testcase_11 AC 61 ms
9,472 KB
testcase_12 AC 67 ms
15,104 KB
testcase_13 AC 68 ms
9,472 KB
testcase_14 AC 59 ms
10,140 KB
testcase_15 AC 60 ms
10,692 KB
testcase_16 AC 63 ms
9,984 KB
testcase_17 AC 61 ms
9,984 KB
testcase_18 AC 60 ms
9,856 KB
testcase_19 AC 57 ms
9,728 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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, tmpdist+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