Description
某校开展了同学们喜闻乐见的阳光长跑活动。为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动。一时间操场上熙熙攘攘,摩肩接踵,盛况空前。
为了让同学们更好地监督自己,学校推行了刷卡机制。
学校中有n个地点,用1到n的整数表示,每个地点设有若干个刷卡机。
有以下三类事件:
1、修建了一条连接A地点和B地点的跑道。
2、A点的刷卡机台数变为了B。
3、进行了一次长跑。问一个同学从A出发,最后到达B最多可以刷卡多少次。具体的要求如下:
当同学到达一个地点时,他可以在这里的每一台刷卡机上都刷卡。但每台刷卡机只能刷卡一次,即使多次到达同一地点也不能多次刷卡。
为了安全起见,每条跑道都需要设定一个方向,这条跑道只能按照这个方向单向通行。最多的刷卡次数即为在任意设定跑道方向,按照任意路径从A地点到B地点能刷卡的最多次数。
Input
输入的第一行包含两个正整数n,m,表示地点的个数和操作的个数。
第二行包含n个非负整数,其中第i个数为第个地点最开始刷卡机的台数。
接下来有m行,每行包含三个非负整数P,A,B,P为事件类型,A,B为事件的两个参数。
最初所有地点之间都没有跑道。
每行相邻的两个数之间均用一个空格隔开。表示地点编号的数均在1到n之间,每个地点的刷卡机台数始终不超过10000,P=1,2,3。
Output
输出的行数等于第3类事件的个数,每行表示一个第3类事件。如果该情况下存在一种设定跑道方向的方案和路径的方案,可以到达,则输出最多可以刷卡的次数。如果A不能到达B,则输出-1。
Sample Input
10 20 30 40 50 60 70 80 90
3 1 2
1 1 3
1 1 2
1 8 9
1 2 4
1 2 5
1 4 6
1 4 7
3 1 8
3 8 8
1 8 9
3 8 8
3 7 5
3 7 3
1 4 1
3 7 5
3 7 3
1 5 7
3 6 5
3 3 6
1 2 4
1 5 5
3 3 6
2 8 180
3 8 8
2 9 190
3 9 9
2 5 150
3 3 6
2 1 210
3 3 6
Sample Output
-1
-1
80
170
180
170
190
170
250
280
280
270
370
380
580
HINT
数据规模及约定对于100%的数据,m<=5n,任意时刻,每个地点的刷卡机台数不超过10000。
分析:
如果没有动态加边,这道题的解法就是:
我们用tarjan缩点(图的双连通分量),把整个图变成一棵树(因为边是无向的)
在树上直接链剖即可
有了动态加边的限制之后,考虑在树上用LCT进行动态的缩点和维护操作
如果加入某条边之前两个端点就联通,那么我们就将两个点之间的路径提出来,
加入当前边之后,这些点就能形成一个连通分量,我们把这些点缩成一个代表结点,将权值都加给这个结点
用另外一个并查集维护每个点所属的代表元素信息
对于询问2,其实就变成了从LCT中提取缩点后两点所属的点之间的路径上的权值总和
我们用belong数组记录每个点缩点后所属的标志结点
在写LCT的时候,以前我们调用的就是pre[bh]
现在我们需要调用findset(pre[bh])
但是在rotate的时候我们还是调用pre[bh]
在代码中有两个地方需要注意:
if (opt==2)
{
int xx=x; x=findset(x);
expose(x); splay(x);
v[x]+=y-a[xx]; a[xx]=y;
update(x);
}
这个是修改操作,a数组记录的就是每个结点的值
但是,v记录的不一定是单点的值,ta记录的可能是一个连通分量的权值和
因为我们需要用LCT动态维护连通分量,所以每次修改单点值的时候,我们累加的是增值
void merge(int x,int y) //把所有信息统计到y结点上
{
belong[findset(x)]=findset(y);
push(x);
if (x!=y) v[y]+=v[x];
if (ch[x][0]) merge(ch[x][0],y);
if (ch[x][1]) merge(ch[x][1],y);
ch[x][0]=ch[x][1]=0;
}
merge操作可谓本题精髓
调用这个函数的前提是在加入一边后,两个端点x和y已经相连
我们先把x—>y路径提出来(普通的路径查询)
这样x—>y的所有信息就都统计到了一棵splay上(偏爱路径)
我们直接遍历一下这棵splay,统计信息即可
tip
学姐表示:
因为每个点最多只会被合并一次,所以我们的时间复杂度仍然是O(MlogN),
但是LCT常数巨大,所以能不findset就不要findset!!!
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int N=200005;
int n,m,fa[N],q[N],belong[N],a[N],pre[N];
int ch[N][2],v[N],sum[N];
bool rev[N];
int findset(int x)
{
if (belong[x]!=x) belong[x]=findset(belong[x]);
return belong[x];
}
int find_fa(int x)
{
if (fa[x]!=x) fa[x]=find_fa(fa[x]);
return fa[x];
}
int isroot(int bh)
{
int f=findset(pre[bh]);
return ch[f][0]!=bh&&ch[f][1]!=bh;
}
int get(int bh)
{
return (ch[findset(pre[bh])][0]==bh ? 0:1);
}
void update(int bh)
{
if (!bh) return;
sum[bh]=v[bh];
if (ch[bh][0]) sum[bh]+=sum[ch[bh][0]];
if (ch[bh][1]) sum[bh]+=sum[ch[bh][1]];
}
void push(int bh)
{
if (bh&&rev[bh])
{
rev[bh]=0;
swap(ch[bh][0],ch[bh][1]);
if (ch[bh][0]) rev[ch[bh][0]]^=1;
if (ch[bh][1]) rev[ch[bh][1]]^=1;
}
}
void rotate(int bh)
{
int fa=pre[bh];
int grand=pre[fa];
int wh=get(bh);
if (!isroot(fa)) ch[grand][ch[grand][0]==fa? 0:1]=bh;
pre[bh]=grand;
ch[fa][wh]=ch[bh][wh^1];
pre[ch[fa][wh]]=fa;
ch[bh][wh^1]=fa;
pre[fa]=bh;
update(fa);
update(bh);
}
void splay(int bh)
{
int top=0;
q[++top]=bh;
for (int i=bh;!isroot(i);i=findset(pre[i]))
q[++top]=findset(pre[i]);
while (top) push(q[top]),pre[q[top]]=findset(pre[q[top]]),top--;
for (int fa;!isroot(bh);rotate(bh))
if (!isroot(fa=pre[bh]))
rotate(get(bh)==get(fa)? fa:bh);
}
void expose(int bh)
{
int t=0;
while (bh)
{
splay(bh);
ch[bh][1]=t;
update(bh);
t=bh; bh=findset(pre[bh]);
}
}
void makeroot(int bh)
{
expose(bh);
splay(bh);
rev[bh]^=1;
}
void link(int x,int y)
{
makeroot(x);
pre[x]=y;
}
void merge(int x,int y) //把所有信息统计到y结点上
{
belong[findset(x)]=findset(y);
push(x);
if (x!=y) v[y]+=v[x];
if (ch[x][0]) merge(ch[x][0],y);
if (ch[x][1]) merge(ch[x][1],y);
ch[x][0]=ch[x][1]=0;
}
int unionn(int f1,int f2)
{
fa[f1]=f2;
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
{
scanf("%d",&v[i]); a[i]=v[i];
belong[i]=i; fa[i]=i;
}
int opt,x,y;
for (int i=1;i<=m;i++)
{
scanf("%d%d%d",&opt,&x,&y);
if (opt==1)
{
x=findset(x);
y=findset(y);
if (x==y) continue; //已经在一个连通分量中了
int r1=find_fa(x),r2=find_fa(y);
if (r1!=r2)
{
unionn(r1,r2);
link(x,y);
}
else
{
makeroot(x); expose(y); splay(y);
merge(y,y); update(y);
}
}
else if (opt==2)
{
int xx=x; x=findset(x);
expose(x); splay(x);
v[x]+=y-a[xx]; a[xx]=y;
update(x);
}
else
{
x=findset(x); y=findset(y);
if (find_fa(x)!=find_fa(y))
{
printf("-1\n");
continue;
}
makeroot(x);
expose(y); splay(y);
printf("%d\n",sum[y]);
}
}
return 0;
}