「题解」「BZOJ-4668」冷战

题目

点这里

思路及代码

我们可以使用并查集的按秩合并(但是不要路径压缩)。

两个集合被合并起来,连上的边的权值就设为当前时间。

然后我们可以发现,询问 \(j\)\(k\) 何时联通,就是查询 \(j\)\(k\) 在并查集树路径上边权最大值。因为我们按秩合并了,所以树高是 \(\log ⁡n\) 的,并不会超时,复杂度 \(\mathcal O(n\log n)\)

测试记录

#include<cstdio>
#include<algorithm>
using namespace std;

#define rep(i,__l,__r) for(signed i=__l,i##_end_=__r;i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=__l,i##_end_=__r;i>=i##_end_;--i)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
#define LL long long
#define ull unsigned long long
#define uint unsigned int
#define pii pair< int,int >
#define Endl putchar('\n')
// #define FILEOI
// #define int long long
// #define int unsigned

#ifdef FILEOI
# define MAXBUFFERSIZE 500000
    inline char fgetc(){
        static char buf[MAXBUFFERSIZE+5],*p1=buf,*p2=buf;
        return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXBUFFERSIZE,stdin),p1==p2)?EOF:*p1++;
    }
# undef MAXBUFFERSIZE
# define cg (c=fgetc())
#else
# define cg (c=getchar())
#endif
template<class T>inline void qread(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
inline int qread(){
    int x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
template<class T>void fwrit(const T x){
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);
    putchar(x%10^48);
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int MAXN=500000;
const int INF=0x3f3f3f3f;

int pre[MAXN+5],e[MAXN+5],rk[MAXN+5],dep[MAXN+5];

inline int findSet(int u){
    if(pre[u]==u)return u;
    int ret=findSet(pre[u]);
    dep[u]=dep[pre[u]]+1;
    return ret;
}

inline void merge(const int x,const int y,const int t){
    int u=findSet(x),v=findSet(y);
    if(u==v)return;
    if(rk[u]>rk[v])swap(u,v);
    pre[u]=v,e[u]=t;
    if(rk[u]==rk[v])++rk[v];
}

inline int query(int u,int v){
    if(findSet(u)^findSet(v))return 0;
    if(dep[u]<dep[v])swap(u,v);
    int maxx=-INF;
    while(dep[u]>dep[v])maxx=Max(e[u],maxx),u=pre[u];
    while(u^v){
        maxx=Max(maxx,Max(e[u],e[v]));
        u=pre[u],v=pre[v];
    }
    return maxx;
}

int N,M,lastans;

signed main(){
#ifdef FILEOI
    freopen("file.in","r",stdin);
    freopen("file.out","w",stdout);
#endif
    qread(N,M);
    rep(i,1,N)pre[i]=i,dep[i]=1;
    int opt,u,v,tot=0;
    rep(i,1,M){
        opt=qread();
        u=qread()^lastans;
        v=qread()^lastans;
        if(opt==0)merge(u,v,++tot);
        else writc(lastans=query(u,v),'\n');
    }
    return 0;
}
posted @ 2020-01-20 13:58  Arextre  阅读(124)  评论(0编辑  收藏  举报