10651 | 2024-04-07 18:50:25 | 111 | XORfa visszatér | cpp17 | Runtime error 28/100 | 1.582s | 254332 KiB |
#include<bits/stdc++.h>
using namespace std;
#define int long long
struct Trie{
vector<array<int,2>>t;
vector<set<int>>v;
Trie():t(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();
v.emplace_back();
}
y=t[y][b];
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];
v[y].erase(n);
}
}
int get(int x){
int y=0;
for(int i=30;i>=0;i--){
int b=x>>i&1;
if(t[y][b^1]&&!v[t[y][b^1]].empty()){
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);
multiset<int>ans;
ans.insert(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);
ans.erase(ans.find(v[x]^v[y]));
if(s.size()>1){
int z=t.get(v[y]);
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=t.get(v[x]);
if(!p[x].count(y)){
ans.insert(v[x]^v[y]);
p[x].insert(y);
p[y].insert(x);
}
}
s.insert(x);
t.add(x,v[x]);
}
// for(int i:ans)cout<<i<<' ';cout<<'\n';
cout<<*--ans.end()<<'\n';
}
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1900 KiB | ||||
2 | Accepted | 3ms | 2096 KiB | ||||
subtask2 | 11/11 | ||||||
3 | Accepted | 3ms | 2748 KiB | ||||
4 | Accepted | 3ms | 2948 KiB | ||||
5 | Accepted | 3ms | 3236 KiB | ||||
6 | Accepted | 3ms | 3268 KiB | ||||
7 | Accepted | 3ms | 3288 KiB | ||||
8 | Accepted | 3ms | 3292 KiB | ||||
9 | Accepted | 3ms | 3268 KiB | ||||
10 | Accepted | 3ms | 3508 KiB | ||||
11 | Accepted | 4ms | 3940 KiB | ||||
12 | Accepted | 3ms | 3876 KiB | ||||
13 | Accepted | 3ms | 3716 KiB | ||||
subtask3 | 0/13 | ||||||
14 | Runtime error | 32ms | 16540 KiB | ||||
15 | Runtime error | 30ms | 16528 KiB | ||||
16 | Accepted | 449ms | 49748 KiB | ||||
17 | Accepted | 684ms | 115040 KiB | ||||
18 | Accepted | 648ms | 148072 KiB | ||||
19 | Accepted | 669ms | 164908 KiB | ||||
20 | Accepted | 569ms | 82892 KiB | ||||
21 | Accepted | 587ms | 115780 KiB | ||||
22 | Runtime error | 32ms | 17632 KiB | ||||
23 | Runtime error | 32ms | 17760 KiB | ||||
subtask4 | 17/17 | ||||||
24 | Accepted | 8ms | 7884 KiB | ||||
25 | Accepted | 8ms | 7396 KiB | ||||
26 | Accepted | 8ms | 9036 KiB | ||||
27 | Accepted | 9ms | 11012 KiB | ||||
28 | Accepted | 9ms | 11024 KiB | ||||
29 | Accepted | 8ms | 7948 KiB | ||||
30 | Accepted | 8ms | 8100 KiB | ||||
31 | Accepted | 8ms | 8100 KiB | ||||
32 | Accepted | 39ms | 8228 KiB | ||||
33 | Accepted | 41ms | 8380 KiB | ||||
34 | Accepted | 41ms | 8188 KiB | ||||
35 | Accepted | 41ms | 8104 KiB | ||||
subtask5 | 0/59 | ||||||
36 | Accepted | 559ms | 144272 KiB | ||||
37 | Accepted | 439ms | 100524 KiB | ||||
38 | Accepted | 685ms | 222428 KiB | ||||
39 | Accepted | 762ms | 232728 KiB | ||||
40 | Accepted | 795ms | 254332 KiB | ||||
41 | Accepted | 570ms | 142452 KiB | ||||
42 | Accepted | 560ms | 143536 KiB | ||||
43 | Accepted | 554ms | 142364 KiB | ||||
44 | Accepted | 551ms | 142432 KiB | ||||
45 | Accepted | 231ms | 76392 KiB | ||||
46 | Time limit exceeded | 1.552s | 82128 KiB | ||||
47 | Time limit exceeded | 1.582s | 82272 KiB | ||||
48 | Time limit exceeded | 1.559s | 82224 KiB | ||||
49 | Time limit exceeded | 1.554s | 82176 KiB |