結果

問題 No.277 根掘り葉掘り
ユーザー mayoko_mayoko_
提出日時 2015-09-04 22:48:41
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,256 bytes
コンパイル時間 834 ms
コンパイル使用メモリ 79,888 KB
実行使用メモリ 17,488 KB
最終ジャッジ日時 2023-09-26 06:06:33
合計ジャッジ時間 3,862 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,724 KB
testcase_01 AC 3 ms
5,732 KB
testcase_02 WA -
testcase_03 AC 4 ms
5,732 KB
testcase_04 AC 4 ms
5,728 KB
testcase_05 AC 3 ms
5,932 KB
testcase_06 AC 4 ms
5,772 KB
testcase_07 AC 4 ms
5,792 KB
testcase_08 AC 3 ms
5,732 KB
testcase_09 AC 191 ms
17,488 KB
testcase_10 AC 168 ms
9,724 KB
testcase_11 AC 186 ms
9,632 KB
testcase_12 AC 191 ms
13,532 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

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> G[MAXN];
int dp[MAXN], d[MAXN];

void dfs(int v, int p, int depth) {
    d[v] = depth;
    dp[v] = MAXN;
    bool leaf = true;
    for (int ch : G[v]) {
        if (ch == p) continue;
        leaf = false;
        dfs(ch, v, depth+1);
        dp[v] = min(dp[v], dp[ch]+1);
    }
    if (leaf) dp[v] = 0;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    for (int i = 0; i < N-1; i++) {
        int x, y;
        cin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs(1, 0, 0);
    for (int i = 1; i <= N; i++) {
        cout << min(d[i], dp[i]) << endl;
    }
    return 0;
}
0