結果

問題 No.1817 Reversed Edges
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-05-24 01:27:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 226 ms / 2,000 ms
コード長 1,114 bytes
コンパイル時間 1,151 ms
コンパイル使用メモリ 114,644 KB
実行使用メモリ 15,076 KB
最終ジャッジ日時 2023-08-24 22:55:29
合計ジャッジ時間 6,321 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 191 ms
8,916 KB
testcase_08 AC 93 ms
5,900 KB
testcase_09 AC 179 ms
8,308 KB
testcase_10 AC 106 ms
6,148 KB
testcase_11 AC 134 ms
6,936 KB
testcase_12 AC 221 ms
9,860 KB
testcase_13 AC 216 ms
9,424 KB
testcase_14 AC 218 ms
9,308 KB
testcase_15 AC 223 ms
9,708 KB
testcase_16 AC 222 ms
9,700 KB
testcase_17 AC 223 ms
9,360 KB
testcase_18 AC 223 ms
9,360 KB
testcase_19 AC 224 ms
9,676 KB
testcase_20 AC 226 ms
9,748 KB
testcase_21 AC 224 ms
9,360 KB
testcase_22 AC 185 ms
9,656 KB
testcase_23 AC 187 ms
9,828 KB
testcase_24 AC 205 ms
15,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <numeric>
#include <deque>
#include <complex>
#include <cassert>

using namespace std;
using ll = long long;
int x=0;
vector<int> ans;

void dfs(vector<vector<pair<int, int>>> &E, int from, int p){

    for(auto [to, c] : E[from]){
        if (p == to) continue;
        if (c == -1) x++;
        dfs(E, to, from);
    }
}

void dfs2(vector<vector<pair<int, int>>> &E, int from, int p){

    for(auto [to, c] : E[from]){
        if (p == to) continue;
        if (c == 1) ans[to] = ans[from]+1;
        else ans[to] = ans[from]-1;
        dfs2(E, to, from);
    }
}

int main(){

    int N, a, b;
    cin >> N;
    ans.resize(N);
    vector<vector<pair<int, int>>> E(N);
    for (int i=0; i<N-1; i++){
        cin >> a >> b; a--; b--;
        if (a > b) swap(a, b);
        E[a].push_back({b, 1});
        E[b].push_back({a, -1});
    }

    dfs(E, 0, -1);
    ans[0] = x;
    dfs2(E, 0, -1);

    for (int i=0; i<N; i++) cout << ans[i] << endl;

    return 0;
}
0