結果

問題 No.277 根掘り葉掘り
ユーザー ctyl_0ctyl_0
提出日時 2015-09-05 03:44:33
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 93 ms / 3,000 ms
コード長 1,635 bytes
コンパイル時間 792 ms
コンパイル使用メモリ 81,604 KB
実行使用メモリ 9,908 KB
最終ジャッジ日時 2023-09-26 08:38:48
合計ジャッジ時間 3,167 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,684 KB
testcase_01 AC 4 ms
5,724 KB
testcase_02 AC 3 ms
5,760 KB
testcase_03 AC 3 ms
5,672 KB
testcase_04 AC 3 ms
5,720 KB
testcase_05 AC 4 ms
5,720 KB
testcase_06 AC 4 ms
5,776 KB
testcase_07 AC 3 ms
5,996 KB
testcase_08 AC 4 ms
5,712 KB
testcase_09 AC 92 ms
9,212 KB
testcase_10 AC 71 ms
9,908 KB
testcase_11 AC 90 ms
9,428 KB
testcase_12 AC 89 ms
9,532 KB
testcase_13 AC 92 ms
9,604 KB
testcase_14 AC 89 ms
9,548 KB
testcase_15 AC 89 ms
9,760 KB
testcase_16 AC 93 ms
9,740 KB
testcase_17 AC 93 ms
9,484 KB
testcase_18 AC 92 ms
9,492 KB
testcase_19 AC 89 ms
9,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>

#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
typedef long long ll;
const int inf = (int)1e+9;

using namespace std;


int inputValue(){
    int a;
    cin >> a;
    return a;
};

void inputArray(int * p, int a){
    rep(i, a){
        cin >> p[i];
    }
};

void inputVector(vector<int> * p, int a){
    rep(i, a){
        int input;
        cin >> input;
        p -> push_back(input);
    }
}

template <typename T>
void output(T a, int precision) {
    if(precision > 0){
        cout << setprecision(precision)  << a << "\n";
    }
    else{
        cout << a << "\n";
    }
}

vector<int> node[100000];
int cost[100000];

int main(int argc, const char * argv[]) {
    
    // source code
    int N = inputValue();
    
    rep(i, N){
        cost[i] = inf;
    }
    
    rep(i, N - 1){
        int x, y;
        std::cin >> x >> y;
        node[x - 1].push_back(y - 1);
        node[y - 1].push_back(x - 1);
    }
    
    queue<int> q;
    cost[0] = 0;
    q.push(0);
    rep(i, N){
        if (node[i].size() == 1) {
            cost[i] = 0;
            q.push(i);
        }
    }
    
    while (!q.empty()) {
        int f = q.front();
        q.pop();
        rep(i, node[f].size()){
            if (cost[node[f][i]] > cost[f] + 1) {
                cost[node[f][i]] = cost[f] + 1;
                q.push(node[f][i]);
            }
        }
    }
    
    rep(i, N){
        output(cost[i], 0);
    }
    
    return 0;
}
0