結果

問題 No.1817 Reversed Edges
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-05-24 01:22:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,083 bytes
コンパイル時間 1,200 ms
コンパイル使用メモリ 112,652 KB
実行使用メモリ 15,064 KB
最終ジャッジ日時 2023-08-24 22:53:30
合計ジャッジ時間 8,270 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 180 ms
9,876 KB
testcase_23 AC 187 ms
9,800 KB
testcase_24 AC 211 ms
15,064 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--;
        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