HDU3938 Portal (并查集经典+最小生成树)

本题从题目给出的条件我们发现了最小生成树的影子,也就是kruscal的影子

其实我们在写kruscal的时候就是利用并查集的思想来写的

这题需要注意的是,我们在求取的过程中l并不需要减少,我们只要最小生成树中的最大边权小于等于l就行了

这就让我们想到了可以从小到大对边权排序,之后枚举维护树集合,如果两个点还不在同一颗树中那就合并

那么答案就是求取的路径的个数,我们知道每个边对路径的个数的贡献就是左边节点?右边节点,根据乘法原理可得。

本题还需要注意的是,询问的个数很多,如果在线查询,每次从头开始,会超时,因此用离线思想,将l也排序,这样就只需O(N)就能做了

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const int N=1e5+10;
int n,m;
int p[N];
int size[N];
struct node{
    int a,b;
    int w;
    bool operator <(const node &t){
        return w<t.w;
    }
}s[N];
struct qi{
    int l;
    int k;
    bool operator <(const qi&t){
        return l<t.l;
    }
}q[N];
int ans[N];
int find(int x){
    if(p[x]!=x){
        p[x]=find(p[x]);
    }
    return p[x];
}
int main(){
    int t;
    while(cin>>n>>m>>t){
        int i;
        for(i=1;i<=n;i++){
            p[i]=i;
            size[i]=1;
        }
        for(i=1;i<=m;i++){
            int u,v;
            int w;
            scanf("%d%d%d",&u,&v,&w);
            s[i].a=u,s[i].b=v,s[i].w=w;
        }
        sort(s+1,s+1+m);
        for(i=1;i<=t;i++){
            scanf("%d",&q[i].l);
            q[i].k=i;
        }
        sort(q+1,q+1+t);
        int pos=1;
        int res=0;
        for(i=1;i<=t;i++){
            while(pos<=m&&s[pos].w<=q[i].l){
                 int pa=find(s[pos].a),pb=find(s[pos].b);
                 if(pa!=pb){
                     res+=size[pa]*size[pb];
                     p[pa]=pb;
                     size[pb]+=size[pa];
                 }
                 pos++;
            }
            ans[q[i].k]=res;
        }
        for(i=1;i<=t;i++)
        printf("%d\n",ans[i]);

    }
}

原文地址:https://www.cnblogs.com/ctyakwf/p/12362645.html

时间: 2024-08-06 22:54:14

HDU3938 Portal (并查集经典+最小生成树)的相关文章

POJ-2421Constructing Roads,又是最小生成树,和第八届河南省赛的引水工程惊人的相似,并查集与最小生成树的灵活与能用,水过~~~

Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K               Description There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A

HDU--1558--Segment set--并查集***经典***

Segment set Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3699    Accepted Submission(s): 1394 Problem Description A segment and all segments which are connected with it compose a segment set

hdu 1233(还是畅通工程)(prime算法,克鲁斯卡尔算法)(并查集,最小生成树)

还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 26860    Accepted Submission(s): 11985 Problem Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路

hdu 1233(还是畅通project)(prime算法,克鲁斯卡尔算法)(并查集,最小生成树)

还是畅通project Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 26860    Accepted Submission(s): 11985 Problem Description 某省调查乡村交通状况,得到的统计表中列出了随意两村庄间的距离.省政府"畅通project"的目标是使全省不论什么两个村庄间都能够实现公路交

POJ 3723 Conscription (Kruskal并查集求最小生成树)

Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14661   Accepted: 5102 Description Windy has a country, and he wants to build an army to protect his country. He has picked up N girls and M boys and wants to collect them to b

hdu1875 畅通工程再续 并查集+kruskal最小生成树

Problem Description 相信大家都听说一个"百岛湖"的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全畅通!经过考察小组RPRush对百岛湖的情况充分了解后,决定在符合条件的小岛间建上桥,所谓符合条件,就是2个小岛之间的距离不能小于10米,也不能大于1000米.当然,为了节省资金,只要求实现任意2个小岛之间有路通即可.其中桥的价格为 100元/米.

团伙(并查集经典)

题目大体的说: 1.我朋友的朋友是我的朋友: 2.我敌人的敌人是我的朋友: 所有是朋友的人组成一个团伙.告诉你关于这N个人的M条信息,即某两个人是朋友,或者某两个人是敌人,请你编写一个程序,计算出这个城市最多可能有多少个团伙? 方法: 1 #include<cstdio> 2 #include<cstring> 3 long n,m,x,y,re=0,b,r1,r2; 4 long father[1001],a[1001][1001]; 5 long find (long); 6

Ghostbusters(并查集,最小生成树)

Ghostbusters 时间限制: 1 Sec  内存限制: 128 MB提交: 33  解决: 7[提交] [状态] [讨论版] [命题人:admin] 题目描述 The Bureau of Approved Peripherals for Computers (BAPC) is designing a new standard for computer keyboards. With every new norm and regulation, hardware becomes obsol

HDU ACM 5253 连接的管道-&gt;最小生成树(并查集)

分析:并查集实现最小生成树.不能用cin和cout(使用了ios::sync_with_stdio(false);都不行),否则TLE. #include<iostream> #include<algorithm> #include<cmath> using namespace std; #define N 1005 int father[N*N]; struct EDGE { int u,v; int len; bool operator<(const EDGE