結果

問題 No.1817 Reversed Edges
コンテスト
ユーザー nawawan
提出日時 2022-01-21 21:56:18
言語 C++17
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 130 ms / 2,000 ms
コード長 1,002 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,311 ms
コンパイル使用メモリ 220,004 KB
実行使用メモリ 14,336 KB
最終ジャッジ日時 2026-06-25 01:39:41
合計ジャッジ時間 4,613 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#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