結果

問題 No.277 根掘り葉掘り
ユーザー nasadigitalnasadigital
提出日時 2015-09-06 23:46:10
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 227 ms / 3,000 ms
コード長 1,351 bytes
コンパイル時間 1,344 ms
コンパイル使用メモリ 155,000 KB
実行使用メモリ 12,552 KB
最終ジャッジ日時 2023-09-26 09:50:51
合計ジャッジ時間 5,046 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 227 ms
12,552 KB
testcase_10 AC 200 ms
11,544 KB
testcase_11 AC 217 ms
10,664 KB
testcase_12 AC 223 ms
12,316 KB
testcase_13 AC 226 ms
10,756 KB
testcase_14 AC 219 ms
10,552 KB
testcase_15 AC 226 ms
10,540 KB
testcase_16 AC 225 ms
10,400 KB
testcase_17 AC 220 ms
10,472 KB
testcase_18 AC 219 ms
10,228 KB
testcase_19 AC 219 ms
10,132 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