106622024-04-07 19:21:37111XORfa visszatércpp17Wrong answer 0/100328ms165204 KiB
#include<bits/stdc++.h>
using namespace std;

#define int long long

struct Trie{
	vector<array<int,2>>t;
	vector<int>c;
	vector<set<int>>v;
	
	Trie():t(1),c(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();
				c.emplace_back();
				v.emplace_back();
			}
			y=t[y][b];
			c[y]++;
		}
		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];
			c[y]--;
		}
		v[y].erase(n);
	}
	
	int get(int x){
		int y=0;
		for(int i=30;i>=0;i--){
			int b=x>>i&1;
			if(c[t[y][b^1]]){
				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);
	multimap<int,pair<int,int>>ans;
	ans.emplace(0,pair<int,int>{0,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);
			}
			p[x].clear();
		}
		else{
			if(!s.empty()){
				int y=t.get(v[x]);
				if(!p[x].count(y)){
					ans.emplace(v[x]^v[y],pair<int,int>{x,y});
					p[x].insert(y);
					p[y].insert(x);
				}
			}
			s.insert(x);
			t.add(x,v[x]);
		}
		// while((--ans.end())->first!=0&&(!s.count((--ans.end())->second.first)||!s.count((--ans.end())->second.second))){
			// auto[x,y]=(--ans.end())->second;
			// ans.erase(--ans.end());
			// if(s.size()>1){
				// if(s.count(x)){
					// int z=t.get(v[x]);
					// if(!p[x].count(z)){
						// ans.emplace(v[x]^v[z],pair<int,int>{x,z});
						// p[x].insert(z);
						// p[z].insert(x);
					// }
				// }
				// if(s.count(y)){
					// int z=t.get(v[y]);
					// if(!p[y].count(z)){
						// ans.emplace(v[y]^v[z],pair<int,int>{y,z});
						// p[y].insert(z);
						// p[z].insert(y);
					// }
				// }
			// }
		// }
		// for(auto[i,_]:ans)cout<<i<<' ';cout<<endl;
		cout<<(--ans.end())->first<<'\n';
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Wrong answer3ms1896 KiB
2Accepted3ms2088 KiB
subtask20/11
3Wrong answer3ms2588 KiB
4Wrong answer3ms2552 KiB
5Wrong answer3ms2864 KiB
6Wrong answer3ms3028 KiB
7Wrong answer3ms2992 KiB
8Wrong answer3ms3240 KiB
9Wrong answer3ms3500 KiB
10Wrong answer3ms3424 KiB
11Wrong answer3ms3380 KiB
12Wrong answer3ms3692 KiB
13Wrong answer3ms3904 KiB
subtask30/13
14Wrong answer65ms18916 KiB
15Wrong answer64ms19348 KiB
16Wrong answer112ms24224 KiB
17Accepted136ms32276 KiB
18Accepted140ms37380 KiB
19Accepted152ms39460 KiB
20Wrong answer126ms28680 KiB
21Wrong answer129ms32000 KiB
22Wrong answer64ms20192 KiB
23Wrong answer67ms20260 KiB
subtask40/17
24Wrong answer6ms7448 KiB
25Wrong answer6ms7564 KiB
26Wrong answer6ms7628 KiB
27Wrong answer7ms9584 KiB
28Accepted7ms9664 KiB
29Wrong answer4ms7516 KiB
30Wrong answer6ms7504 KiB
31Wrong answer4ms7568 KiB
32Wrong answer4ms5836 KiB
33Wrong answer4ms5832 KiB
34Wrong answer4ms5784 KiB
35Wrong answer4ms5876 KiB
subtask50/59
36Wrong answer206ms97244 KiB
37Wrong answer206ms87868 KiB
38Wrong answer317ms163276 KiB
39Wrong answer328ms164676 KiB
40Wrong answer300ms165204 KiB
41Wrong answer204ms96348 KiB
42Wrong answer243ms96760 KiB
43Wrong answer207ms96332 KiB
44Wrong answer239ms96400 KiB
45Wrong answer170ms87824 KiB
46Wrong answer153ms41420 KiB
47Wrong answer172ms41380 KiB
48Wrong answer152ms41364 KiB
49Wrong answer156ms41588 KiB