結果

問題 No.1103 Directed Length Sum
ユーザー misora192misora192
提出日時 2020-07-23 08:35:22
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 720 ms / 3,000 ms
コード長 976 bytes
コンパイル時間 1,729 ms
コンパイル使用メモリ 170,960 KB
実行使用メモリ 179,068 KB
最終ジャッジ日時 2023-09-05 21:12:57
合計ジャッジ時間 11,202 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
27,032 KB
testcase_01 AC 12 ms
27,104 KB
testcase_02 AC 386 ms
179,068 KB
testcase_03 AC 220 ms
57,944 KB
testcase_04 AC 409 ms
51,240 KB
testcase_05 AC 720 ms
69,140 KB
testcase_06 AC 262 ms
42,744 KB
testcase_07 AC 59 ms
30,716 KB
testcase_08 AC 95 ms
32,404 KB
testcase_09 AC 38 ms
29,360 KB
testcase_10 AC 128 ms
34,844 KB
testcase_11 AC 441 ms
53,548 KB
testcase_12 AC 266 ms
43,024 KB
testcase_13 AC 134 ms
35,108 KB
testcase_14 AC 31 ms
28,424 KB
testcase_15 AC 211 ms
39,304 KB
testcase_16 AC 506 ms
56,612 KB
testcase_17 AC 528 ms
58,196 KB
testcase_18 AC 126 ms
34,816 KB
testcase_19 AC 457 ms
54,024 KB
testcase_20 AC 44 ms
29,620 KB
testcase_21 AC 84 ms
31,992 KB
testcase_22 AC 358 ms
49,212 KB
testcase_23 AC 220 ms
40,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=(0);i<(n);i++)

using namespace std;

typedef long long ll;

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

const int max_n = 1010101;
ll MOD = 1e9 + 7;
int n;
vector<int> par;
vector<int> g[max_n];

vector<ll> dp1, dp2, dp3;

void dfs(int p){
	dp2[p]++;
	dp2[p] %= MOD;

	for(int u : g[p]){
		dfs(u);
		dp1[p] += dp1[u] + dp2[u] + dp3[u];
		dp1[p] %= MOD;

		dp2[p] += dp2[u];
		dp2[p] %= MOD;

		dp3[p] += dp2[u] + dp3[u];
		dp3[p] %= MOD;
	}
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	cin >> n;
	par.resize(n, -1);
	rep(i, n-1){
		int a, b;
		cin >> a >> b;
		a--; b--;
		par[b] = a;
		g[a].push_back(b);
	}

	int root = -1;
	rep(i, n){
		if(par[i] == -1) root = i;
	}
	
	dp1.resize(n, 0);
	dp2.resize(n, 0);
	dp3.resize(n, 0);

	dfs(root);
	cout << dp1[root] << endl;
}	
0