10666 | 2024-04-07 19:33:57 | 111 | XORfa visszatér | cpp17 | Wrong answer 0/100 | 1.564s | 165576 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;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1904 KiB | ||||
2 | Accepted | 3ms | 2096 KiB | ||||
subtask2 | 0/11 | ||||||
3 | Accepted | 3ms | 2716 KiB | ||||
4 | Accepted | 3ms | 2928 KiB | ||||
5 | Accepted | 3ms | 3184 KiB | ||||
6 | Accepted | 3ms | 3396 KiB | ||||
7 | Wrong answer | 3ms | 3628 KiB | ||||
8 | Accepted | 3ms | 3532 KiB | ||||
9 | Wrong answer | 3ms | 3520 KiB | ||||
10 | Wrong answer | 3ms | 3296 KiB | ||||
11 | Wrong answer | 3ms | 3580 KiB | ||||
12 | Wrong answer | 3ms | 3796 KiB | ||||
13 | Wrong answer | 3ms | 4004 KiB | ||||
subtask3 | 0/13 | ||||||
14 | Wrong answer | 65ms | 17948 KiB | ||||
15 | Wrong answer | 71ms | 17940 KiB | ||||
16 | Accepted | 119ms | 24900 KiB | ||||
17 | Accepted | 148ms | 32780 KiB | ||||
18 | Accepted | 152ms | 38124 KiB | ||||
19 | Accepted | 156ms | 39944 KiB | ||||
20 | Accepted | 130ms | 29536 KiB | ||||
21 | Accepted | 141ms | 32344 KiB | ||||
22 | Wrong answer | 70ms | 18572 KiB | ||||
23 | Wrong answer | 68ms | 18576 KiB | ||||
subtask4 | 0/17 | ||||||
24 | Accepted | 4ms | 7064 KiB | ||||
25 | Wrong answer | 4ms | 7028 KiB | ||||
26 | Accepted | 4ms | 7024 KiB | ||||
27 | Accepted | 7ms | 9104 KiB | ||||
28 | Accepted | 7ms | 9096 KiB | ||||
29 | Wrong answer | 4ms | 6944 KiB | ||||
30 | Accepted | 4ms | 7088 KiB | ||||
31 | Accepted | 4ms | 7112 KiB | ||||
32 | Wrong answer | 23ms | 5372 KiB | ||||
33 | Wrong answer | 24ms | 5472 KiB | ||||
34 | Wrong answer | 23ms | 5572 KiB | ||||
35 | Wrong answer | 23ms | 5376 KiB | ||||
subtask5 | 0/59 | ||||||
36 | Accepted | 212ms | 97524 KiB | ||||
37 | Wrong answer | 211ms | 88056 KiB | ||||
38 | Accepted | 270ms | 163696 KiB | ||||
39 | Accepted | 294ms | 165040 KiB | ||||
40 | Accepted | 310ms | 165576 KiB | ||||
41 | Accepted | 211ms | 96588 KiB | ||||
42 | Accepted | 246ms | 97108 KiB | ||||
43 | Wrong answer | 215ms | 96652 KiB | ||||
44 | Accepted | 210ms | 96964 KiB | ||||
45 | Wrong answer | 145ms | 88324 KiB | ||||
46 | Time limit exceeded | 1.564s | 22904 KiB | ||||
47 | Time limit exceeded | 1.562s | 22780 KiB | ||||
48 | Time limit exceeded | 1.552s | 22948 KiB | ||||
49 | Time limit exceeded | 1.531s | 22832 KiB |