結果

問題 No.277 根掘り葉掘り
ユーザー face4face4
提出日時 2018-10-31 19:19:53
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 126 ms / 3,000 ms
コード長 1,224 bytes
コンパイル時間 660 ms
コンパイル使用メモリ 79,364 KB
実行使用メモリ 10,820 KB
最終ジャッジ日時 2023-08-12 11:48:41
合計ジャッジ時間 3,027 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 81 ms
9,624 KB
testcase_10 AC 66 ms
10,820 KB
testcase_11 AC 97 ms
9,816 KB
testcase_12 AC 102 ms
9,568 KB
testcase_13 AC 126 ms
10,044 KB
testcase_14 AC 110 ms
10,000 KB
testcase_15 AC 112 ms
10,044 KB
testcase_16 AC 112 ms
9,784 KB
testcase_17 AC 111 ms
9,828 KB
testcase_18 AC 116 ms
9,956 KB
testcase_19 AC 115 ms
9,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<stack>
using namespace std;

int main(){
    int n;
    cin >> n;

    int INF = 100001;
    vector<vector<int>> v(n+1);
    vector<int> r(n+1, INF), l(n+1, INF);
    vector<int> deg(n+1, 0);

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

    stack<pair<int,int>> s;
    s.push({1, 0});
    while(!s.empty()){
        pair<int,int> p = s.top();  s.pop();
        r[p.first] = p.second;
        for(int next : v[p.first]){
            if(r[next] != INF)  continue;
            s.push({next, p.second+1});
        }
    }

    for(int i = 2; i <= n; i++){
        if(deg[i] != 1)  continue;
        s.push({i, 0});
        while(!s.empty()){
            pair<int,int> now = s.top();    s.pop();
            if(now.second >= l[now.first])  continue;
            l[now.first] = now.second;
            for(int next : v[now.first]){
                if(now.second + 1 < l[next]){
                    s.push({next, now.second + 1});
                }
            }
        }
    }

    for(int i = 1; i <= n; i++) printf("%d\n", min(l[i], r[i]));

    return 0;
}
0