結果

問題 No.1103 Directed Length Sum
ユーザー tokusakuraitokusakurai
提出日時 2020-07-03 21:47:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,377 bytes
コンパイル時間 1,570 ms
コンパイル使用メモリ 172,396 KB
実行使用メモリ 198,936 KB
最終ジャッジ日時 2023-10-17 00:30:08
合計ジャッジ時間 12,525 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
51,556 KB
testcase_01 AC 16 ms
51,556 KB
testcase_02 WA -
testcase_03 AC 561 ms
93,108 KB
testcase_04 AC 603 ms
83,012 KB
testcase_05 AC 1,100 ms
104,580 KB
testcase_06 AC 377 ms
73,680 KB
testcase_07 AC 84 ms
58,452 KB
testcase_08 AC 124 ms
60,656 KB
testcase_09 AC 59 ms
54,620 KB
testcase_10 AC 171 ms
63,012 KB
testcase_11 AC 688 ms
87,484 KB
testcase_12 AC 374 ms
73,860 KB
testcase_13 AC 185 ms
63,264 KB
testcase_14 AC 48 ms
53,860 KB
testcase_15 AC 276 ms
67,944 KB
testcase_16 AC 753 ms
91,076 KB
testcase_17 AC 784 ms
92,504 KB
testcase_18 AC 169 ms
62,964 KB
testcase_19 AC 676 ms
88,208 KB
testcase_20 AC 67 ms
55,268 KB
testcase_21 AC 112 ms
60,052 KB
testcase_22 AC 546 ms
80,688 KB
testcase_23 AC 316 ms
68,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const ld EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

const int MAX_V = 1e6;
vector<int> es[MAX_V], rs[MAX_V];
ll subtree_size[MAX_V];
ll ans = 0;

ll dfs(int now){
    ll ret = 0, s = 1;
    for(auto &e: es[now]){
        ret += dfs(e)+subtree_size[e];
        s += subtree_size[e];
    }
    subtree_size[now] = s;
    ans += ret;
    return ret;
}

int main(){
    int N;
    cin >> N;
    rep(i, N-1){
        int A, B;
        cin >> A >> B;
        A--, B--;
        es[A].pb(B), rs[B].pb(A);
    }
    int root = -1;
    rep(i, N) if(rs[i].empty()) root = i;
    dfs(root);
    cout << ans << endl;
}
0