結果

問題 No.277 根掘り葉掘り
ユーザー tnoda_tnoda_
提出日時 2015-09-08 17:57:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,805 ms / 3,000 ms
コード長 1,771 bytes
コンパイル時間 947 ms
コンパイル使用メモリ 91,256 KB
実行使用メモリ 22,628 KB
最終ジャッジ日時 2023-09-26 10:09:15
合計ジャッジ時間 12,709 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
18,376 KB
testcase_01 AC 7 ms
18,612 KB
testcase_02 AC 8 ms
18,396 KB
testcase_03 AC 8 ms
18,628 KB
testcase_04 AC 10 ms
18,448 KB
testcase_05 AC 8 ms
18,388 KB
testcase_06 AC 9 ms
18,424 KB
testcase_07 AC 9 ms
18,384 KB
testcase_08 AC 8 ms
18,388 KB
testcase_09 AC 71 ms
22,232 KB
testcase_10 AC 1,805 ms
22,628 KB
testcase_11 AC 870 ms
22,160 KB
testcase_12 AC 949 ms
22,056 KB
testcase_13 AC 914 ms
22,228 KB
testcase_14 AC 1,096 ms
22,260 KB
testcase_15 AC 866 ms
22,236 KB
testcase_16 AC 953 ms
22,120 KB
testcase_17 AC 1,000 ms
22,128 KB
testcase_18 AC 843 ms
22,084 KB
testcase_19 AC 958 ms
22,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <cmath>
#include <climits>
#include <iostream>
#include <iomanip>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <algorithm>
#include <numeric>
#include <functional>

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define ALL(a) (a).begin(),(a).end()

using namespace std;
typedef long long ll;

int N;
const int MAX_N = 500010;
vector<int> G[MAX_N];
queue<int> q;
int depth_0[MAX_N];
int depth_1[MAX_N];
bool leaves[MAX_N];
bool visited[MAX_N];

void bfs_0() {
  depth_0[1] = 0;
  visited[1] = true;
  q.push(1);
  while (!q.empty()) {
    int v = q.front(); q.pop();
    bool is_leaf = true;
    for (int u : G[v]) {
      if (visited[u]) continue;
      is_leaf = false;
      visited[u] = true;
      depth_0[u] = depth_0[v] + 1;
      q.push(u);
    }
    leaves[v] = is_leaf;
  }
}

void bfs_1(int s) {
  memset(visited, 0, sizeof(visited));
  depth_1[s] = 0;
  visited[s] = true;
  q.push(s);
  while (!q.empty()) {
    int v = q.front(); q.pop();
    if (depth_1[v] >= depth_0[v]) continue;
    depth_0[v] = depth_1[v];
    for (int u : G[v]) {
      if (visited[u]) continue;
      visited[u] = true;
      depth_1[u] = depth_1[v] + 1;
      q.push(u);
    }
  }
}


int main(int argc, char *argv[])
{
  cin.tie(0);
  ios::sync_with_stdio(false);
    
  cin >> N;
  REP(i,N-1) {
    int x, y;
    cin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  bfs_0();
  REP(i,MAX_N) if (leaves[i]) bfs_1(i);
  REP(i,N) cout << depth_0[i+1] << '\n';
  cout << flush;

  return 0;
}
0