結果

問題 No.277 根掘り葉掘り
ユーザー 👑 jupirojupiro
提出日時 2020-07-14 02:09:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 63 ms / 3,000 ms
コード長 2,050 bytes
コンパイル時間 2,278 ms
コンパイル使用メモリ 136,156 KB
実行使用メモリ 15,132 KB
最終ジャッジ日時 2023-08-09 02:32:19
合計ジャッジ時間 3,430 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 63 ms
15,132 KB
testcase_10 AC 40 ms
10,200 KB
testcase_11 AC 52 ms
9,472 KB
testcase_12 AC 58 ms
12,516 KB
testcase_13 AC 56 ms
9,592 KB
testcase_14 AC 53 ms
9,840 KB
testcase_15 AC 55 ms
9,808 KB
testcase_16 AC 59 ms
10,008 KB
testcase_17 AC 54 ms
9,840 KB
testcase_18 AC 53 ms
9,404 KB
testcase_19 AC 54 ms
9,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;
const long double PI = acos((long double)(-1));

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;

int n;
vector<vector<int>> g;
vector<int> s;

void dfs(int cur, int pre) {
    bool leaf = true;
    for (auto nxt : g[cur]) {
        if (nxt == pre) continue;
        leaf = false;
        dfs(nxt, cur);
    }
    if (leaf) s.emplace_back(cur);
}

vector<int> MultiBFS(vector<int> &s, vector<vector<int>>& g) {
    queue<int> q;
    vector<int> d(g.size(), inf);
    for (int i = 0; i < s.size(); i++) d[s[i]] = 0, q.emplace(s[i]);
    while (!q.empty()) {
        int cur = q.front();
        q.pop();
        for (auto nxt : g[cur]) {
            if (chmin(d[nxt], d[cur] + 1)) q.emplace(nxt);
        }
    }
    return d;
}
int main()
{
    /*
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    */
    scanf("%d", &n);
    g.resize(n);
    for (int i = 0; i < n - 1; i++) {
        int u, v; scanf("%d %d", &u, &v);
        u--; v--;
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }
    dfs(0, -1);
    s.emplace_back(0);
    auto d = MultiBFS(s, g);
    for (int i = 0; i < n; i++) {
        printf("%d\n", d[i]);
    }
    return 0;
}
0