結果

問題 No.1769 Don't Stop the Game
ユーザー kotatsugamekotatsugame
提出日時 2021-11-26 23:39:04
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 766 ms / 3,000 ms
コード長 954 bytes
コンパイル時間 811 ms
コンパイル使用メモリ 85,420 KB
実行使用メモリ 65,224 KB
最終ジャッジ日時 2024-06-29 18:37:02
合計ジャッジ時間 13,545 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:43:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   43 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<vector>
#include<map>
using namespace std;
int N;
vector<pair<int,int> >G[2<<17];
int ch[2<<17],Xr[2<<17];
int add[2<<17];
map<int,int>ret;
int dfs(int u,int p,int xr)
{
	Xr[u]=xr;
	ch[u]=1;
	map<int,int>now;
	for(pair<int,int>&e:G[u])
	{
		int v=e.first;
		if(v==p)continue;
		ch[u]+=dfs(v,u,xr^e.second);
		e.second=ret[Xr[u]];
		if(now.size()<ret.size())now.swap(ret);
		for(pair<int,int>p:ret)now[p.first]+=p.second;
	}
	ret.swap(now);
	add[u]=ch[u]-ret[Xr[u]];
	ret[Xr[u]]=ch[u];
	return ch[u];
}
long ans;
void dfs2(int u,int p)
{
	int tmp=ret[Xr[u]];
	ans-=tmp-add[u];
	for(pair<int,int>e:G[u])
	{
		int v=e.first;
		if(v==p)continue;
		ret[Xr[u]]=e.second+N-ch[v];
		dfs2(v,u);
	}
	ret[Xr[u]]=tmp;
}
main()
{
	cin>>N;
	for(int i=1;i<N;i++)
	{
		int a,b,c;cin>>a>>b>>c;
		a--,b--;
		G[a].push_back(make_pair(b,c));
		G[b].push_back(make_pair(a,c));
	}
	dfs(0,-1,0);
	ans=(long)N*(N-1);
	dfs2(0,-1);
	cout<<ans<<endl;
}
0