結果

問題 No.277 根掘り葉掘り
ユーザー outlineoutline
提出日時 2021-02-15 05:19:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 65 ms / 3,000 ms
コード長 1,961 bytes
コンパイル時間 1,601 ms
コンパイル使用メモリ 145,492 KB
実行使用メモリ 13,276 KB
最終ジャッジ日時 2023-09-29 23:42:14
合計ジャッジ時間 3,848 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 65 ms
13,276 KB
testcase_10 AC 44 ms
9,600 KB
testcase_11 AC 57 ms
9,300 KB
testcase_12 AC 61 ms
11,560 KB
testcase_13 AC 61 ms
9,600 KB
testcase_14 AC 56 ms
9,512 KB
testcase_15 AC 58 ms
9,704 KB
testcase_16 AC 57 ms
9,488 KB
testcase_17 AC 56 ms
9,544 KB
testcase_18 AC 55 ms
9,464 KB
testcase_19 AC 56 ms
9,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
constexpr int INF = 1001001001;
// constexpr int mod = 1000000007;
constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    vector<vector<int>> g(n);
    for(int i = 1; i < n; ++i){
        int u, v;
        cin >> u >> v;
        --u, --v;
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }

    vector<bool> is_leaf(n, true);
    auto dfs = [&](auto&& self, int from = 0, int par = -1) -> void {
        for(int to : g[from]){
            if(to == par)   continue;
            is_leaf[from] = false;
            self(self, to, from);
        }
    };
    dfs(dfs);

    vector<int> dist(n, INF);
    queue<int> que;
    dist[0] = 0;
    que.emplace(0);
    for(int i = 1; i < n; ++i){
        if(is_leaf[i]){
            dist[i] = 0;
            que.emplace(i);
        }
    }
    while(!que.empty()){
        int from = que.front();
        que.pop();
        for(int to : g[from]){
            if(chmin(dist[to], dist[from] + 1)){
                que.emplace(to);
            }
        }
    }

    for(int i = 0; i < n; ++i)  cout << dist[i] << '\n';

    return 0;
}
0