「Luogu P3950」部落冲突

给一棵树,每次可以使一条可以经过边设置为不可经过,也可以将不可经过的边设置成可以经过,多次询问两点间路径上的所有边是否都可以经过。

链接

Luogu P3950

题解

我不会告诉你这题我调了
将可以经过转换成,不可经过转换成,然后树剖+边权下放。对于每次询问,考虑两点间路径上的点权和减去的点权是否等于即可。
最近($2018.12.24$)数据加强了,存边数组开$7e5$过不了,要开$6e5$才能卡过。
但是,换一种思路,也可以用LCT过。
坑。

代码

树剖:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include<bits/stdc++.h>
using namespace std;
typedef int ll;
struct Edge{
ll to,prev;
};
struct SegmentTree{
ll l,r,sum,tag;
};
struct Battle{
ll x,y;
};
const ll MAXN=700051;
Battle bt[MAXN];
Edge ed[MAXN];
SegmentTree tree[MAXN<<1];
ll last[MAXN],val[MAXN],depth[MAXN],fa[MAXN],size[MAXN],heavy[MAXN];
ll id[MAXN],pre[MAXN],top[MAXN];
ll tot,nc,cnt,ccnt,x,y,from,to,num,toto,tx;
char op;
inline ll read()
{
register ll num=0,neg=1;
register char ch=getchar();
while(!isdigit(ch)&&ch!='-')
{
ch=getchar();
}
if(ch=='-')
{
neg=-1;
ch=getchar();
}
while(isdigit(ch))
{
num=(num<<3)+(num<<1)+(ch-'0');
ch=getchar();
}
return num*neg;
}
inline void addEdge(ll from,ll to)
{
ed[++tot].prev=last[from];
ed[tot].to=to;
last[from]=tot;
}
inline void update(ll node)
{
tree[node].sum=tree[node<<1].sum+tree[(node<<1)|1].sum;
}
inline void create(ll l,ll r,ll node)
{
tree[node].l=l,tree[node].r=r;
if(l==r)
{
tree[node].sum=val[l];
return;
}
ll mid=(l+r)>>1;
create(l,mid,node<<1);
create(mid+1,r,(node<<1)|1);
update(node);
}
inline void spread(ll node)
{
if(tree[node].tag)
{
tree[node<<1].sum+=tree[node].tag*(tree[node<<1].r-tree[node<<1].l+1);
tree[(node<<1)|1].sum+=tree[node].tag*(tree[(node<<1)|1].r-tree[(node<<1)|1].l+1);
tree[node<<1].tag+=tree[node].tag;
tree[(node<<1)|1].tag+=tree[node].tag;
tree[node].tag=0;
}
}
inline void change(ll pos,ll val,ll node)
{
if(tree[node].l==tree[node].r)
{
tree[node].sum+=val;
return;
}
ll mid=(tree[node].l+tree[node].r)>>1;
if(pos<=mid)
{
change(pos,val,node<<1);
}
else
{
change(pos,val,(node<<1)|1);
}
update(node);
}
inline void add(ll l,ll r,ll val,ll node)
{
if(l<=tree[node].l&&r>=tree[node].r)
{
tree[node].sum+=val*(tree[node].r-tree[node].l+1);
tree[node].tag+=val;
return;
}
spread(node);
ll mid=(tree[node].l+tree[node].r)>>1;
if(l<=mid)
{
add(l,r,val,node<<1);
}
if(r>mid)
{
add(l,r,val,(node<<1)|1);
}
update(node);
}
inline ll query(ll l,ll r,ll node)
{
if(l<=tree[node].l&&r>=tree[node].r)
{
return tree[node].sum;
}
spread(node);
ll mid=(tree[node].l+tree[node].r)>>1,val=0;
if(l<=mid)
{
val+=query(l,r,node<<1);
}
if(r>mid)
{
val+=query(l,r,(node<<1)|1);
}
return val;
}
inline void dfs(ll node,ll f,ll dep)
{
depth[node]=dep,fa[node]=f,size[node]=1;
ll maxn=-1;
for(register int i=last[node];i;i=ed[i].prev)
{
if(ed[i].to!=f)
{
dfs(ed[i].to,node,dep+1);
size[node]+=size[ed[i].to];
if(size[ed[i].to]>maxn)
{
heavy[node]=ed[i].to,maxn=size[ed[i].to];
}
}
}
}
inline void ddfs(ll node,ll link)
{
id[node]=++ccnt,val[ccnt]=pre[node],top[node]=link;
if(!heavy[node])
{
return;
}
ddfs(heavy[node],link);
for(register int i=last[node];i;i=ed[i].prev)
{
if(ed[i].to!=fa[node]&&ed[i].to!=heavy[node])
{
ddfs(ed[i].to,ed[i].to);
}
}
}
inline ll queryPath(ll x,ll y)
{
ll ans=0;
while(top[x]!=top[y])
{
if(depth[top[x]]<depth[top[y]])
{
swap(x,y);
}
ans+=query(id[top[x]],id[x],1);
x=fa[top[x]];
}
if(depth[x]>depth[y])
{
swap(x,y);
}
ans+=query(id[x],id[y],1);
return ans;
}
inline ll querySubtree(ll root)
{
return query(id[root],id[root]+size[root]-1,1);
}
inline void changePoint(ll x,ll val)
{
change(id[x],val,1);
}
inline void changePath(ll x,ll y,ll val)
{
while(top[x]!=top[y])
{
if(depth[top[x]]<depth[top[y]])
{
swap(x,y);
}
add(id[top[x]],id[x],val,1);
x=fa[top[x]];
}
if(depth[x]>depth[y])
{
swap(x,y);
}
add(id[x],id[y],val,1);
}
inline void changeSubtree(ll root,ll val)
{
add(id[root],id[root]+size[root]-1,val,1);
}
inline ll lca(ll x,ll y)
{
while(top[x]!=top[y])
{
if(depth[top[x]]<depth[top[y]])
{
swap(x,y);
}
x=fa[top[x]];
}
return depth[x]<depth[y]?x:y;
}
int main()
{
nc=read(),cnt=read();
for(register int i=0;i<nc-1;i++)
{
from=read(),to=read();
addEdge(from,to),addEdge(to,from);
}
dfs(1,0,1),ddfs(1,1),create(1,nc,1);
for(register int i=0;i<cnt;i++)
{
cin>>op;
switch(op)
{
case 'C':{
x=read(),y=read();
bt[++num].x=x,bt[num].y=y;
tx=depth[x]>depth[y]?x:y;
changePoint(tx,1);
break;
}
case 'U':{
x=read();
y=bt[x].y,x=bt[x].x;
tx=depth[x]>depth[y]?x:y;
changePoint(tx,-1);
break;
}
case 'Q':{
x=read(),y=read();
toto=queryPath(x,y)-queryPath(lca(x,y),lca(x,y));
toto?puts("No"):puts("Yes");
break;
}
}
}
}

LCT:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include<bits/stdc++.h>
using namespace std;
typedef int ll;
const ll MAXN=3e5+51;
struct Battle{
ll x,y;
};
Battle bt[MAXN];
ll cnt,qcnt,x,y,tot;
char op;
inline ll read()
{
register ll num=0,neg=1;
register char ch=getchar();
while(!isdigit(ch)&&ch!='-')
{
ch=getchar();
}
if(ch=='-')
{
neg=-1;
ch=getchar();
}
while(isdigit(ch))
{
num=(num<<3)+(num<<1)+(ch-'0');
ch=getchar();
}
return num*neg;
}
namespace LCT{
struct Node{
ll fa,val,tag;
ll ch[2];
};
struct LinkCutTree{
Node nd[MAXN];
ll st[MAXN];
inline bool nroot(ll x)
{
return nd[nd[x].fa].ch[0]==x||nd[nd[x].fa].ch[1]==x;
}
inline void update(ll x)
{
nd[x].val=nd[nd[x].ch[0]].val^nd[nd[x].ch[1]].val;
}
inline void reverse(ll x)
{
swap(nd[x].ch[0],nd[x].ch[1]);
}
inline void spread(ll x)
{
if(nd[x].tag)
{
reverse(x);
nd[nd[x].ch[0]].tag^=1,nd[nd[x].ch[1]].tag^=1;
nd[x].tag=0;
}
}
inline void rotate(ll x)
{
ll fa=nd[x].fa,gfa=nd[fa].fa;
ll dir=nd[fa].ch[1]==x,son=nd[x].ch[!dir];
if(nroot(fa))
{
nd[gfa].ch[nd[gfa].ch[1]==fa]=x;
}
nd[x].ch[!dir]=fa,nd[fa].ch[dir]=son;
if(son)
{
nd[son].fa=fa;
}
nd[fa].fa=x,nd[x].fa=gfa;
}
inline void splay(ll x)
{
ll fa=x,gfa,cur=0;
st[++cur]=fa;
while(nroot(fa))
{
st[++cur]=fa=nd[fa].fa;
}
while(cur)
{
spread(st[cur--]);
}
while(nroot(x))
{
fa=nd[x].fa,gfa=nd[fa].fa;
if(nroot(fa))
{
rotate((nd[fa].ch[0]==x)^(nd[gfa].ch[0]==fa)?x:fa);
}
rotate(x);
}
}
inline void access(ll x)
{
for(register int i=0;x;x=nd[i=x].fa)
{
splay(x),nd[x].ch[1]=i;
}
}
inline void makeRoot(ll x)
{
access(x),splay(x),nd[x].tag^=1;
}
inline ll findRoot(ll x)
{
access(x),splay(x);
while(nd[x].ch[0])
{
spread(x),x=nd[x].ch[0];
}
return x;
}
inline void split(ll x,ll y)
{
makeRoot(x),access(y),splay(y);
}
inline void link(ll x,ll y)
{
makeRoot(x),nd[x].fa=y;
}
inline void cut(ll x,ll y)
{
split(x,y),nd[x].fa=nd[y].ch[0]=0;
}
};
}
LCT::LinkCutTree lct;
int main()
{
cnt=read(),qcnt=read();
for(register int i=0;i<cnt-1;i++)
{
x=read(),y=read();
lct.link(x,y);
}
for(register int i=0;i<qcnt;i++)
{
op=getchar();
while(op<'-')
{
op=getchar();
}
if(op=='Q')
{
x=read(),y=read();
puts(lct.findRoot(x)==lct.findRoot(y)?"Yes":"No");
}
if(op=='C')
{
x=read(),y=read();
lct.cut(x,y),bt[++tot].x=x,bt[tot].y=y;
}
if(op=='U')
{
x=read();
lct.link(bt[x].x,bt[x].y);
}
}
}