結果

問題 No.277 根掘り葉掘り
ユーザー kappybarkappybar
提出日時 2020-05-01 18:08:38
言語 C++14
(gcc 11.2.0 + boost 1.78.0)
結果
AC  
実行時間 224 ms / 3,000 ms
コード長 978 bytes
コンパイル時間 1,445 ms
使用メモリ 17,108 KB
最終ジャッジ日時 2023-01-13 18:02:38
合計ジャッジ時間 5,281 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 4 ms
6,040 KB
testcase_01 AC 4 ms
6,052 KB
testcase_02 AC 4 ms
6,092 KB
testcase_03 AC 4 ms
6,096 KB
testcase_04 AC 5 ms
6,064 KB
testcase_05 AC 5 ms
6,136 KB
testcase_06 AC 5 ms
6,020 KB
testcase_07 AC 5 ms
6,140 KB
testcase_08 AC 4 ms
6,036 KB
testcase_09 AC 224 ms
17,108 KB
testcase_10 AC 185 ms
9,716 KB
testcase_11 AC 211 ms
9,372 KB
testcase_12 AC 215 ms
13,112 KB
testcase_13 AC 210 ms
9,424 KB
testcase_14 AC 210 ms
9,708 KB
testcase_15 AC 213 ms
10,160 KB
testcase_16 AC 212 ms
9,736 KB
testcase_17 AC 209 ms
9,764 KB
testcase_18 AC 211 ms
9,640 KB
testcase_19 AC 211 ms
9,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll =  long long ;
using P = pair<int,int> ;
using pll = pair<ll,ll>;
constexpr int INF = 1e9;
constexpr ll LINF = 1e18;
constexpr int MOD = 1000000007;
int n = 100005;
vector<vector<int>> tree(n,vector<int>());
vector<int> R(n,0);
vector<int> L(n,0);

int dfs(int i,int p=-1,int d=0){
    R[i] = d;
    int res = INF;
    for(int c:tree[i]){
        if(c == p) continue;
        res = min(res,dfs(c,i,d+1)+1);
    }
    L[i] = (res==INF ? 0 : res);
    return L[i];
}

void dfs2(int i,int p=-1){
    if(p != -1) L[i] = min(L[i],L[p]+1);
    for(int c:tree[i]){
        if(c==p) continue;
        dfs2(c,i);
    }
}

int main(){
    cin >> n;
    rep(i,n-1){
        int a,b;
        cin >> a >> b;
        --a;--b;
        tree[a].push_back(b);
        tree[b].push_back(a);
    }
    dfs(0);
    dfs2(0);
    rep(i,n){
        cout << min(L[i],R[i]) << endl;
    }
    return 0;
}
0