結果

問題 No.1103 Directed Length Sum
ユーザー kotatsugame
提出日時 2020-07-03 21:54:50
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 478 bytes
コンパイル時間 547 ms
コンパイル使用メモリ 71,864 KB
実行使用メモリ 125,960 KB
最終ジャッジ日時 2024-09-17 00:37:01
合計ジャッジ時間 9,258 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 21 WA * 1
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:21:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   21 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;
int N;
vector<int>G[1<<20];
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;
	}
	ans+=c+t;
	return make_pair(c+1,c+t);
}
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