結果

問題 No.1103 Directed Length Sum
ユーザー HaaHaa
提出日時 2020-07-03 22:31:27
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,001 ms / 3,000 ms
コード長 711 bytes
コンパイル時間 1,715 ms
コンパイル使用メモリ 175,124 KB
実行使用メモリ 107,480 KB
最終ジャッジ日時 2023-10-17 05:29:49
合計ジャッジ時間 11,903 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
29,044 KB
testcase_01 AC 14 ms
29,044 KB
testcase_02 AC 744 ms
107,480 KB
testcase_03 AC 494 ms
37,780 KB
testcase_04 AC 557 ms
39,896 KB
testcase_05 AC 1,001 ms
47,552 KB
testcase_06 AC 355 ms
36,200 KB
testcase_07 AC 81 ms
30,920 KB
testcase_08 AC 124 ms
31,976 KB
testcase_09 AC 55 ms
30,392 KB
testcase_10 AC 173 ms
32,768 KB
testcase_11 AC 614 ms
40,952 KB
testcase_12 AC 361 ms
36,200 KB
testcase_13 AC 179 ms
33,032 KB
testcase_14 AC 43 ms
30,128 KB
testcase_15 AC 277 ms
34,880 KB
testcase_16 AC 691 ms
42,272 KB
testcase_17 AC 720 ms
42,800 KB
testcase_18 AC 174 ms
32,768 KB
testcase_19 AC 632 ms
41,216 KB
testcase_20 AC 64 ms
30,656 KB
testcase_21 AC 115 ms
31,712 KB
testcase_22 AC 508 ms
39,104 KB
testcase_23 AC 299 ms
35,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 1e18;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

VVI edge(1114514,VI(0));
ll ans=0;

P dfs(int v){
	ll sum=0, esum=0;
	for(auto to:edge[v]){
		P x=dfs(to);
		esum=(esum+x.second)%MOD;
		sum=(sum+x.first+x.second)%MOD;
	}
	ans=(ans+sum)%MOD;
	return  {sum,esum+1};
}

int main(){
	int n; cin >> n;
	int a, b;
	vector<bool> root(n,1);
	REP(i,n-1){
		cin >> a >> b;
		a--; b--;
		root[b]=0;
		edge[a].push_back(b);
	}
	REP(i,n){
		if(root[i])
			dfs(i);
	}
	cout << ans << endl;
	return 0;
}
0