結果

問題 No.277 根掘り葉掘り
ユーザー nasadigitalnasadigital
提出日時 2015-09-06 23:40:18
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,272 bytes
コンパイル時間 1,573 ms
コンパイル使用メモリ 154,752 KB
実行使用メモリ 13,832 KB
最終ジャッジ日時 2023-09-26 09:50:38
合計ジャッジ時間 4,979 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 WA -
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 WA -
testcase_08 AC 2 ms
4,384 KB
testcase_09 WA -
testcase_10 AC 204 ms
12,908 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

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

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

void dfs(int cur,int d){
    rez[cur]=d;
    for(int ctr1=0;ctr1<adjList[cur].size();ctr1++){
        dfs(adjList[cur][ctr1],d+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);
        adjListR.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);
        adjListR[y].push_back(x);
    }
    dfs(0,0);
    queue<ii> kju;
    for(int ctr1=0;ctr1<n;ctr1++)
        if(adjList[ctr1].size()==0)
            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<adjListR[t.first].size();ctr1++)
            kju.push(make_pair(adjListR[t.first][ctr1],t.second+1));
    }
    for(int ctr1=0;ctr1<n;ctr1++)
        cout<<min(rez[ctr1],rez2[ctr1])<<endl;
    return 0;
}
0