結果

問題 No.1103 Directed Length Sum
ユーザー dekomori_sanaedekomori_sanae
提出日時 2022-02-19 17:35:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,472 ms / 3,000 ms
コード長 1,691 bytes
コンパイル時間 860 ms
コンパイル使用メモリ 90,252 KB
実行使用メモリ 159,296 KB
最終ジャッジ日時 2023-09-11 20:45:44
合計ジャッジ時間 16,018 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 794 ms
159,296 KB
testcase_03 AC 552 ms
88,988 KB
testcase_04 AC 822 ms
50,464 KB
testcase_05 AC 1,472 ms
84,924 KB
testcase_06 AC 507 ms
33,952 KB
testcase_07 AC 91 ms
10,524 KB
testcase_08 AC 153 ms
14,348 KB
testcase_09 AC 51 ms
7,728 KB
testcase_10 AC 226 ms
18,488 KB
testcase_11 AC 914 ms
54,588 KB
testcase_12 AC 516 ms
34,160 KB
testcase_13 AC 231 ms
19,188 KB
testcase_14 AC 36 ms
6,464 KB
testcase_15 AC 377 ms
27,440 KB
testcase_16 AC 1,019 ms
61,152 KB
testcase_17 AC 1,058 ms
63,848 KB
testcase_18 AC 216 ms
18,684 KB
testcase_19 AC 916 ms
56,004 KB
testcase_20 AC 59 ms
8,708 KB
testcase_21 AC 121 ms
13,288 KB
testcase_22 AC 730 ms
46,380 KB
testcase_23 AC 414 ms
29,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <numeric>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;

vvl G;
vl sz;  // sz[v] : vの部分木のサイズ

void dfs1(ll v, ll p = -1) {
    for(ll u : G[v]) {
        if(u == p) { continue; }
        dfs1(u, v);
        sz[v] += sz[u];
    }
}

vl dp;

void dfs2(ll v, ll p = -1) {
    dp[v] = sz[v] - 1;
    for(ll u : G[v]) {
        if(u == p) { continue; }
        dfs2(u, v);
        dp[v] += dp[u];
        dp[v] %= mod;
    }
}

int main() {
    ll n;
    cin >> n;    

    vl indeg(n);
    G.resize(n);
    rep(i, n-1) {
        ll a, b;
        cin >> a >> b;
        a--;  b--;
        G[a].pb(b);
        G[b].pb(a);
        indeg[b]++;
    }

    ll root = -1;
    rep(v, n) {
        if(indeg[v] == 0) {
            root = v;
            break;
        }
    }

    sz.assign(n, 1);
    dfs1(root);
    
    dp.resize(n);
    dfs2(root);

    ll ans = 0;
    rep(v, n) {
        ans += dp[v];
        ans %= mod;
    }

    out(ans);
    re0;
}
0