結果

問題 No.277 根掘り葉掘り
ユーザー alpha_virginisalpha_virginis
提出日時 2015-09-04 22:39:53
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 252 ms / 3,000 ms
コード長 977 bytes
コンパイル時間 1,716 ms
コンパイル使用メモリ 146,488 KB
実行使用メモリ 12,632 KB
最終ジャッジ日時 2023-09-26 05:49:17
合計ジャッジ時間 5,822 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,008 KB
testcase_01 AC 4 ms
6,024 KB
testcase_02 AC 3 ms
6,108 KB
testcase_03 AC 4 ms
5,944 KB
testcase_04 AC 4 ms
6,048 KB
testcase_05 AC 4 ms
6,052 KB
testcase_06 AC 4 ms
5,944 KB
testcase_07 AC 4 ms
5,948 KB
testcase_08 AC 4 ms
5,988 KB
testcase_09 AC 225 ms
12,632 KB
testcase_10 AC 193 ms
9,512 KB
testcase_11 AC 215 ms
9,604 KB
testcase_12 AC 252 ms
11,016 KB
testcase_13 AC 250 ms
9,688 KB
testcase_14 AC 242 ms
9,788 KB
testcase_15 AC 249 ms
9,836 KB
testcase_16 AC 250 ms
9,660 KB
testcase_17 AC 245 ms
9,736 KB
testcase_18 AC 247 ms
9,672 KB
testcase_19 AC 246 ms
9,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <cstdint>
#include <sys/time.h>

typedef std::int_fast32_t  s32;
typedef std::uint_fast32_t u32;
typedef std::int_fast64_t  s64;
typedef std::uint_fast64_t u64;

const unsigned long mod = 1000000007;
const double EPS = 0.00000001;
const int INF = (1 << 30);

int ans[112345];

std::vector<int> connect[112345];

void dfs(int i, int depth) {
  if( ans[i] <= depth ) return;
  ans[i] = depth;
  for(int j = 0; j < (int)connect[i].size(); ++j) {
    dfs(connect[i][j], depth + 1);
  }
  return;
}

int main() {
  
  int n;
  std::cin >> n;

  for(int i = 0; i < n; ++i) {
    ans[i] = INF;
  }
  
  for(int i = 1; i < n; ++i) {
    int x, y;
    std::cin >> x >> y;
    x -= 1; y -= 1;
    connect[x].push_back(y);
    connect[y].push_back(x);
  }

  dfs(0, 0);
  for(int i = 0; i < n; ++i) {
    if( connect[i].size() == 1 ) {
      dfs(i, 0);
    }
  }

  for(int i = 0; i < n; ++i) {
    std::cout << ans[i] << std::endl;
  }
  
  return 0;
}
0