結果

問題 No.1790 Subtree Deletion
ユーザー laneguelanegue
提出日時 2021-12-24 15:20:03
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 1,696 bytes
コンパイル時間 3,475 ms
コンパイル使用メモリ 229,108 KB
実行使用メモリ 88,888 KB
最終ジャッジ日時 2023-09-04 15:32:39
合計ジャッジ時間 22,125 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 102 ms
4,480 KB
testcase_09 AC 422 ms
53,092 KB
testcase_10 AC 1,805 ms
86,300 KB
testcase_11 AC 1,817 ms
88,148 KB
testcase_12 AC 562 ms
62,008 KB
testcase_13 AC 977 ms
65,044 KB
testcase_14 AC 294 ms
23,988 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

struct Node{
	long value;
	long parent = -1;
	long edge_value;
	long[] children;
}
void main(){
	auto N = readln.chomp.to!int;
	auto edge = new long[][][N];
	for(auto n = 0; n < N - 1; n++){
		auto input = readln.chomp.split(" ").to!(long[]);
		auto l = input[0] - 1;
		auto r = input[1] - 1;
		auto a = input[2];
		edge[l] ~= [r, a];
		edge[r] ~= [l, a];
	}
	//stderr.writeln(edge);
	auto nodes = new Node[N];
	auto stack = DList!long([0]);
	while(!stack.empty){
		auto node = stack.back;
		if(nodes[node].children.length == 0){
			foreach(e; edge[node]){
				if(e[0] == nodes[node].parent) continue;
				nodes[e[0]].parent = node;
				nodes[e[0]].edge_value = e[1];
				nodes[node].children ~= e[0];
				stack.insertBack(e[0]);
			}
			if(nodes[node].children.length == 0){
				stack.removeBack;
			}
		}else{
			foreach(c; nodes[node].children){
				nodes[node].value ^= nodes[c].value;
				nodes[node].value ^= nodes[c].edge_value;
			}
			stack.removeBack;
		}
	}
	//stderr.writeln(nodes);
	auto Q = readln.chomp.to!int;
	for(auto q = 0; q < Q; q++){
		auto input = readln.chomp.split(" ").to!(long[]);
		auto t = input[0];
		auto x = input[1] - 1;
		if(t == 1){
			auto parent = nodes[x].parent;
			auto xor = nodes[x].value ^ nodes[x].edge_value;
			while(parent >= 0){
				nodes[parent].value ^= xor;
				parent = nodes[parent].parent;
			}
			nodes[x].parent = -1;
			auto queue = DList!long([x]);
			while(!queue.empty){
				auto node = queue.front;
				queue.removeFront;
				nodes[node].value = 0;
				foreach(c; nodes[node].children){
					queue.insertBack(c);
				}
			}
		}else{
			writeln(nodes[x].value);
		}
	//stderr.writeln(nodes);
	}
	//stderr.writeln(nodes);
}
0