結果

問題 No.277 根掘り葉掘り
ユーザー alpha_virginis
提出日時 2015-09-04 22:39:53
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 240 ms / 3,000 ms
コード長 977 bytes
コンパイル時間 1,763 ms
コンパイル使用メモリ 160,512 KB
実行使用メモリ 14,104 KB
最終ジャッジ日時 2024-07-19 01:10:46
合計ジャッジ時間 5,104 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

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