結果

問題 No.1098 LCAs
ユーザー けーむけーむ
提出日時 2020-07-28 10:37:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,299 bytes
コンパイル時間 1,914 ms
コンパイル使用メモリ 172,052 KB
実行使用メモリ 25,980 KB
最終ジャッジ日時 2023-09-11 06:21:36
合計ジャッジ時間 5,751 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 WA -
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 WA -
testcase_11 AC 2 ms
4,376 KB
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 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 218 ms
25,208 KB
testcase_29 AC 221 ms
25,980 KB
testcase_30 AC 222 ms
25,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"

ll n;
vector<vector<ll>> g;
vector<ll> dp, ans;

ll dfs(ll v, ll p = -1) {
    dp[v] = 1;
    for(auto &u : g[v]) {
        if (u == p) continue;
        dp[v] += dfs(u, v);
    }
    return dp[v];
}

ll dfs2(ll v, ll p = -1) {
    ans[v] = 1;
    for(auto &u : g[v]) {
        if (u == p) continue;
        ans[v] *= dp[u] + 1;
        dfs2(u, v);
    }
    ans[v] = ans[v] * 2 - 1;
    return ans[v];
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    cin >> n;
    g.resize(n);
    rep(i, n - 1) {
        ll v, w;
        cin >> v >> w;
        v--; w--;
        g[v].push_back(w);
        g[w].push_back(v);
    }
    dp.resize(n, 0);
    dfs(0);
    ans.resize(n, 0);
    dfs2(0);
    rep(i, n) cout << ans[i] << endl;
    return 0;
}
0