BZOJ4668 冷战(并查集)

  显然可以用LCT维护kruskal重构树。或者启发式合并维护kruskal重构树的倍增数组虽然多了个log也不一定比LCT慢吧。

  当然这里的kruskal重构树几乎只是把树上的边权换成了点权,并不重要。

  我们要查询的是树上两点间路径边权最大值。显然要并查集按秩合并一波。然后……并查集的树高就是log啊?维护个鬼的倍增数组啊直接暴力啊?

#include<iostream> 
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 500010
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
    int x=0,f=1;char c=getchar();
    while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
    while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f;
}
int n,m,lastans,fa[N],len[N],deep[N];
bool flag[N];
int find(int x){return fa[x]==x?x:find(fa[x]);}
void merge(int x,int y,int i)
{
    int p=find(x),q=find(y);
    if (p!=q)
    {
        if (deep[p]<deep[q]) swap(p,q);
        fa[q]=p;if (deep[p]==deep[q]) deep[p]++;
        len[q]=i;
    }
}
int query(int x,int y)
{
    int p=find(x),q=find(y);
    if (p!=q) return 0;
    int u=x;while (fa[u]!=u) flag[u]=1,u=fa[u];flag[u]=1;
    int ans=0;
    while (!flag[y]) ans=max(ans,len[y]),y=fa[y];
    u=x;while (u!=y) ans=max(ans,len[u]),flag[u]=0,u=fa[u];
    while (fa[u]!=u) flag[u]=0,u=fa[u];flag[u]=0;
    return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("bzoj4668.in","r",stdin);
    freopen("bzoj4668.out","w",stdout);
    const char LL[]="%I64d\n";
#else
    const char LL[]="%lld\n";
#endif
    n=read(),m=read();
    for (int i=1;i<=n;i++) fa[i]=i,deep[i]=1;
    int cnt=0;
    while (m--)
    {
        int op=read();
        if (op==0)
        {
            int x=read()^lastans,y=read()^lastans;
            merge(x,y,++cnt);
        }
        else
        {
            int x=read()^lastans,y=read()^lastans;
            printf("%d\n",lastans=query(x,y));
        }
    }
    return 0;
}

 

posted @ 2018-11-06 12:49  Gloid  阅读(108)  评论(0编辑  收藏  举报