結果

問題 No.277 根掘り葉掘り
ユーザー mayoko_mayoko_
提出日時 2015-09-04 23:13:58
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 222 ms / 3,000 ms
コード長 1,812 bytes
コンパイル時間 1,050 ms
コンパイル使用メモリ 92,372 KB
実行使用メモリ 14,912 KB
最終ジャッジ日時 2023-09-26 06:27:10
合計ジャッジ時間 5,158 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 187 ms
11,036 KB
testcase_10 AC 194 ms
14,912 KB
testcase_11 AC 212 ms
14,124 KB
testcase_12 AC 208 ms
12,924 KB
testcase_13 AC 222 ms
12,928 KB
testcase_14 AC 212 ms
12,736 KB
testcase_15 AC 201 ms
12,492 KB
testcase_16 AC 213 ms
12,692 KB
testcase_17 AC 213 ms
12,448 KB
testcase_18 AC 207 ms
12,624 KB
testcase_19 AC 208 ms
12,660 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