結果

問題 No.277 根掘り葉掘り
ユーザー koprickykopricky
提出日時 2017-08-07 11:48:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 185 ms / 3,000 ms
コード長 1,578 bytes
コンパイル時間 1,515 ms
コンパイル使用メモリ 164,716 KB
実行使用メモリ 17,280 KB
最終ジャッジ日時 2024-04-20 03:24:04
合計ジャッジ時間 4,695 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,632 KB
testcase_01 AC 3 ms
5,760 KB
testcase_02 AC 4 ms
5,760 KB
testcase_03 AC 4 ms
5,760 KB
testcase_04 AC 3 ms
5,760 KB
testcase_05 AC 3 ms
5,760 KB
testcase_06 AC 3 ms
5,760 KB
testcase_07 AC 4 ms
5,760 KB
testcase_08 AC 3 ms
5,760 KB
testcase_09 AC 184 ms
17,280 KB
testcase_10 AC 164 ms
10,584 KB
testcase_11 AC 165 ms
9,856 KB
testcase_12 AC 185 ms
13,696 KB
testcase_13 AC 177 ms
10,112 KB
testcase_14 AC 178 ms
10,364 KB
testcase_15 AC 179 ms
10,692 KB
testcase_16 AC 175 ms
10,312 KB
testcase_17 AC 170 ms
10,368 KB
testcase_18 AC 182 ms
10,168 KB
testcase_19 AC 181 ms
10,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(a, b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define pb push_back
#define show(x) cout <<#x<<" = "<<(x)<<endl
#define spair(p) cout <<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout <<" "<<kbrni;cout<<endl

using namespace std;

typedef pair<int,int>P;

const int MAX_N = 100005;

vector<int> G[MAX_N];
int d[MAX_N];
vector<int> ha;
int memo[MAX_N];

void dfs(int u,int p,int k)
{
    d[u] = k;
    if(G[u].size() == 1 && G[u][0] == p){
        ha.pb(u);
    }
    rep(i,G[u].size()){
        if(G[u][i] != p){
            dfs(G[u][i],u,k+1);
        }
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    rep(i,n-1){
        int a,b;
        cin >> a >> b;
        G[a-1].pb(b-1);
        G[b-1].pb(a-1);
    }
    dfs(0,-1,0);
    rep(i,n){
        memo[i] = INF;
    }
    queue<int> que;
    rep(i,ha.size()){
        memo[ha[i]] = 0;
        que.push(ha[i]);
    }
    while(!que.empty()){
        int p = que.front();
        que.pop();
        rep(i,G[p].size()){
            if(memo[G[p][i]] > memo[p]+1){
                memo[G[p][i]] = memo[p]+1;
                que.push(G[p][i]);
            }
        }
    }
    rep(i,n){
        cout << min(d[i],memo[i]) << endl;
    }
    return 0;
}
0