106502024-04-07 18:24:18111XORfa visszatércpp17Runtime error 28/100513ms5104 KiB
#include<bits/stdc++.h>
using namespace std;

#define int long long

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N,Q;
	cin>>N>>Q;
	if(N>1000||Q>1000)exit(1);
	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;
	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);
			for(int y:p[x]){
				p[y].erase(x);
				ans.erase(ans.find(v[x]^v[y]));
				if(s.size()>1){
					int z=y;
					for(int a:s){
						if((v[y]^v[a])>(v[y]^v[z])){
							z=a;
						}
					}
					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=x;
				for(int a:s){
					if((v[x]^v[a])>(v[x]^v[y])){
						y=a;
					}
				}
				if(!p[x].count(y)){
					ans.insert(v[x]^v[y]);
					p[x].insert(y);
					p[y].insert(x);
				}
			}
			s.insert(x);
		}
		// for(int i:ans)cout<<i<<' ';cout<<'\n';
		cout<<*--ans.end()<<'\n';
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1824 KiB
2Accepted3ms2016 KiB
subtask211/11
3Accepted3ms2380 KiB
4Accepted3ms2584 KiB
5Accepted3ms2548 KiB
6Accepted3ms2808 KiB
7Accepted3ms3136 KiB
8Accepted3ms2976 KiB
9Accepted3ms3228 KiB
10Accepted3ms3576 KiB
11Accepted3ms3636 KiB
12Accepted3ms3480 KiB
13Accepted3ms3804 KiB
subtask30/13
14Runtime error3ms3788 KiB
15Runtime error3ms3944 KiB
16Runtime error3ms4032 KiB
17Runtime error3ms3928 KiB
18Runtime error2ms4036 KiB
19Runtime error2ms4028 KiB
20Runtime error2ms3924 KiB
21Runtime error2ms3924 KiB
22Runtime error3ms4152 KiB
23Runtime error2ms4236 KiB
subtask417/17
24Accepted6ms4604 KiB
25Accepted4ms4332 KiB
26Accepted8ms4540 KiB
27Accepted8ms4664 KiB
28Accepted9ms5000 KiB
29Accepted6ms4724 KiB
30Accepted7ms4648 KiB
31Accepted7ms4652 KiB
32Accepted508ms4896 KiB
33Accepted513ms4852 KiB
34Accepted513ms4864 KiB
35Accepted513ms5104 KiB
subtask50/59
36Runtime error2ms4528 KiB
37Runtime error3ms4528 KiB
38Runtime error3ms4772 KiB
39Runtime error2ms4760 KiB
40Runtime error2ms4888 KiB
41Runtime error2ms4636 KiB
42Runtime error2ms4528 KiB
43Runtime error2ms4624 KiB
44Runtime error2ms4624 KiB
45Runtime error2ms4464 KiB
46Runtime error2ms4564 KiB
47Runtime error2ms4568 KiB
48Runtime error2ms4560 KiB
49Runtime error3ms4564 KiB