結果

問題 No.1817 Reversed Edges
ユーザー hiro71687khiro71687k
提出日時 2023-03-31 04:40:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 254 ms / 2,000 ms
コード長 1,261 bytes
コンパイル時間 5,125 ms
コンパイル使用メモリ 264,964 KB
実行使用メモリ 19,652 KB
最終ジャッジ日時 2023-10-23 18:14:07
合計ジャッジ時間 12,989 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,212 KB
testcase_01 AC 5 ms
6,212 KB
testcase_02 AC 4 ms
6,212 KB
testcase_03 AC 4 ms
6,212 KB
testcase_04 AC 4 ms
6,212 KB
testcase_05 AC 4 ms
6,212 KB
testcase_06 AC 4 ms
6,212 KB
testcase_07 AC 223 ms
11,732 KB
testcase_08 AC 102 ms
9,092 KB
testcase_09 AC 205 ms
11,468 KB
testcase_10 AC 116 ms
9,356 KB
testcase_11 AC 153 ms
10,148 KB
testcase_12 AC 254 ms
12,524 KB
testcase_13 AC 249 ms
12,524 KB
testcase_14 AC 238 ms
12,524 KB
testcase_15 AC 245 ms
12,524 KB
testcase_16 AC 246 ms
12,524 KB
testcase_17 AC 250 ms
12,524 KB
testcase_18 AC 245 ms
12,524 KB
testcase_19 AC 248 ms
12,524 KB
testcase_20 AC 246 ms
12,524 KB
testcase_21 AC 246 ms
12,524 KB
testcase_22 AC 185 ms
12,776 KB
testcase_23 AC 188 ms
12,780 KB
testcase_24 AC 211 ms
19,652 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll=long long;
using ld=long double;
ld pie=3.141592653589793;
ll mod=1000000007;
ll inf=999999999999999999;
vector<ll>dp(200000,-1);
vector<ll>ans(200000,-1);
void dfs(ll now,vector<vector<ll>>&g){
    dp[now]=0;
    for (ll i = 0; i < g[now].size(); i++)
    {
        if (dp[g[now][i]]==-1)
        {
            dfs(g[now][i],g);
            dp[now]+=dp[g[now][i]];
            if (now>g[now][i])
            {
                dp[now]+=1;
            }
        }
    }
}
void dfs2(ll now,ll oya,vector<vector<ll>>&g){
    if (now!=0)
    {
        if (now>oya)
        {
            ans[now]=ans[oya]+1;
        }else{
            ans[now]=ans[oya]-1;
        }
    }
    for (ll i = 0; i < g[now].size(); i++)
    {
        if (g[now][i]!=oya)
        {
            dfs2(g[now][i],now,g);
        }
    }
}
int main(){
    ll n;
    cin >> n;
    vector<vector<ll>>g(n);
    for (ll i = 0; i < n-1; i++)
    {
        ll a,b;
        cin >> a >> b;
        a--,b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs(0,g);
    ans[0]=dp[0];
    dfs2(0,-1,g);
    for (ll i = 0; i < n; i++)
    {
        cout << ans[i] << endl;
    }
    
}
0