結果

問題 No.277 根掘り葉掘り
ユーザー 👑 jupirojupiro
提出日時 2020-07-14 02:03:55
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,913 bytes
コンパイル時間 1,663 ms
コンパイル使用メモリ 133,192 KB
実行使用メモリ 17,144 KB
最終ジャッジ日時 2023-08-09 02:24:02
合計ジャッジ時間 4,347 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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> dep;
vector<int> dp;


void treedep(int cur, int pre) {
    if (pre == -1) dep[cur] = 0;
    else dep[cur] = dep[pre] + 1;
    for (auto nxt : g[cur]) {
        if (nxt == pre) continue;
        treedep(nxt, cur);
    }
}

void dfs(int cur, int pre) {
    dp[cur] = inf;
    for (auto nxt : g[cur]) {
        if (nxt == pre) continue;
        dfs(nxt, cur);
        chmin(dp[cur], dp[nxt] + 1);
    }
    if (dp[cur] == inf) dp[cur] = 0;
}
int main()
{
    /*
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    */
    scanf("%d", &n);
    g.resize(n); dep.resize(n); dp.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);
    }
    treedep(0, -1);
    dfs(0, -1);
    for (int i = 0; i < n; i++) {
        printf("%d\n", min(dep[i], dp[i]));
    }
    return 0;
}
0