結果

問題 No.277 根掘り葉掘り
ユーザー motimoti
提出日時 2015-10-22 22:34:31
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 188 ms / 3,000 ms
コード長 1,272 bytes
コンパイル時間 954 ms
コンパイル使用メモリ 109,072 KB
実行使用メモリ 10,504 KB
最終ジャッジ日時 2023-09-30 05:03:37
合計ジャッジ時間 4,380 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,688 KB
testcase_01 AC 3 ms
5,696 KB
testcase_02 AC 2 ms
5,692 KB
testcase_03 AC 3 ms
5,716 KB
testcase_04 AC 3 ms
5,728 KB
testcase_05 AC 3 ms
5,704 KB
testcase_06 AC 3 ms
5,720 KB
testcase_07 AC 3 ms
5,772 KB
testcase_08 AC 3 ms
5,692 KB
testcase_09 AC 185 ms
9,388 KB
testcase_10 AC 182 ms
10,504 KB
testcase_11 AC 185 ms
9,768 KB
testcase_12 AC 182 ms
9,668 KB
testcase_13 AC 181 ms
10,304 KB
testcase_14 AC 182 ms
10,044 KB
testcase_15 AC 186 ms
9,584 KB
testcase_16 AC 180 ms
10,024 KB
testcase_17 AC 182 ms
10,176 KB
testcase_18 AC 188 ms
9,616 KB
testcase_19 AC 181 ms
9,968 KB
権限があれば一括ダウンロードができます

ソースコード

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 dist[100001];

void bfs() {

  rep(i, N) dist[i] = 1e9;

  queue<pair<int, int>> q;
  vector<bool> vis(N);
  q.emplace(0, 0);
  vis[0] = 1;  
  dist[0] = 0;
  rep(i, N) {
    if(deg[i] == 1) q.emplace(i, 0), dist[i] = 0, vis[i] = 1;
  }

  while(!q.empty()) {
    auto p = q.front(); q.pop();
    dist[p.first] = min(dist[p.first], 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);
    }
  }

}

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);
  }

  bfs();
  rep(i, N) cout << dist[i] << endl;

  return 0;
}
0