結果

問題 No.277 根掘り葉掘り
ユーザー kappybarkappybar
提出日時 2020-05-01 18:08:38
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 228 ms / 3,000 ms
コード長 978 bytes
コンパイル時間 1,658 ms
コンパイル使用メモリ 169,752 KB
実行使用メモリ 16,996 KB
最終ジャッジ日時 2023-08-26 00:27:51
合計ジャッジ時間 5,430 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,256 KB
testcase_01 AC 5 ms
5,940 KB
testcase_02 AC 4 ms
5,964 KB
testcase_03 AC 5 ms
5,936 KB
testcase_04 AC 4 ms
6,132 KB
testcase_05 AC 4 ms
6,000 KB
testcase_06 AC 5 ms
6,120 KB
testcase_07 AC 5 ms
5,960 KB
testcase_08 AC 5 ms
5,960 KB
testcase_09 AC 228 ms
16,996 KB
testcase_10 AC 193 ms
9,604 KB
testcase_11 AC 213 ms
9,216 KB
testcase_12 AC 226 ms
13,148 KB
testcase_13 AC 221 ms
9,356 KB
testcase_14 AC 215 ms
9,852 KB
testcase_15 AC 211 ms
10,076 KB
testcase_16 AC 220 ms
9,740 KB
testcase_17 AC 214 ms
9,628 KB
testcase_18 AC 219 ms
9,564 KB
testcase_19 AC 215 ms
9,648 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