結果

問題 No.1769 Don't Stop the Game
ユーザー 👑 NachiaNachia
提出日時 2021-11-27 14:15:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 333 ms / 3,000 ms
コード長 1,375 bytes
コンパイル時間 986 ms
コンパイル使用メモリ 90,204 KB
実行使用メモリ 33,776 KB
最終ジャッジ日時 2023-09-12 16:35:57
合計ジャッジ時間 8,515 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 197 ms
18,628 KB
testcase_09 AC 149 ms
14,048 KB
testcase_10 AC 222 ms
17,884 KB
testcase_11 AC 135 ms
14,492 KB
testcase_12 AC 227 ms
22,016 KB
testcase_13 AC 241 ms
22,380 KB
testcase_14 AC 249 ms
22,200 KB
testcase_15 AC 251 ms
22,400 KB
testcase_16 AC 281 ms
22,204 KB
testcase_17 AC 304 ms
22,392 KB
testcase_18 AC 313 ms
22,540 KB
testcase_19 AC 323 ms
22,224 KB
testcase_20 AC 322 ms
22,452 KB
testcase_21 AC 333 ms
22,400 KB
testcase_22 AC 329 ms
22,320 KB
testcase_23 AC 234 ms
22,276 KB
testcase_24 AC 240 ms
22,248 KB
testcase_25 AC 146 ms
22,828 KB
testcase_26 AC 230 ms
22,836 KB
testcase_27 AC 196 ms
33,656 KB
testcase_28 AC 275 ms
33,776 KB
testcase_29 AC 270 ms
29,000 KB
testcase_30 AC 273 ms
29,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)

int N;
vector<vector<pair<int,int>>> E;
vector<int> P,Z,H,SH,black_stack,prev_block,block_size,I;

void dfs(int p = 0, int pre = -1){

	int h = H[p];
	int prev_black_p = black_stack[h];
	block_size[p] = Z[p];
	prev_block[p] = prev_black_p;

	block_size[prev_black_p] -= Z[p];

	for(auto [e,d] : E[p]) if(e != pre){
		black_stack[h] = e;
		dfs(e,p);
		black_stack[h] = prev_black_p;
	}
}


int main(){
	cin >> N;
	E.resize(N);
	rep(i,N-1){
		int u,v,c; cin >> u >> v >> c; u--; v--;
		E[u].push_back({ v,c });
		E[v].push_back({ u,c });
	}

	I.push_back(0);
	P.assign(N,-1);
	H.assign(N,0);
	rep(i,I.size()){
		int p = I[i];
		for(auto [e,h] : E[p]){
			if(P[p] == e) continue;
			I.push_back(e);
			P[e] = p;
			H[e] = H[p] ^ h;
		}
	}
	Z.assign(N,1);
	for(int i=N-1; i>=1; i--) Z[P[I[i]]] += Z[I[i]];
	
	SH = H;
	sort(SH.begin(), SH.end());
	SH.erase(unique(SH.begin(),SH.end()),SH.end());
	for(int& h : H) h = lower_bound(SH.begin(),SH.end(),h) - SH.begin();
	
	black_stack.assign(N,0);
	rep(i,N) black_stack[i] = N + i;
	prev_block.assign(N,0);
	rep(i,N) prev_block[i] = N + H[i];
	block_size.assign(2*N,N);

	dfs();
	
	long long ans = 0;
	for(int p=1; p<N; p++) ans += block_size[p] + block_size[prev_block[p]];
	cout << ans << endl;

	return 0;
}
0