結果

問題 No.277 根掘り葉掘り
ユーザー kk
提出日時 2021-03-17 13:24:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 179 ms / 3,000 ms
コード長 743 bytes
コンパイル時間 2,282 ms
コンパイル使用メモリ 200,600 KB
実行使用メモリ 9,872 KB
最終ジャッジ日時 2024-04-26 23:49:38
合計ジャッジ時間 5,541 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,816 KB
testcase_01 AC 4 ms
6,944 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 4 ms
6,944 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 4 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 173 ms
9,284 KB
testcase_10 AC 166 ms
9,872 KB
testcase_11 AC 176 ms
9,600 KB
testcase_12 AC 179 ms
9,472 KB
testcase_13 AC 176 ms
9,848 KB
testcase_14 AC 175 ms
9,728 KB
testcase_15 AC 174 ms
9,600 KB
testcase_16 AC 170 ms
9,728 KB
testcase_17 AC 172 ms
9,728 KB
testcase_18 AC 172 ms
9,728 KB
testcase_19 AC 173 ms
9,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

const int INF = 1<<28;

int n;
vector<int> g[100000];
int dist[100000];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

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

  fill(dist, dist + n, INF);
  queue<int> q;
  for (int i = 0; i < n; i++) {
    if (i == 0 || g[i].size() == 1) {
      q.push(i);
      dist[i] = 0;
    }
  }

  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int w: g[v]) {
      if (dist[w] == INF) {
        dist[w] = dist[v] + 1;
        q.push(w);
      }
    }
  }

  for (int i = 0; i < n; i++)
    cout << dist[i] << endl;
  
  return 0;
}
0