結果

問題 No.1103 Directed Length Sum
ユーザー kotatsugamekotatsugame
提出日時 2020-07-03 21:57:25
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,019 ms / 3,000 ms
コード長 527 bytes
コンパイル時間 774 ms
コンパイル使用メモリ 71,128 KB
実行使用メモリ 126,032 KB
最終ジャッジ日時 2023-10-17 02:09:29
合計ジャッジ時間 10,937 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
29,604 KB
testcase_01 AC 10 ms
29,604 KB
testcase_02 AC 753 ms
126,032 KB
testcase_03 AC 487 ms
36,888 KB
testcase_04 AC 574 ms
41,420 KB
testcase_05 AC 1,019 ms
48,088 KB
testcase_06 AC 357 ms
38,244 KB
testcase_07 AC 79 ms
31,372 KB
testcase_08 AC 117 ms
32,304 KB
testcase_09 AC 50 ms
30,720 KB
testcase_10 AC 163 ms
35,304 KB
testcase_11 AC 613 ms
42,256 KB
testcase_12 AC 358 ms
38,308 KB
testcase_13 AC 173 ms
35,388 KB
testcase_14 AC 41 ms
30,440 KB
testcase_15 AC 273 ms
36,988 KB
testcase_16 AC 702 ms
43,472 KB
testcase_17 AC 735 ms
43,968 KB
testcase_18 AC 172 ms
35,284 KB
testcase_19 AC 635 ms
42,500 KB
testcase_20 AC 61 ms
30,952 KB
testcase_21 AC 107 ms
32,052 KB
testcase_22 AC 519 ms
40,628 KB
testcase_23 AC 293 ms
37,296 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:24:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   24 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;
int N;
vector<int>G[1<<20];
const long mod=1e9+7;
long ans;
pair<int,long>dfs(int u,int p)
{
	int c=0;
	long t=0;
	for(int v:G[u])if(v!=p)
	{
		pair<int,long>q=dfs(v,u);
		c+=q.first;
		t+=q.second;
		t%=mod;
	}
	ans+=c+t;
	ans%=mod;
	return make_pair(c+1,(c+t)%mod);
}
int deg[1<<20];
main()
{
	cin>>N;
	for(int i=1;i<N;i++)
	{
		int u,v;cin>>u>>v;
		u--,v--;
		G[u].push_back(v);
		deg[v]++;
	}
	int root=0;
	while(deg[root])root++;
	dfs(root,-1);
	cout<<ans<<endl;
}
0