uva11354 并查集的秩合并 bond

Once again, James Bond is on his way to saving the world. Bond’s latest mission requires him to travel
between several pairs of cities in a certain country.
The country has N cities (numbered by 1, 2, … , N), connected by M bidirectional roads. Bond is
going to steal a vehicle, and drive along the roads from city s to city t. The country’s police will be
patrolling the roads, looking for Bond, however, not all roads get the same degree of attention from the
police.
More formally, for each road MI6 has estimated its dangerousness, the higher it is, the more likely
Bond is going to be caught while driving on this road. Dangerousness of a path from s to t is defined
as the maximum dangerousness of any road on this path.
Now, it’s your job to help Bond succeed in saving the world by finding the least dangerous paths
for his mission.
Input
There will be at most 5 cases in the input file.
The first line of each case contains two integers N, M (2 ≤ N ≤ 50000, 1 ≤ M ≤ 100000) – number
of cities and roads. The next M lines describe the roads. The i-th of these lines contains three integers:
xi
, yi
, di (1 ≤ xi
, yi ≤ N, 0 ≤ di ≤ 109
) – the numbers of the cities connected by the i-th road and its
dangerousness.
Description of the roads is followed by a line containing an integer Q (1 ≤ Q ≤ 50000), followed by
Q lines, the i-th of which contains two integers si and ti (1 ≤ si
, ti ≤ N, si ̸= ti).
Consecutive input sets are separated by a blank line.
Output
For each case, output Q lines, the i-th of which contains the minimum dangerousness of a path between
cities si and ti
. Consecutive output blocks are separated by a blank line.
The input file will be such that there will always be at least one valid path.
Sample Input
4 5
1 2 10
1 3 20
1 4 100
2 4 30
3 4 10
2
1 4
4 1
2 1
1 2 100
1
1 2
Sample Output
20
20
100
题目大意是给你n个点,并给你两点之间的的危险值,然后给你q个查询,每次查询给你两个点,问你从点s到点t的危险值最小,其中,这个最小值是在你找的s到t的路径中危险值最大的数。
分析:一开始我想的是先求出最小生成树,然后在最小生成树的n-1条边中选择s到t的路径,后来发现没法很方便的寻找s到t,后来上网查发现是需要用LCA算法寻找s与t的最小公共祖先,然后求出结果,后来看大佬的代码发现了使用并查集的秩合并一样可以实现。
首先对输入的数据进行cost从小到大的排序,然后对m条输入进行并查集的建立合并,定义c数组,使c【子节点】=a【i】.cost,这样,因为经过排序,使得已经加入的边的最小值进入并查集。
然后对于q条查询,先对u进行遍历,直到找到far【u】=u,再对v进行遍历,直到c1【v】!=0||far【v】=v;
输出即可

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>//并查集按秩合并
using namespace std;
int n,m;
struct node
{
    int begin_,end_,cost;
    bool operator<(const node &R)const
    {
        return cost<R.cost;
    }
}a[50010];
int far[50010];
int ran[50010];
int c[50010];
int c1[50010];
void init(int n1)
{
    for(int i=0;i<=n1;i++)
    {
        far[i]=i;
        ran[i]=0;
        c[i]=0;
    }
}
int find1(int x)
{
   return x==far[x]?far[x]:find1(far[x]);
}
void set_tree(int x,int y,int z)
{
    x=find1(x);
    y=find1(y);
    if(x==y) return ;
    if(ran[x]<ran[y])
    {
        far[x]=y;
        c[x]=z;
    }
    else
    {
        far[y]=x;
        c[y]=z;
        if(ran[x]==ran[y]) ran[x]++;
    }
}
int solve(int u,int v)
{
    for(int i=0;i<=n;i++)
        c1[i]=0;
    int ans=1,ans1=0;
    while(1)
    {
        c1[u]=ans;
       if(far[u]==u) break;
        ans=max(ans,c[u]);
        u=far[u];
    }
    while(1)
    {
        if(c1[v])
        {
            ans1=max(ans1,c1[v]); break;
        }
        else if(far[v]==v)
        {
            break;
        }
        else
        {
            ans1=max(ans1,c[v]);
            v=far[v];
        }
    }
    return ans1;
}
int main()
{
    //ios::sync_with_stdio(false);
    int kase=0;
   while(cin>>n>>m){
    if(kase++) cout<<endl;
    init(n);
    for(int i=0;i<m;i++)
    {
        cin>>a[i].begin_>>a[i].end_>>a[i].cost;
    }
    sort(a,a+m);
    for(int i=0;i<m;i++)
    {
        set_tree(a[i].begin_,a[i].end_,a[i].cost);
    }
//    for(int i=1;i<=n;i++)
//        cout<<c[i]<<endl;
    int q;
    cin>>q;
    while(q--)
    {
        int u,v;
        cin>>u>>v;
        //int res=solve(u,v);
        cout<<solve(u,v)<<endl;
    }
   }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值