106662024-04-07 19:33:57111XORfa visszatércpp17Wrong answer 0/1001.564s165576 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<int>h(N+1);
	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();
			h[x]=0;
		}
		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);
					h[x]=max(h[x],v[x]^v[y]);
					h[y]=max(h[y],v[x]^v[y]);
				}
			}
			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]);
					int o=v[x]^v[z];
					if(!p[x].count(z)&&o>h[z]){
						ans.emplace(o,pair<int,int>{x,z});
						p[x].insert(z);
						p[z].insert(x);
					}
				}
				if(s.count(y)){
					int z=t.get(v[y]);
					int o=v[y]^v[z];
					if(!p[y].count(z)&&o>h[z]){
						ans.emplace(o,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
1Accepted3ms1904 KiB
2Accepted3ms2096 KiB
subtask20/11
3Accepted3ms2716 KiB
4Accepted3ms2928 KiB
5Accepted3ms3184 KiB
6Accepted3ms3396 KiB
7Wrong answer3ms3628 KiB
8Accepted3ms3532 KiB
9Wrong answer3ms3520 KiB
10Wrong answer3ms3296 KiB
11Wrong answer3ms3580 KiB
12Wrong answer3ms3796 KiB
13Wrong answer3ms4004 KiB
subtask30/13
14Wrong answer65ms17948 KiB
15Wrong answer71ms17940 KiB
16Accepted119ms24900 KiB
17Accepted148ms32780 KiB
18Accepted152ms38124 KiB
19Accepted156ms39944 KiB
20Accepted130ms29536 KiB
21Accepted141ms32344 KiB
22Wrong answer70ms18572 KiB
23Wrong answer68ms18576 KiB
subtask40/17
24Accepted4ms7064 KiB
25Wrong answer4ms7028 KiB
26Accepted4ms7024 KiB
27Accepted7ms9104 KiB
28Accepted7ms9096 KiB
29Wrong answer4ms6944 KiB
30Accepted4ms7088 KiB
31Accepted4ms7112 KiB
32Wrong answer23ms5372 KiB
33Wrong answer24ms5472 KiB
34Wrong answer23ms5572 KiB
35Wrong answer23ms5376 KiB
subtask50/59
36Accepted212ms97524 KiB
37Wrong answer211ms88056 KiB
38Accepted270ms163696 KiB
39Accepted294ms165040 KiB
40Accepted310ms165576 KiB
41Accepted211ms96588 KiB
42Accepted246ms97108 KiB
43Wrong answer215ms96652 KiB
44Accepted210ms96964 KiB
45Wrong answer145ms88324 KiB
46Time limit exceeded1.564s22904 KiB
47Time limit exceeded1.562s22780 KiB
48Time limit exceeded1.552s22948 KiB
49Time limit exceeded1.531s22832 KiB