bnuoj 51275(道路修建-并查集按秩合并)

来源:互联网 发布:黑魂三捏脸数据女 编辑:程序博客网 时间:2024/06/07 15:46

题意:维护一个并查集,但需要询问2个点是在第几次操作后连通的。
直接可持久化并查集应该行,
但更简单的方法是,把并查集按秩合并,
即t时刻,在u,v所处连通块间连一条权值为t的边,
这样每个并查集的树高是log级的。

#include<bits/stdc++.h>using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define ForkD(i,k,n) for(int i=n;i>=k;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define Forp(x) for(int p=Pre[x];p;p=Next[p])#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  #define Lson (o<<1)#define Rson ((o<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,127,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define INF (2139062143)#define F (100000007)#define pb push_back#define mp make_pair #define fi first#define se second#define vi vector<int> #define pi pair<int,int>#define SI(a) ((a).size())typedef long long ll;typedef unsigned long long ull;ll mul(ll a,ll b){return (a*b)%F;}ll add(ll a,ll b){return (a+b)%F;}ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}void upd(ll &a,ll b){a=(a%F+b%F)%F;}int read(){    int x=0,f=1; char ch=getchar();    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}    return x*f;} #define MAXN (100000+10)#define MAXM (500000+10)class bingchaji{public:    int father[MAXN],tcn[MAXN],ra[MAXN],vis[MAXN],n;    void mem(int _n)    {        n=_n;        For(i,n) father[i]=i,tcn[i]=ra[i]=0,vis[i]=-1;    }    int getfather(int x)     {        if (father[x]==x) return x;        return getfather(father[x]);    }    bool unite(int x,int y,int t)    {        x=getfather(x),y=getfather(y);        if (x==y) return 0;        if (ra[x]>ra[y]) {            father[y]=x;            tcn[y]=t;        } else {            father[x]=y;            tcn[x]=t;            if (ra[x]==ra[y]) ++ra[y];        }               return 1;    }    int Query(int x,int y) {        if ( getfather(x)^getfather(y) ) return 0;        int p=x,cost=0;        while (1) {            vis[p]=cost;            if (p==father[p]) break;            cost=max(cost,tcn[p]);            p=father[p];        }        p=y; cost=0;        int res=0;        while (1) {            if (vis[p]>=0) {                res=max(cost,vis[p]);                break;            }            if (p==father[p]) break;            cost=max(cost,tcn[p]);            p=father[p];        }        p=x;        while (1) {            vis[p]=-1;            if (p==father[p]) break;            p=father[p];        }        return res;         } }S;int main(){//  freopen("A.in","r",stdin);//  freopen(".out","w",stdout);    int T=read();    while(T--){        int n=read();        S.mem(n);        int m=read();        int ans=n,p1=0;        For(i,m) {            int a,b,c;            scanf("%d%d%d",&a,&b,&c);            b^=p1,c^=p1;            if (a==0) {                if (S.unite(b,c,i)) --ans;                printf("%d\n", p1 = ans);            } else {                printf("%d\n", p1 = S.Query(b,c) );            }        }       }    return 0;}
0 0
原创粉丝点击