結果

問題 No.277 根掘り葉掘り
ユーザー h_nosonh_noson
提出日時 2016-05-07 20:35:09
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,162 bytes
コンパイル時間 634 ms
コンパイル使用メモリ 70,684 KB
実行使用メモリ 11,072 KB
最終ジャッジ日時 2024-04-15 13:39:03
合計ジャッジ時間 5,067 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,888 KB
testcase_01 AC 3 ms
5,888 KB
testcase_02 AC 2 ms
5,760 KB
testcase_03 AC 2 ms
5,760 KB
testcase_04 WA -
testcase_05 AC 3 ms
5,888 KB
testcase_06 AC 4 ms
5,888 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 222 ms
9,344 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 332 ms
9,652 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 332 ms
9,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

#define RREP(i,s,e) for (i = s; i >= e; i--)
#define rrep(i,n) RREP(i,(int)(n)-1,0)
#define REP(i,s,e) for (i = s; i <= e; i++)
#define rep(i,n) REP(i,0,(int)(n)-1)
#define INF 100000000

typedef long long ll;

vector<int> e[100000];
int dist[100000];

void dijkstra(int x) {
    priority_queue<pair<int,int>> q;
    dist[x] = 0;
    q.push(make_pair(0,x));
    while (!q.empty()) {
        int from = q.top().second;
        q.pop();
        for (auto to : e[from]) {
            if (dist[to] > dist[from] + 1) {
                dist[to] = dist[from] + 1;
                q.push(make_pair(-dist[to],to));
            }
        }
    }
}

int main() {
    int i, n;
    vector<int> leaf;
    cin >> n;
    rep (i,n-1) {
        int x, y;
        cin >> x >> y;
        x--; y--;
        e[x].push_back(y);
        e[y].push_back(x);
    }
    leaf.push_back(0);
    REP (i,2,n) if (e[i].size() == 1)
        leaf.push_back(i);
    rep (i,n) dist[i] = INF;
    for (auto x : leaf)
        dijkstra(x);
    rep (i,n)
        cout << dist[i] << endl;
    return 0;
}
0