UVA11354 Bond disjoint-set

  The meaning of problems: None given an m n points to FIG edges, each edge there is a risk, there is a query q, each given two points s, t, find a way, such that the upper path The maximum risk minimal

 

By rank merge disjoint-set

Path compression can not otherwise undermine its tree structure

#include<bits/stdc++.h>
using namespace std;
//input by bxd
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
#define see(x) (cerr<<(#x)<<'='<<(x)<<endl)
#define pb push_back
#define inf 0x3f3f3f3f
#define CLR(A,v)  memset(A,v,sizeof A)
typedef pair<int,int>pii;
//////////////////////////////////
const int N=1e6+10;

int f[N],x,y,q,n,m,siz[N],w[N];
struct Edge
{
    int u,v,w;
}edge[N];
int find1(int x)
{
    return f[x]==x?x:find1(f[x]);
}

void kruscal()
{
    sort(edge+1,edge+1+m,[](Edge a,Edge b){return a.w<b.w;});
    int cnt=0;
    rep(i,1,n)f[i]=i,siz[i]=1;
    CLR(w,0);
    rep(i,1,m)
    {
        intx = find1 (edge [i] .u), y = find1 (edge [i] .v);
        if (x == y) continue ;
        if (siz [x] < siz [y]) 
        { 
            f [x] = y; 
            siz [y] = max (siz [y], siz [x] + 1 ); 
            w [x] = edge [i] .w; 
        } 
        Else 
        { 
            f [y] = x; 
            siz [x] = max (siz [x], siz [y] + 1 ); 
            w [y] = edge [i] .w; 
        } 
        CNT ++ ;
        if (CNT == n1)break;
    }
}
int c[N];
int query(int x,int y)
{
    rep(i,1,n)c[i]=-1;
    int ans=0,temp=0;
    while(1)
    {
        c[x]=temp;
        if(f[x]==x)break;
        temp=max(temp,w[x]);
        x=f[x];
    }
    while(1)
    {
        if(c[y]>=0){ans=max(ans,c[y]);break;}//lca相同时就退出
        if(f[y]==y)break;
        ans=max(ans,w[y]);
        y=f[y];
    }
    return ans;
}

int main()
{   
    int ok=0;
    while(scanf("%d%d",&n,&m)==2)
    {   
        if(ok)printf("\n");
        ok=1;
        rep(i,1,m)
        scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
        kruscal();
        scanf("%d",&q);
        while(q--)
        {
            scanf("%d%d",&x,&y);
            printf("%d\n",query(x,y));
        }
    }
    return 0;
}
View Code

Guess you like

Origin www.cnblogs.com/bxd123/p/11335096.html