結果

問題 No.277 根掘り葉掘り
ユーザー mayoko_mayoko_
提出日時 2015-09-04 23:13:58
言語 C++11
(gcc 8.5.0)
結果
AC  
実行時間 203 ms / 3,000 ms
コード長 1,812 bytes
コンパイル時間 912 ms
使用メモリ 14,852 KB
最終ジャッジ日時 2023-02-17 03:34:53
合計ジャッジ時間 4,468 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 1 ms
7,656 KB
testcase_01 AC 1 ms
9,700 KB
testcase_02 AC 0 ms
9,716 KB
testcase_03 AC 1 ms
7,672 KB
testcase_04 AC 1 ms
9,692 KB
testcase_05 AC 1 ms
9,688 KB
testcase_06 AC 1 ms
9,692 KB
testcase_07 AC 1 ms
7,580 KB
testcase_08 AC 1 ms
7,588 KB
testcase_09 AC 165 ms
10,916 KB
testcase_10 AC 169 ms
14,852 KB
testcase_11 AC 194 ms
14,108 KB
testcase_12 AC 189 ms
12,844 KB
testcase_13 AC 203 ms
12,836 KB
testcase_14 AC 188 ms
12,832 KB
testcase_15 AC 187 ms
12,416 KB
testcase_16 AC 188 ms
12,776 KB
testcase_17 AC 189 ms
12,436 KB
testcase_18 AC 190 ms
12,596 KB
testcase_19 AC 185 ms
12,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
//#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
//#include<list>
#include<queue>
#include<deque>
#include<algorithm>
//#include<numeric>
#include<utility>
#include<complex>
//#include<memory>
#include<functional>
#include<cassert>
#include<set>
#include<stack>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

const int MAXN = 100010;
vector<int> L;

struct edge {
    int v;
    ll w;
    edge() {}
    edge(int v, ll w) : v(v), w(w) {};
};

vector<ll> dijkstra(int n, vector<vector<edge> >& G, int s) {
    vector<ll> d(n, LLONG_MAX/10); d[s] = 0;
    priority_queue<pair<ll, int> > que;
    que.push(make_pair(0ll, s));
    for (int el : L) {
        d[el] = 0;
        que.push(make_pair(0ll, el));
    }
    while (!que.empty()) {
        auto p = que.top(); que.pop();
        int u = p.second;
        ll dist = -p.first;
        if (dist > d[u]) continue;
        for (edge e : G[u]) {
            if (d[e.v] > d[u]+e.w) {
                d[e.v] = d[u] + e.w;
                que.push(make_pair(-d[e.v], e.v));
            }
        }
    }
    return d;
}

vector<vector<edge> > G;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    G.resize(N);
    for (int i = 0; i < N-1; i++) {
        int x, y;
        cin >> x >> y;
        x--; y--;
        G[x].emplace_back(y, 1);
        G[y].emplace_back(x, 1);
    }
    for (int i = 0; i < N; i++) if (G[i].size() == 1) L.push_back(i);
    auto d = dijkstra(N, G, 0);
    for (int i = 0; i < N; i++) {
        cout << d[i] << endl;
    }
    return 0;
}
0