結果

問題 No.277 根掘り葉掘り
ユーザー naimonon77naimonon77
提出日時 2016-02-20 20:38:39
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,596 bytes
コンパイル時間 1,558 ms
コンパイル使用メモリ 166,508 KB
実行使用メモリ 10,340 KB
最終ジャッジ日時 2023-10-23 18:43:01
合計ジャッジ時間 4,905 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,136 KB
testcase_01 AC 2 ms
6,136 KB
testcase_02 WA -
testcase_03 AC 3 ms
6,140 KB
testcase_04 AC 3 ms
6,144 KB
testcase_05 AC 3 ms
6,140 KB
testcase_06 AC 3 ms
6,140 KB
testcase_07 AC 3 ms
6,140 KB
testcase_08 AC 3 ms
6,140 KB
testcase_09 AC 187 ms
9,924 KB
testcase_10 AC 175 ms
10,340 KB
testcase_11 AC 186 ms
10,068 KB
testcase_12 AC 224 ms
9,924 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#ifdef __unix__
#include <bits/stdc++.h>
#else
#include "bits\stdc++.h"
#endif
#include <vector>
#include <set>
#include <string>
#include <queue>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define MAX 100005

int N;
vector<int> connect[MAX];
int depth[MAX] = {0,0};
int L[MAX];
int INF = 1 << 30;

void seek_depth() {
  int i;
  bool visited[MAX]={0};
  queue<int> qu;
  qu.push(1);
  visited[1] = true;
  while( not qu.empty() ) {
    int now = qu.front(); qu.pop();
    rep(i,connect[now].size()) {
      int next = connect[now][i];
      if( not visited[next] ) {
        visited[next] = true;
        depth[next] = depth[now]+1;
        qu.push(next);
      }
    }
  }
}

void seek_L(int leaf) {
  int i;
  queue<int> qu;
  L[leaf] = 0;
  qu.push(leaf);
  while( not qu.empty() ) {
    int now = qu.front(); qu.pop();
    rep(i,connect[now].size()) {
      int next = connect[now][i];
      if(L[next] > L[now]+1
        && depth[now] > depth[next]) {
        L[next] = L[now]+1;
        qu.push(next);
      }
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int i,j,k;
  cin >> N;
  rep(i,N+1) L[i] = INF;
  rep(i,N-1) {
    int x,y;
    cin >> x >> y;
    connect[x].push_back(y);
    connect[y].push_back(x);
  }
  seek_depth();
  for(i=1;i<N+1;i++) {
    if(connect[i].size() == 1) seek_L(i);
  }
  for(i=1;i<N+1;i++) {
    cout << min(depth[i],L[i]) << endl;
  }
  
  return 0;
}
0