結果

問題 No.277 根掘り葉掘り
ユーザー motimoti
提出日時 2015-10-22 21:53:12
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,182 bytes
コンパイル時間 1,854 ms
コンパイル使用メモリ 108,064 KB
実行使用メモリ 10,088 KB
最終ジャッジ日時 2023-09-30 05:03:13
合計ジャッジ時間 7,592 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
10,088 KB
testcase_01 AC 4 ms
5,980 KB
testcase_02 AC 3 ms
5,712 KB
testcase_03 AC 4 ms
5,716 KB
testcase_04 AC 4 ms
5,708 KB
testcase_05 WA -
testcase_06 AC 3 ms
5,820 KB
testcase_07 AC 3 ms
5,716 KB
testcase_08 AC 4 ms
5,764 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <assert.h>
#include <array>
#include <cstdio>
#include <cstring>
#include <random>
#include <functional>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)

typedef long long ll;

int N;
vector<int> tree[100001];
vector<int> deg;

int bfs(int s) {
  if(s == 0) return 0;
  
  queue<pair<int, int>> q;
  q.emplace(s, 0);

  vector<bool> vis(N);
  vis[s] = 1;

  while(!q.empty()) {
    auto p = q.front(); q.pop();
    if(deg[p.first] == 1) {
      return p.second;
    }
    rep(i, tree[p.first].size()) {
      int tar = tree[p.first][i];
      if(vis[tar]) { continue; }
      vis[tar] = 1;
      q.emplace(tar, p.second+1);
    }
  }
  assert(false);
}

int main() {

  cin >> N;
  deg.resize(N);
  rep(i, N-1) {
    int x, y; cin >> x >> y;
    x--, y--;
    deg[x]++, deg[y]++;
    tree[x].push_back(y);
    tree[y].push_back(x);
  }

  rep(i, N) {
    cout << bfs(i) << endl;
  }

  return 0;
}
0