某校开展了同学们喜闻乐见的阳光长跑活动。为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动。一时间操场上熙熙攘攘,摩肩接踵,盛况空前。
为了让同学们更好地监督自己,学校推行了刷卡机制。
学校中有n个地点,用1到n的整数表示,每个地点设有若干个刷卡机。
有以下三类事件:
1、修建了一条连接A地点和B地点的跑道。
2、A点的刷卡机台数变为了B。
3、进行了一次长跑。问一个同学从A出发,最后到达B最多可以刷卡多少次。具体的要求如下:
当同学到达一个地点时,他可以在这里的每一台刷卡机上都刷卡。但每台刷卡机只能刷卡一次,即使多次到达同一地点也不能多次刷卡。
为了安全起见,每条跑道都需要设定一个方向,这条跑道只能按照这个方向单向通行。最多的刷卡次数即为在任意设定跑道方向,按照任意路径从A地点到B地点能刷卡的最多次数。
输入的第一行包含两个正整数n,m,表示地点的个数和操作的个数。
第二行包含n个非负整数,其中第i个数为第个地点最开始刷卡机的台数。
接下来有m行,每行包含三个非负整数P,A,B,P为事件类型,A,B为事件的两个参数。
最初所有地点之间都没有跑道。
每行相邻的两个数之间均用一个空格隔开。表示地点编号的数均在1到n之间,每个地点的刷卡机台数始终不超过10000,P=1,2,3。
输出的行数等于第3类事件的个数,每行表示一个第3类事件。如果该情况下存在一种设定跑道方向的方案和路径的方案,可以到达,则输出最多可以刷卡的次数。如果A不能到达B,则输出-1。
9 31 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
-1 -1 80 170 180 170 190 170 250 280 280 270 370 380 580
数据规模及约定
对于100%的数据,m<=5n,任意时刻,每个地点的刷卡机台数不超过10000。N<=1.5×105
分析:只有加边没有删边操作,我们考虑把每次把新形成的边双缩成一个点,这样每次询问就相当于查询当前缩点后树上的权值和了,每次缩点用并查集暴力合并就好。
#include <bits/stdc++.h>
#define INF 2147483640
#define eps 1e-9
const int MAXN = 1e6 + 5;
using namespace std;
int n,m,q,f[MAXN],f2[MAXN],s[MAXN],ch[MAXN][2],val[MAXN],sum[MAXN],fa[MAXN];
bool lazy[MAXN];
inline int nextChr()
{
static const int siz=1<<22;
static char buf[siz],*chr=buf+siz;
if(chr==buf+siz)fread(chr=buf,1,siz,stdin);
return int(*chr++);
}
inline int read()
{
register int r=0,c=nextChr();
for(;c<48;c=nextChr());
for(;c>47;c=nextChr())r=(r<<3)+(r<<1)+c-48;
return r;
}
inline int Find(int x)
{
if(f[x] == x) return x;
f[x] = Find(f[x]);
return f[x];
}
inline int Find2(int x)
{
if(f2[x] == x) return x;
f2[x] = Find2(f2[x]);
return f2[x];
}
inline bool isroot(int x)
{
return ch[Find(fa[x])][0] != x && ch[f[fa[x]]][1] != x;
}
void push_up(int x)
{
int ls = ch[x][0],rs = ch[x][1];
sum[x] = sum[ls] + sum[rs] + val[x];
}
void rotate(int x)
{
int y = Find(fa[x]),z = Find(fa[y]);
int d = ch[y][0] == x ? 0 : 1;
if(!isroot(y))
{
if(ch[z][0] == y) ch[z][0] = x;
else ch[z][1] = x;
}
fa[y] = x,fa[x] = z;fa[ch[x][d^1]] = y;
ch[y][d] = ch[x][d^1],ch[x][d^1] = y;
push_up(y),push_up(x);
}
inline void push_down(int x)
{
if(!lazy[x]) return;
int ls = ch[x][0],rs = ch[x][1];
lazy[x] ^= 1;lazy[ls] ^= 1;lazy[rs] ^= 1;
swap(ch[ls][0],ch[ls][1]);
swap(ch[rs][0],ch[rs][1]);
}
void splay(int x)
{
int tot = 0;s[++tot] = x;
for(int i = x;!isroot(i);i = fa[i]) s[++tot] = fa[i];
for(;tot;tot--) push_down(s[tot]);
while(!isroot(x))
{
int y = fa[x],z = Find(fa[y]);
if(!isroot(y))
{
if((ch[z][0] == y) ^ (ch[y][0] == x)) rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x)
{
int t = 0;
while(x)
{
splay(x);
ch[x][1] = t;
push_up(x);
t = x,x = Find(fa[x]);
}
}
void makeroot(int x)
{
access(x),splay(x);
swap(ch[x][0],ch[x][1]);
lazy[x] ^= 1;
}
void link(int x,int y)
{
makeroot(x);fa[x] = y;
f2[f2[x]] = y;
}
bool linked(int x,int y)
{
return Find2(x) == Find2(y);
}
void rebuild(int x,int y)
{
if(!x) return;
f[f[x]] = y;
rebuild(ch[x][0],y);
rebuild(ch[x][1],y);
}
int main()
{
n = read(),m = read();
for(int i = 1;i <= n;i++)
{
val[i] = read();
sum[i] = val[i];
}
for(int i = 1;i <= 2*n;i++) f[i] = f2[i] = i;
for(int i = 1;i <= m;i++)
{
int p,x,y;
p = read(),x = read(),y = read();
if(p == 1)
{
if(!linked(Find(x),Find(y))) link(f[x],f[y]);
else
{
makeroot(f[x]);
access(f[y]);
splay(f[y]);
++n;
val[n] = sum[n] = sum[f[y]];
f2[n] = f2[f[y]];
rebuild(f[y],n);
}
}
if(p == 2)
{
makeroot(Find(x));
access(f[x]);
splay(f[x]);
sum[f[x]] += y-val[x];
val[f[x]] += y-val[x];
val[x] = y;
}
if(p == 3)
{
if(!linked(Find(x),Find(y))) printf("-1\n");
else
{
makeroot(f[x]);
access(f[y]);
splay(f[y]);
printf("%d\n",sum[f[y]]);
}
}
}
}