結果

問題 No.277 根掘り葉掘り
ユーザー nasadigitalnasadigital
提出日時 2015-09-06 23:46:10
言語 C++11
(gcc 8.5.0)
結果
AC  
実行時間 219 ms / 3,000 ms
コード長 1,351 bytes
コンパイル時間 1,270 ms
使用メモリ 12,640 KB
最終ジャッジ日時 2023-02-17 07:40:53
合計ジャッジ時間 4,733 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 2 ms
9,692 KB
testcase_01 AC 1 ms
7,604 KB
testcase_02 AC 1 ms
9,672 KB
testcase_03 AC 1 ms
9,676 KB
testcase_04 AC 1 ms
7,588 KB
testcase_05 AC 2 ms
7,652 KB
testcase_06 AC 2 ms
7,568 KB
testcase_07 AC 2 ms
7,656 KB
testcase_08 AC 1 ms
7,592 KB
testcase_09 AC 213 ms
12,640 KB
testcase_10 AC 186 ms
11,568 KB
testcase_11 AC 206 ms
10,188 KB
testcase_12 AC 212 ms
12,040 KB
testcase_13 AC 219 ms
10,616 KB
testcase_14 AC 210 ms
10,524 KB
testcase_15 AC 209 ms
10,456 KB
testcase_16 AC 208 ms
10,484 KB
testcase_17 AC 213 ms
10,532 KB
testcase_18 AC 210 ms
10,316 KB
testcase_19 AC 210 ms
10,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> ii;

vector<vi> adjList;
int rez[100001];
int rez2[100001];
bool passed[100001];

void dfs(int cur,int d){
    if(passed[cur])
        return;
    passed[cur]=true;
    rez[cur]=min(d,rez2[cur]);
    for(int ctr1=0;ctr1<adjList[cur].size();ctr1++){
        dfs(adjList[cur][ctr1],rez[cur]+1);
    }
}

int main()
{
    memset(passed,false,sizeof(passed));
    int n;
    cin>>n;
    for(int ctr1=0;ctr1<n;ctr1++)
    {
        vi t;
        adjList.push_back(t);
        rez[ctr1]=9999999;
    }
    for(int ctr1=0;ctr1<n-1;ctr1++){
        int x,y;
        cin>>x>>y;
        x--;y--;
        adjList[x].push_back(y);
        adjList[y].push_back(x);
    }

    queue<ii> kju;
    for(int ctr1=0;ctr1<n;ctr1++)
        if(adjList[ctr1].size()==1)
            kju.push(make_pair(ctr1,0));
    while(!kju.empty()){
        ii t=kju.front();
        kju.pop();
        if(passed[t.first])
            continue;
        passed[t.first]=true;
        rez2[t.first]=t.second;
        for(int ctr1=0;ctr1<adjList[t.first].size();ctr1++)
            kju.push(make_pair(adjList[t.first][ctr1],t.second+1));
    }
    memset(passed,false,sizeof(passed));
    dfs(0,0);
    for(int ctr1=0;ctr1<n;ctr1++)
        cout<<min(rez[ctr1],rez2[ctr1])<<endl;
    return 0;
}
0