10662 | 2024-04-07 19:21:37 | 111 | XORfa visszatér | cpp17 | Wrong answer 0/100 | 328ms | 165204 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;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Wrong answer | 3ms | 1896 KiB | ||||
2 | Accepted | 3ms | 2088 KiB | ||||
subtask2 | 0/11 | ||||||
3 | Wrong answer | 3ms | 2588 KiB | ||||
4 | Wrong answer | 3ms | 2552 KiB | ||||
5 | Wrong answer | 3ms | 2864 KiB | ||||
6 | Wrong answer | 3ms | 3028 KiB | ||||
7 | Wrong answer | 3ms | 2992 KiB | ||||
8 | Wrong answer | 3ms | 3240 KiB | ||||
9 | Wrong answer | 3ms | 3500 KiB | ||||
10 | Wrong answer | 3ms | 3424 KiB | ||||
11 | Wrong answer | 3ms | 3380 KiB | ||||
12 | Wrong answer | 3ms | 3692 KiB | ||||
13 | Wrong answer | 3ms | 3904 KiB | ||||
subtask3 | 0/13 | ||||||
14 | Wrong answer | 65ms | 18916 KiB | ||||
15 | Wrong answer | 64ms | 19348 KiB | ||||
16 | Wrong answer | 112ms | 24224 KiB | ||||
17 | Accepted | 136ms | 32276 KiB | ||||
18 | Accepted | 140ms | 37380 KiB | ||||
19 | Accepted | 152ms | 39460 KiB | ||||
20 | Wrong answer | 126ms | 28680 KiB | ||||
21 | Wrong answer | 129ms | 32000 KiB | ||||
22 | Wrong answer | 64ms | 20192 KiB | ||||
23 | Wrong answer | 67ms | 20260 KiB | ||||
subtask4 | 0/17 | ||||||
24 | Wrong answer | 6ms | 7448 KiB | ||||
25 | Wrong answer | 6ms | 7564 KiB | ||||
26 | Wrong answer | 6ms | 7628 KiB | ||||
27 | Wrong answer | 7ms | 9584 KiB | ||||
28 | Accepted | 7ms | 9664 KiB | ||||
29 | Wrong answer | 4ms | 7516 KiB | ||||
30 | Wrong answer | 6ms | 7504 KiB | ||||
31 | Wrong answer | 4ms | 7568 KiB | ||||
32 | Wrong answer | 4ms | 5836 KiB | ||||
33 | Wrong answer | 4ms | 5832 KiB | ||||
34 | Wrong answer | 4ms | 5784 KiB | ||||
35 | Wrong answer | 4ms | 5876 KiB | ||||
subtask5 | 0/59 | ||||||
36 | Wrong answer | 206ms | 97244 KiB | ||||
37 | Wrong answer | 206ms | 87868 KiB | ||||
38 | Wrong answer | 317ms | 163276 KiB | ||||
39 | Wrong answer | 328ms | 164676 KiB | ||||
40 | Wrong answer | 300ms | 165204 KiB | ||||
41 | Wrong answer | 204ms | 96348 KiB | ||||
42 | Wrong answer | 243ms | 96760 KiB | ||||
43 | Wrong answer | 207ms | 96332 KiB | ||||
44 | Wrong answer | 239ms | 96400 KiB | ||||
45 | Wrong answer | 170ms | 87824 KiB | ||||
46 | Wrong answer | 153ms | 41420 KiB | ||||
47 | Wrong answer | 172ms | 41380 KiB | ||||
48 | Wrong answer | 152ms | 41364 KiB | ||||
49 | Wrong answer | 156ms | 41588 KiB |