結果

問題 No.1817 Reversed Edges
ユーザー nawawannawawan
提出日時 2022-01-21 21:56:18
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 234 ms / 2,000 ms
コード長 1,002 bytes
コンパイル時間 2,311 ms
コンパイル使用メモリ 207,036 KB
実行使用メモリ 14,352 KB
最終ジャッジ日時 2023-08-17 05:51:06
合計ジャッジ時間 7,827 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 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,380 KB
testcase_07 AC 202 ms
8,456 KB
testcase_08 AC 92 ms
5,624 KB
testcase_09 AC 188 ms
8,012 KB
testcase_10 AC 106 ms
6,012 KB
testcase_11 AC 137 ms
6,764 KB
testcase_12 AC 228 ms
9,012 KB
testcase_13 AC 229 ms
9,012 KB
testcase_14 AC 230 ms
9,112 KB
testcase_15 AC 230 ms
9,060 KB
testcase_16 AC 231 ms
9,172 KB
testcase_17 AC 234 ms
9,172 KB
testcase_18 AC 228 ms
9,332 KB
testcase_19 AC 229 ms
9,164 KB
testcase_20 AC 231 ms
9,068 KB
testcase_21 AC 231 ms
9,124 KB
testcase_22 AC 178 ms
9,368 KB
testcase_23 AC 181 ms
9,468 KB
testcase_24 AC 205 ms
14,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
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].push_back(b);
        G[b].push_back(a);
    } 
    vector<int> dp(N);
    auto dfs = [&](auto dfs, int now, int par = -1)->void{
        for(int to : G[now]){
            if(to == par) continue;
            dfs(dfs, to, now);
            dp[now] += dp[to];
            if(now > to) dp[now]++;
        }
    };
    dfs(dfs, 0);
    auto dfs2 = [&](auto dfs2, int now, int temp, int par = -1)->void{
        dp[now] += temp;
        for(int to : G[now]){
            if(to == par) continue;
            if(now > to){
                dfs2(dfs2, to, dp[now] - dp[to] - 1, now);
            }
            else{
                dfs2(dfs2, to, dp[now] - dp[to] + 1, now);
            }
        }
    };
    dfs2(dfs2, 0, 0);
    for(int i = 0; i < N; i++) cout << dp[i] << endl;
}
0