結果

問題 No.277 根掘り葉掘り
ユーザー pekempeypekempey
提出日時 2015-09-04 23:08:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 220 ms / 3,000 ms
コード長 961 bytes
コンパイル時間 1,370 ms
コンパイル使用メモリ 149,512 KB
実行使用メモリ 9,808 KB
最終ジャッジ日時 2023-09-26 06:21:40
合計ジャッジ時間 5,474 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,768 KB
testcase_01 AC 4 ms
5,800 KB
testcase_02 AC 3 ms
5,796 KB
testcase_03 AC 4 ms
5,764 KB
testcase_04 AC 4 ms
5,744 KB
testcase_05 AC 4 ms
5,684 KB
testcase_06 AC 4 ms
5,796 KB
testcase_07 AC 4 ms
5,724 KB
testcase_08 AC 3 ms
6,020 KB
testcase_09 AC 217 ms
9,192 KB
testcase_10 AC 191 ms
9,612 KB
testcase_11 AC 215 ms
9,560 KB
testcase_12 AC 219 ms
9,400 KB
testcase_13 AC 220 ms
9,640 KB
testcase_14 AC 209 ms
9,512 KB
testcase_15 AC 209 ms
9,640 KB
testcase_16 AC 213 ms
9,548 KB
testcase_17 AC 212 ms
9,524 KB
testcase_18 AC 213 ms
9,808 KB
testcase_19 AC 211 ms
9,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a) for (int i = 0; i < (a); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define repr(i, a) for (int i = (a) - 1; i >= 0; i--)
#define repr2(i, a, b) for (int i = (b) - 1; i >= (a); i--)
using namespace std;
typedef long long ll;

int N;
vector<int> G[101010];
int dist[101010];

int main() {
    cin >> N;
    rep (i, N - 1) {
        int x, y;
        cin >> x >> y;
        x--; y--;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    const int inf = 1e9;
    rep (i, N) dist[i] = inf;
    queue<int> q;
    rep (i, N) {
    	if (i == 0 || G[i].size() == 1) {
    		q.push(i);
    		dist[i] = 0;	
    	}
    }
    while (!q.empty()) {
    	int curr = q.front(); q.pop();
    	for (int next : G[curr]) {
    		if (dist[next] > dist[curr] + 1) {
    			dist[next] = dist[curr] + 1;
    			q.push(next);
    		}
    	}
    }
    rep (i, N) {
    	cout << dist[i] << endl;
    }

    return 0;
}
0