結果

問題 No.277 根掘り葉掘り
ユーザー どららどらら
提出日時 2015-09-27 02:50:41
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,157 bytes
コンパイル時間 571 ms
コンパイル使用メモリ 77,132 KB
実行使用メモリ 14,288 KB
最終ジャッジ日時 2023-09-26 16:41:55
合計ジャッジ時間 4,471 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,364 KB
testcase_01 AC 3 ms
6,156 KB
testcase_02 WA -
testcase_03 AC 4 ms
6,132 KB
testcase_04 AC 4 ms
6,260 KB
testcase_05 AC 4 ms
6,156 KB
testcase_06 AC 4 ms
6,096 KB
testcase_07 AC 4 ms
6,372 KB
testcase_08 AC 4 ms
6,364 KB
testcase_09 AC 223 ms
14,288 KB
testcase_10 AC 189 ms
9,668 KB
testcase_11 AC 205 ms
9,612 KB
testcase_12 AC 217 ms
11,964 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <iostream>
#include <queue>
#include <list>
#include <stack>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;

#define INF 10000000

vector<int> G[100005];
int ret[100005];
int visited[100005];

int dfs(int x, int r){
  visited[x] = 1;
  ret[x] = min(ret[x], r);

  int mn = INF;
  rep(i,G[x].size()){
    if(visited[G[x][i]] > 0) continue;
    int res = dfs(G[x][i], r+1);
    mn = min(mn, res);
  }

  if(mn == INF){
    ret[x] = min(ret[x], 0);
    return 1;
  }
  
  ret[x] = min(ret[x], mn);
  return mn+1;
}

int main(){
  int N;
  cin >> N;
  rep(i,N-1){
    int x, y;
    cin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }

  rep(i,100005) ret[i] = INF;
  
  dfs(1,0);

  REP(i,1,N+1){
    cout << ret[i] << endl;
  }
  
  return 0;
}
0