結果

問題 No.277 根掘り葉掘り
ユーザー motimoti
提出日時 2015-10-22 22:34:31
言語 C++11
(gcc 8.5.0)
結果
AC  
実行時間 208 ms / 3,000 ms
コード長 1,272 bytes
コンパイル時間 866 ms
使用メモリ 10,576 KB
最終ジャッジ日時 2023-02-22 22:32:41
合計ジャッジ時間 4,873 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 3 ms
6,248 KB
testcase_01 AC 3 ms
6,172 KB
testcase_02 AC 4 ms
6,208 KB
testcase_03 AC 3 ms
6,172 KB
testcase_04 AC 3 ms
6,252 KB
testcase_05 AC 3 ms
6,252 KB
testcase_06 AC 3 ms
6,196 KB
testcase_07 AC 3 ms
6,256 KB
testcase_08 AC 3 ms
6,260 KB
testcase_09 AC 198 ms
9,404 KB
testcase_10 AC 182 ms
10,576 KB
testcase_11 AC 205 ms
9,752 KB
testcase_12 AC 202 ms
9,688 KB
testcase_13 AC 208 ms
10,084 KB
testcase_14 AC 201 ms
9,992 KB
testcase_15 AC 202 ms
9,708 KB
testcase_16 AC 201 ms
9,760 KB
testcase_17 AC 198 ms
9,936 KB
testcase_18 AC 201 ms
9,744 KB
testcase_19 AC 200 ms
9,896 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