結果

問題 No.1103 Directed Length Sum
ユーザー hanbei_dayohanbei_dayo
提出日時 2020-07-16 13:35:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,349 ms / 3,000 ms
コード長 1,613 bytes
コンパイル時間 1,218 ms
コンパイル使用メモリ 101,036 KB
実行使用メモリ 159,644 KB
最終ジャッジ日時 2023-08-16 05:09:47
合計ジャッジ時間 16,637 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
32,028 KB
testcase_01 AC 12 ms
32,124 KB
testcase_02 AC 780 ms
159,644 KB
testcase_03 AC 536 ms
85,168 KB
testcase_04 AC 749 ms
60,192 KB
testcase_05 AC 1,349 ms
81,436 KB
testcase_06 AC 478 ms
51,972 KB
testcase_07 AC 100 ms
35,496 KB
testcase_08 AC 161 ms
37,516 KB
testcase_09 AC 62 ms
34,184 KB
testcase_10 AC 219 ms
39,356 KB
testcase_11 AC 829 ms
65,192 KB
testcase_12 AC 480 ms
52,284 KB
testcase_13 AC 227 ms
39,764 KB
testcase_14 AC 48 ms
33,648 KB
testcase_15 AC 363 ms
49,612 KB
testcase_16 AC 940 ms
68,248 KB
testcase_17 AC 974 ms
68,940 KB
testcase_18 AC 218 ms
39,024 KB
testcase_19 AC 848 ms
63,568 KB
testcase_20 AC 76 ms
32,260 KB
testcase_21 AC 140 ms
35,452 KB
testcase_22 AC 682 ms
57,192 KB
testcase_23 AC 388 ms
45,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<functional>
#include<math.h>
#include<random>
#include <bitset>
using namespace std;
#define N (1000000000+7)
//#define N 998244353
#define INF 1e16
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> Q;

const int inf = (int)1e9; 

ll gcd(ll a, ll b) {
	if (b > a) {
		ll tmp = b;
		b = a;
		a = tmp;
	}
	if (a%b == 0)return b;
	else return gcd(b, a%b);
}

ll path[1000010];
ll vertex[1000010];


vector<int>g[1000010];
ll num[1000010];

void rec(ll now,ll root){
    if(path[now]!=-1 && vertex[now]!=-1){
        return;
    }
    if(root!=-1 && g[now].size()==1){
        path[now]=0;
        vertex[now]=1;
        return;
    }
    ll v = 0,p = 0;
    for(int i=0;i<g[now].size();i++){
        int next = g[now][i];
        if(root == next)continue;
        rec(next,now);
        v = (v+vertex[next])%N;
        p = (p+path[next]+vertex[next])%N;
    }
    vertex[now]=(v+1)%N;
    path[now]=p%N;
    return;
}

int main(void){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        path[i]=-1;
        vertex[i]=-1;
    }
    for(int i=0;i<n-1;i++){
        int a,b;
        cin>>a>>b;
        g[a].push_back(b);
        g[b].push_back(a);
        num[b]++;
    }
    ll start = 0;
    for(int i=1;i<=n;i++){
        if(num[i]==0)start=i;
    }
    rec(start,-1);
    //for(int i=1;i<=n;i++)cout<<path[i]<<" "<<vertex[i]<<endl;
    ll ans = 0;
    for(int i=1;i<=n;i++)ans = (ans+path[i])%N;
    cout<<ans<<endl;
    return 0;
}
0