結果

問題 No.277 根掘り葉掘り
ユーザー koba-e964koba-e964
提出日時 2015-09-09 20:46:00
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,014 bytes
コンパイル時間 2,523 ms
コンパイル使用メモリ 70,172 KB
実行使用メモリ 17,252 KB
最終ジャッジ日時 2023-09-26 10:16:32
合計ジャッジ時間 5,207 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,836 KB
testcase_01 AC 3 ms
5,672 KB
testcase_02 WA -
testcase_03 AC 4 ms
5,600 KB
testcase_04 AC 4 ms
5,824 KB
testcase_05 AC 3 ms
5,652 KB
testcase_06 AC 4 ms
5,844 KB
testcase_07 AC 4 ms
5,684 KB
testcase_08 AC 3 ms
5,824 KB
testcase_09 AC 222 ms
17,252 KB
testcase_10 AC 189 ms
9,520 KB
testcase_11 AC 216 ms
9,580 KB
testcase_12 AC 223 ms
13,440 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 <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef pair<int, int> PI;
const double EPS=1e-9;

const int N = 100100;

VI edges[N];

int rd[N], ld[N];


void dfs(int v, int par = -1) {
  if (par == -1) {
    rd[v] = 0;
  } else {
    rd[v] = rd[par] + 1;
  }
  int mi = N * 2;
  REP(i, 0, edges[v].size()) {
    int w = edges[v][i];
    if (w == par) { continue; }
    dfs(w, v);
    mi = min(mi, ld[w] + 1);
  }
  if (mi == N * 2) {
    mi = 0;
  }
  ld[v] = mi;
}

int main(void){
  int n;
  cin >> n;
  REP(i, 0, n - 1) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    edges[x].push_back(y);
    edges[y].push_back(x);
  }
  REP(i, 0, n) {
    rd[i] = -1;
  }
  dfs(0);
  REP(i, 0, n) {
    cout << min(rd[i], ld[i]) << endl;
  }
}
0