結果

問題 No.1103 Directed Length Sum
ユーザー けーむけーむ
提出日時 2020-07-20 14:41:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,088 bytes
コンパイル時間 1,689 ms
コンパイル使用メモリ 170,480 KB
実行使用メモリ 159,268 KB
最終ジャッジ日時 2023-08-25 10:33:49
合計ジャッジ時間 12,909 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 WA -
testcase_03 AC 249 ms
89,032 KB
testcase_04 AC 471 ms
57,412 KB
testcase_05 AC 866 ms
97,316 KB
testcase_06 AC 283 ms
38,652 KB
testcase_07 AC 48 ms
11,556 KB
testcase_08 AC 76 ms
16,048 KB
testcase_09 AC 27 ms
8,444 KB
testcase_10 AC 123 ms
20,804 KB
testcase_11 AC 521 ms
62,660 KB
testcase_12 AC 295 ms
38,796 KB
testcase_13 AC 128 ms
21,384 KB
testcase_14 AC 21 ms
6,928 KB
testcase_15 AC 214 ms
30,944 KB
testcase_16 AC 602 ms
69,876 KB
testcase_17 AC 627 ms
72,780 KB
testcase_18 AC 121 ms
20,780 KB
testcase_19 AC 548 ms
64,048 KB
testcase_20 AC 34 ms
9,704 KB
testcase_21 AC 79 ms
14,800 KB
testcase_22 AC 419 ms
52,844 KB
testcase_23 AC 241 ms
32,848 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"

vector<vector<ll>> g, rev;
ll ans = 0;

ll dfs(ll v, ll p = 1) {
    ll cnt = 0;
    for(auto &x : g[v]) {
        cnt += dfs(x, p + 1);
    }
    ans += p * cnt;
    return cnt + 1;
}

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