結果

問題 No.1103 Directed Length Sum
ユーザー chocoruskchocorusk
提出日時 2020-07-03 21:38:19
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 987 ms / 3,000 ms
コード長 1,324 bytes
コンパイル時間 1,441 ms
コンパイル使用メモリ 132,236 KB
実行使用メモリ 148,156 KB
最終ジャッジ日時 2023-10-16 23:39:11
合計ジャッジ時間 11,735 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
29,980 KB
testcase_01 AC 11 ms
29,980 KB
testcase_02 AC 767 ms
148,156 KB
testcase_03 AC 487 ms
42,588 KB
testcase_04 AC 560 ms
46,752 KB
testcase_05 AC 987 ms
54,604 KB
testcase_06 AC 351 ms
43,576 KB
testcase_07 AC 76 ms
32,092 KB
testcase_08 AC 118 ms
33,208 KB
testcase_09 AC 52 ms
31,312 KB
testcase_10 AC 169 ms
36,400 KB
testcase_11 AC 614 ms
47,588 KB
testcase_12 AC 362 ms
43,360 KB
testcase_13 AC 179 ms
36,508 KB
testcase_14 AC 40 ms
30,980 KB
testcase_15 AC 280 ms
42,320 KB
testcase_16 AC 703 ms
48,804 KB
testcase_17 AC 728 ms
50,388 KB
testcase_18 AC 168 ms
36,376 KB
testcase_19 AC 636 ms
48,568 KB
testcase_20 AC 61 ms
31,592 KB
testcase_21 AC 111 ms
32,904 KB
testcase_22 AC 505 ms
46,244 KB
testcase_23 AC 300 ms
42,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD=1e9+7;
ll powmod(ll a, ll k){
    ll ap=a, ans=1;
    while(k){
        if(k&1){
            ans*=ap;
            ans%=MOD;
        }
        ap=ap*ap;
        ap%=MOD;
        k>>=1;
    }
    return ans;
}
ll inv(ll a){
    return powmod(a, MOD-2);
}
vector<int> g[1000010];
int deg[1000010];
int d[1000010], cnt[1000010];
ll ans;
void dfs(int x, int p){
    cnt[x]=1;
    for(auto y:g[x]){
        d[y]=d[x]+1;
        dfs(y, x);
        cnt[x]+=cnt[y];
        (ans+=(ll)(d[x]+1)*cnt[y])%=MOD;
    }
}
int main()
{
	int n; cin>>n;
    for(int i=0; i<n-1; i++){
        int a, b; cin>>a>>b;
        a--; b--;
        g[a].push_back(b);
        deg[b]++;
    }
    int r=0;
    for(int i=0; i<n; i++) if(deg[i]==0) r=i;
    dfs(r, -1);
    cout<<ans<<endl;
	return 0;
}
0