結果

問題 No.277 根掘り葉掘り
ユーザー mamekin
提出日時 2016-02-20 00:28:51
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 1,169 bytes
コンパイル時間 877 ms
コンパイル使用メモリ 99,888 KB
実行使用メモリ 16,768 KB
最終ジャッジ日時 2024-09-22 12:23:50
合計ジャッジ時間 5,283 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 10 WA * 8
権限があれば一括ダウンロードができます

ソースコード

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