結果

問題 No.277 根掘り葉掘り
ユーザー mamekinmamekin
提出日時 2016-02-20 00:28:51
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,169 bytes
コンパイル時間 847 ms
コンパイル使用メモリ 99,444 KB
実行使用メモリ 16,912 KB
最終ジャッジ日時 2023-10-23 18:36:16
合計ジャッジ時間 5,859 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 WA -
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 225 ms
16,912 KB
testcase_10 AC 192 ms
9,552 KB
testcase_11 AC 213 ms
8,992 KB
testcase_12 AC 219 ms
12,952 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

vector<vector<int> > edges;
vector<int> ans;

int solve(int curr, int prev, int dist)
{
    int tmp = INT_MAX;
    for(int next : edges[curr]){
        if(next != prev)
            tmp = min(tmp, solve(next, curr, dist + 1));
    }

    if(tmp < INT_MAX){
        ans[curr] = min(dist, tmp);
        return tmp + 1;
    }
    else{
        ans[curr] = 0;
        return 1;
    }
}

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

    ans.resize(n);
    solve(0, -1, 0);
    for(int i=0; i<n; ++i)
        cout << ans[i] << endl;

    return 0;
}
0