結果

問題 No.1103 Directed Length Sum
ユーザー fastmathfastmath
提出日時 2020-07-03 22:10:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 557 ms / 3,000 ms
コード長 1,741 bytes
コンパイル時間 1,582 ms
コンパイル使用メモリ 170,656 KB
実行使用メモリ 153,004 KB
最終ジャッジ日時 2023-10-17 03:49:15
合計ジャッジ時間 8,027 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
28,836 KB
testcase_01 AC 9 ms
28,836 KB
testcase_02 AC 320 ms
153,004 KB
testcase_03 AC 177 ms
51,300 KB
testcase_04 AC 278 ms
47,316 KB
testcase_05 AC 557 ms
61,436 KB
testcase_06 AC 169 ms
40,568 KB
testcase_07 AC 39 ms
31,176 KB
testcase_08 AC 58 ms
32,628 KB
testcase_09 AC 27 ms
30,316 KB
testcase_10 AC 75 ms
34,336 KB
testcase_11 AC 302 ms
49,072 KB
testcase_12 AC 163 ms
40,696 KB
testcase_13 AC 81 ms
34,520 KB
testcase_14 AC 23 ms
29,948 KB
testcase_15 AC 128 ms
37,900 KB
testcase_16 AC 380 ms
51,676 KB
testcase_17 AC 395 ms
52,704 KB
testcase_18 AC 78 ms
34,292 KB
testcase_19 AC 326 ms
49,592 KB
testcase_20 AC 32 ms
30,620 KB
testcase_21 AC 49 ms
32,188 KB
testcase_22 AC 248 ms
45,636 KB
testcase_23 AC 131 ms
38,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cout << #x << ": " << x << '\n';

const int N = 1e6+7, MOD = 1000 * 1000 * 1000 + 7;

int mod(int n) {
    n %= MOD;
    if (n < 0) return n + MOD;
    else return n;
}   
int fp(int a, int p) {
    int ans = 1, c = a;
    for (int i = 0; (1ll << i) <= p; ++i) {
        if ((p >> i) & 1) ans = mod(ans * c);
        c = mod(c * c);
    }   
    return ans;
}   
int dv(int a, int b) { return mod(a * fp(b, MOD - 2)); }


int cnt[N], dist[N];
bool used[N];
vector <int> g[N];
void dfs(int u) {
    used[u] = 1;
    for (int v : g[u]) {
        if (!used[v]) {
            dfs(v);
        }   
        cnt[u] += cnt[v];
        dist[u] += dist[v];
        cnt[u] = mod(cnt[u]);
        dist[u] = mod(dist[u]);
    }   
    dist[u] += cnt[u];
    ++cnt[u];
        cnt[u] = mod(cnt[u]);
        dist[u] = mod(dist[u]);
}   

signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif

    int n;
    cin >> n;
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        g[u].app(v);
    }   

    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        if (!used[i]) {
            dfs(i);
        }    
    }   
    for (int i = 1; i <= n; ++i)
        ans += dist[i];
    ans = mod(ans);
    cout << ans << endl;
}
0