結果

問題 No.277 根掘り葉掘り
ユーザー tnakao0123tnakao0123
提出日時 2016-03-31 20:13:06
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,287 bytes
コンパイル時間 668 ms
コンパイル使用メモリ 87,348 KB
実行使用メモリ 17,536 KB
最終ジャッジ日時 2024-04-10 06:54:10
合計ジャッジ時間 3,382 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,812 KB
testcase_01 AC 3 ms
6,816 KB
testcase_02 WA -
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 3 ms
6,944 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 107 ms
17,536 KB
testcase_10 AC 75 ms
10,112 KB
testcase_11 AC 97 ms
9,728 KB
testcase_12 AC 104 ms
13,756 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 277.cc: No.277 根掘り葉掘り - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int INF = 1 << 30;

/* typedef */

typedef vector<int> vi;

/* global variables */

vi nbrs[MAX_N];
int rs[MAX_N], ls[MAX_N];

/* subroutines */

int rec(int u, int prt, int r) {
  int minl = INF;
  vi &nbru = nbrs[u];
  for (vi::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
    int &v = *vit;
    if (v != prt) {
      int l = rec(v, u, r + 1) + 1;
      if (minl > l) minl = l;
    }
  }

  rs[u] = r;
  ls[u] = (minl >= INF) ? 0 : minl;

  return ls[u];
}

/* main */

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    nbrs[x].push_back(y);
    nbrs[y].push_back(x);
  }

  rec(0, -1, 0);

  for (int i = 0; i < n; i++) {
    //printf("rs[%d]=%d, ls[%d]=%d\n", i, rs[i], i, ls[i]);
    printf("%d\n", min(rs[i], ls[i]));
  }
  return 0;
}
0