結果

問題 No.1103 Directed Length Sum
ユーザー kotatsugamekotatsugame
提出日時 2020-07-03 21:57:25
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 911 ms / 3,000 ms
コード長 527 bytes
コンパイル時間 695 ms
コンパイル使用メモリ 72,088 KB
実行使用メモリ 125,988 KB
最終ジャッジ日時 2024-09-17 00:43:42
合計ジャッジ時間 9,712 ms
ジャッジサーバーID
(参考情報)
judge6 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 22
権限があれば一括ダウンロードができます
コンパイルメッセージ
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