結果

問題 No.1790 Subtree Deletion
ユーザー laneguelanegue
提出日時 2021-12-24 15:30:39
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 477 ms / 3,000 ms
コード長 1,695 bytes
コンパイル時間 2,252 ms
コンパイル使用メモリ 230,736 KB
実行使用メモリ 55,940 KB
最終ジャッジ日時 2023-09-04 15:33:01
合計ジャッジ時間 8,491 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 455 ms
55,940 KB
testcase_04 AC 466 ms
55,060 KB
testcase_05 AC 463 ms
55,076 KB
testcase_06 AC 477 ms
55,104 KB
testcase_07 AC 472 ms
55,036 KB
testcase_08 AC 103 ms
4,524 KB
testcase_09 AC 436 ms
50,856 KB
testcase_10 AC 473 ms
55,060 KB
testcase_11 AC 473 ms
55,072 KB
testcase_12 AC 353 ms
41,584 KB
testcase_13 AC 353 ms
44,000 KB
testcase_14 AC 116 ms
23,672 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;
				foreach(c; nodes[node].children){
					queue.insertBack(c);
				}
				nodes[node] = Node();
			}
		}else{
			writeln(nodes[x].value);
		}
	//stderr.writeln(nodes);
	}
	//stderr.writeln(nodes);
}
0