結果

問題 No.1817 Reversed Edges
ユーザー sxmIKjcEGh46HMFsxmIKjcEGh46HMF
提出日時 2022-01-21 23:34:27
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 213 ms / 2,000 ms
コード長 961 bytes
コンパイル時間 3,879 ms
コンパイル使用メモリ 228,856 KB
実行使用メモリ 16,764 KB
最終ジャッジ日時 2023-08-17 08:21:50
合計ジャッジ時間 8,805 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 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,376 KB
testcase_07 AC 188 ms
8,600 KB
testcase_08 AC 90 ms
5,640 KB
testcase_09 AC 177 ms
8,324 KB
testcase_10 AC 100 ms
5,984 KB
testcase_11 AC 133 ms
6,696 KB
testcase_12 AC 213 ms
9,076 KB
testcase_13 AC 212 ms
9,080 KB
testcase_14 AC 208 ms
9,288 KB
testcase_15 AC 205 ms
9,076 KB
testcase_16 AC 212 ms
9,172 KB
testcase_17 AC 209 ms
9,120 KB
testcase_18 AC 209 ms
9,120 KB
testcase_19 AC 207 ms
9,124 KB
testcase_20 AC 210 ms
9,124 KB
testcase_21 AC 210 ms
9,168 KB
testcase_22 AC 178 ms
9,352 KB
testcase_23 AC 183 ms
9,584 KB
testcase_24 AC 200 ms
16,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>

#define fi first
#define se second
#define eb emplace_back

using namespace std;
using namespace atcoder;
using ll = long long;

int dfs(int v, int p, vector<vector<int>>& G) {
    int ret = 0;
    if (p != -1 && v < p) ++ret;
    for (int nv: G[v]) {
        if (nv == p) continue;
        ret += dfs(nv, v, G);
    }
    return ret;
}

void dfs2(int v, int p, int score, vector<int>& ans, vector<vector<int>>& G) {
    if (p != -1) score += (v > p ? 1: -1);
    ans[v] = score;
    for (int nv: G[v]) {
        if (nv == p) continue;
        dfs2(nv, v, score, ans, G);
    }
}

int main() {
    int N; cin >> N;
    vector<vector<int>> G(N);
    for (int i = 0; i < N - 1; ++i) {
        int a, b; cin >> a >> b; --a; --b;
        G[a].eb(b);
        G[b].eb(a);
    }

    vector<int> ans(N);
    ans[0] = dfs(0, -1, G);
    dfs2(0, -1, ans[0], ans, G);

    for (int x: ans) cout << x << endl;
    return 0;
}
0