結果

問題 No.1817 Reversed Edges
ユーザー asaringoasaringo
提出日時 2022-01-22 03:13:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 99 ms / 2,000 ms
コード長 1,336 bytes
コンパイル時間 1,808 ms
コンパイル使用メモリ 167,232 KB
実行使用メモリ 17,824 KB
最終ジャッジ日時 2023-08-17 15:18:09
合計ジャッジ時間 5,170 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,916 KB
testcase_01 AC 4 ms
5,784 KB
testcase_02 AC 3 ms
5,800 KB
testcase_03 AC 3 ms
5,700 KB
testcase_04 AC 3 ms
5,848 KB
testcase_05 AC 3 ms
5,696 KB
testcase_06 AC 3 ms
5,720 KB
testcase_07 AC 82 ms
9,604 KB
testcase_08 AC 40 ms
7,580 KB
testcase_09 AC 79 ms
9,368 KB
testcase_10 AC 45 ms
7,860 KB
testcase_11 AC 60 ms
8,460 KB
testcase_12 AC 96 ms
10,284 KB
testcase_13 AC 97 ms
10,324 KB
testcase_14 AC 97 ms
10,092 KB
testcase_15 AC 98 ms
10,132 KB
testcase_16 AC 98 ms
10,136 KB
testcase_17 AC 98 ms
10,156 KB
testcase_18 AC 96 ms
10,156 KB
testcase_19 AC 95 ms
10,328 KB
testcase_20 AC 99 ms
10,188 KB
testcase_21 AC 97 ms
10,144 KB
testcase_22 AC 62 ms
10,168 KB
testcase_23 AC 62 ms
10,308 KB
testcase_24 AC 84 ms
17,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef long double ld ;
typedef pair<ll,ll> P ;
typedef tuple<bool,ll,ll,ll> TP ;
#define chmin(a,b) a = min(a,b)
#define chmax(a,b) a = max(a,b)
#define bit_count(x) __builtin_popcountll(x)
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) a / gcd(a,b) * b
#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define rrep(i,a,b) for(int i = a ; i < b ; i++)
#define endl "\n"

int n ;
vector<int> G[101010] ;
int dp[101010][2] ;
int ep[101010] ;

void dfs(int v , int prev){
    int dir = 0 , rev = 0 ;
    for(int i = 0 ; i < G[v].size() ; i++){
        int u = G[v][i] ;
        if(u == prev) continue ;
        dfs(u,v) ;
        dir += dp[u][0] ;
        rev += dp[u][1] ;
        if(v < u) dir++ ;
        if(v > u) rev++ ;
    }
    dp[v][0] = dir ;
    dp[v][1] = rev ;
}

void rec(int v , int prev){
    if(prev == -1) ep[v] = dp[v][1] ;
    else ep[v] = ep[prev] - (int)(prev > v) + (int)(prev < v) ;
    for(int i = 0 ; i < G[v].size() ; i++){
        int u = G[v][i] ;
        if(u == prev) continue ;
        rec(u,v) ;
    }
}

int main(){
    cin >> n ;
    rep(i,n-1){
        int a ,b ;
        cin >> a >> b ;
        a-- ; b-- ;
        G[a].push_back(b) ;
        G[b].push_back(a) ;
    }
    dfs(0,-1) ; rec(0,-1) ;
    rep(i,n) cout << ep[i] << endl ;
}
0