結果

問題 No.277 根掘り葉掘り
ユーザー face4face4
提出日時 2018-10-31 19:06:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,195 bytes
コンパイル時間 791 ms
コンパイル使用メモリ 79,556 KB
実行使用メモリ 13,312 KB
最終ジャッジ日時 2024-04-30 01:47:16
合計ジャッジ時間 2,949 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

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

    int INF = 100001;
    vector<vector<int>> str(n+1), rev(n+1);
    vector<int> r(n+1, INF), l(n+1, INF);
    bool notLeaf[INF] = {};

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

    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 : str[p.first]){
            s.push({next, p.second+1});
        }
    }

    for(int i = 1; i <= n; i++){
        if(notLeaf[i])  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 : rev[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