106512024-04-07 18:50:25111XORfa visszatércpp17Runtime error 28/1001.582s254332 KiB
#include<bits/stdc++.h>
using namespace std;

#define int long long

struct Trie{
	vector<array<int,2>>t;
	vector<set<int>>v;
	
	Trie():t(1),v(1){
	}
	
	void add(int n,int x){
		int y=0;
		for(int i=30;i>=0;i--){
			int b=x>>i&1;
			if(t[y][b]==0){
				t[y][b]=t.size();
				t.emplace_back();
				v.emplace_back();
			}
			y=t[y][b];
			v[y].insert(n);
		}
	}
	
	void rem(int n,int x){
		int y=0;
		for(int i=30;i>=0;i--){
			int b=x>>i&1;
			y=t[y][b];
			v[y].erase(n);
		}
	}
	
	int get(int x){
		int y=0;
		for(int i=30;i>=0;i--){
			int b=x>>i&1;
			if(t[y][b^1]&&!v[t[y][b^1]].empty()){
				y=t[y][b^1];
			}
			else{
				y=t[y][b];
			}
		}
		return *v[y].begin();
	}
};

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N,Q;
	cin>>N>>Q;
	vector<vector<pair<int,int>>>g(N+1);
	for(int i=0;i<N-1;i++){
		int a,b,w;
		cin>>a>>b>>w;
		g[a].emplace_back(b,w);
		g[b].emplace_back(a,w);
	}
	vector<int>v(N+1);
	auto dfs=[&](auto self,int i,int p,int x)->void{
		v[i]=x;
		for(auto[j,w]:g[i]){
			if(j==p){
				continue;
			}
			self(self,j,i,x^w);
		}
	};
	dfs(dfs,1,0,0);
	set<int>s;
	Trie t;
	vector<set<int>>p(N+1);
	multiset<int>ans;
	ans.insert(0);
	while(Q--){
		int x;
		cin>>x;
		if(s.count(x)){
			s.erase(x);
			t.rem(x,v[x]);
			for(int y:p[x]){
				p[y].erase(x);
				ans.erase(ans.find(v[x]^v[y]));
				if(s.size()>1){
					int z=t.get(v[y]);
					if(!p[y].count(z)){
						ans.insert(v[y]^v[z]);
						p[y].insert(z);
						p[z].insert(y);
					}
				}
			}
			p[x].clear();
		}
		else{
			if(!s.empty()){
				int y=t.get(v[x]);
				if(!p[x].count(y)){
					ans.insert(v[x]^v[y]);
					p[x].insert(y);
					p[y].insert(x);
				}
			}
			s.insert(x);
			t.add(x,v[x]);
		}
		// for(int i:ans)cout<<i<<' ';cout<<'\n';
		cout<<*--ans.end()<<'\n';
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1900 KiB
2Accepted3ms2096 KiB
subtask211/11
3Accepted3ms2748 KiB
4Accepted3ms2948 KiB
5Accepted3ms3236 KiB
6Accepted3ms3268 KiB
7Accepted3ms3288 KiB
8Accepted3ms3292 KiB
9Accepted3ms3268 KiB
10Accepted3ms3508 KiB
11Accepted4ms3940 KiB
12Accepted3ms3876 KiB
13Accepted3ms3716 KiB
subtask30/13
14Runtime error32ms16540 KiB
15Runtime error30ms16528 KiB
16Accepted449ms49748 KiB
17Accepted684ms115040 KiB
18Accepted648ms148072 KiB
19Accepted669ms164908 KiB
20Accepted569ms82892 KiB
21Accepted587ms115780 KiB
22Runtime error32ms17632 KiB
23Runtime error32ms17760 KiB
subtask417/17
24Accepted8ms7884 KiB
25Accepted8ms7396 KiB
26Accepted8ms9036 KiB
27Accepted9ms11012 KiB
28Accepted9ms11024 KiB
29Accepted8ms7948 KiB
30Accepted8ms8100 KiB
31Accepted8ms8100 KiB
32Accepted39ms8228 KiB
33Accepted41ms8380 KiB
34Accepted41ms8188 KiB
35Accepted41ms8104 KiB
subtask50/59
36Accepted559ms144272 KiB
37Accepted439ms100524 KiB
38Accepted685ms222428 KiB
39Accepted762ms232728 KiB
40Accepted795ms254332 KiB
41Accepted570ms142452 KiB
42Accepted560ms143536 KiB
43Accepted554ms142364 KiB
44Accepted551ms142432 KiB
45Accepted231ms76392 KiB
46Time limit exceeded1.552s82128 KiB
47Time limit exceeded1.582s82272 KiB
48Time limit exceeded1.559s82224 KiB
49Time limit exceeded1.554s82176 KiB