結果

問題 No.1817 Reversed Edges
ユーザー horoyoisawahoroyoisawa
提出日時 2022-01-21 22:38:30
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 112 ms / 2,000 ms
コード長 979 bytes
コンパイル時間 1,743 ms
コンパイル使用メモリ 176,292 KB
実行使用メモリ 16,720 KB
最終ジャッジ日時 2023-08-17 06:56:45
合計ジャッジ時間 5,463 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 100 ms
8,344 KB
testcase_08 AC 42 ms
5,596 KB
testcase_09 AC 92 ms
8,020 KB
testcase_10 AC 47 ms
5,896 KB
testcase_11 AC 65 ms
6,704 KB
testcase_12 AC 107 ms
9,372 KB
testcase_13 AC 107 ms
9,344 KB
testcase_14 AC 112 ms
9,204 KB
testcase_15 AC 105 ms
8,996 KB
testcase_16 AC 109 ms
9,200 KB
testcase_17 AC 109 ms
9,212 KB
testcase_18 AC 110 ms
9,120 KB
testcase_19 AC 108 ms
9,124 KB
testcase_20 AC 110 ms
9,072 KB
testcase_21 AC 105 ms
9,220 KB
testcase_22 AC 61 ms
9,176 KB
testcase_23 AC 61 ms
9,212 KB
testcase_24 AC 83 ms
16,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> tree;
vector<bool> visited;
vector<int> out;
int fir = 0;
void dps(int now, int prev, int flag, int ans) {
    if(visited[now]) return;
    visited[now] = true;
    if(flag) out[now] = ans;
    for(int i=0;i<tree[now].size();i++) {
        if(tree[now][i] != prev) {
            if(tree[now][i] > now) dps(tree[now][i], now, flag, ans + 1);
            else {
                fir++;
                dps(tree[now][i], now, flag, ans - 1);
            }
        }
    }
}

int main() {
    int n;
    cin >> n;
    tree.resize(n);
    visited.resize(n);
    out.resize(n);
    for(int i=0;i<n-1;i++) {
        int u, v;
        cin >> u >> v;
        u--;v--;
        tree[u].emplace_back(v);
        tree[v].emplace_back(u);
    }

    dps(0, INT_MAX, 0, 0);

    for(int i=0;i<n;i++) visited[i] = false;

    dps(0, -1, 1, fir);

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

    return 0;
}
0