并查集加优先队列

J - Welcome Party

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
int f[maxn];
int visf[maxn];
int vis[maxn];
int as[maxn];
vector<int> v[maxn];
priority_queue<int,vector<int>,greater<int> > q;
int getf(int a)
{
    if(f[a]==a) return a;
    else return f[a]=getf(f[a]);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        while(q.size()) q.pop();
        for(int i=1; i<=n; i++)
        {
            f[i]=i;
            vis[i]=0;
            visf[i]=0;
            v[i].clear();
        }
        for(int i=1; i<=m; i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            f[getf(b)]=getf(a);
            v[a].push_back(b);
            v[b].push_back(a);
        }
        int ans=0;
        for(int i=1; i<=n; i++)
        {
            if(getf(i)==i) ans++;
            if(visf[getf(i)]==0)
            {
                q.push(i);
                visf[getf(i)]=1;
                vis[i]=1;
            }
        }
        int len=0;
        while(!q.empty())
        {
            int t1=q.top();
            q.pop();
            as[++len]=t1;
            for(int i=0; i<v[t1].size(); i++)
            {
                int y=v[t1][i];
                if(vis[y]) continue;
                q.push(y);
                vis[y]=1;
            }
        }
        printf("%d\n",ans);
        for(int i=1; i<=len; i++)
        {
            if(i!=len) printf("%d ",as[i]);
            else printf("%d\n",as[i]);
        }
    }
}

原文地址:https://www.cnblogs.com/dongdong25800/p/10801223.html

时间: 2024-08-30 14:41:32

并查集加优先队列的相关文章

ZOJ - 4109 - Welcome Party (并查集 + BFS + 优先队列)

题目地址 题目大意:n个人,m种关系 (a和b是朋友),可以看作 n个点,m条边, 用图论的知识解题 问在使最少人不开心的情况下,输出进房间字典序排序最小的顺序.(如果在小A进房间之前房间内没有他的朋友,他就不开心) 使用并查集分块,每个并查集的根节点和独立点(无朋友)的总个数就是输出的不开心的人数.     使用BFS和优先队列遍历存入的图,保证字典序最小.将路径存入答案数组. 代码: #include <bits/stdc++.h> using namespace std; const i

舒适的旅行(并查集)

1050: [HAOI2006]旅行comf Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3107  Solved: 1701[Submit][Status][Discuss] Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点S和T,求一条路径,使得路径上最大边和最小边的比值最小.如果S和T之间没有路径,输出”IMPOSSIBLE”,否则

BZOJ 3454 家族 并查集

题目大意:给定一张无向图,每个点有边权,给每个联通块大小一个喜爱度,求一个最小的区间,使保留这个区间内的所有边权的边时喜爱度之和最大 n<=1000,m<=5000 脑残没法治系列-- 如果暴力枚举区间并每次计算喜爱度,时间复杂度为O(nm^2),超时 固定一个左端点,将右端点右移,每次用并查集加边并维护喜爱度之和,时间复杂度O(m^2) 然后这题就做完了= = #include <cstdio> #include <cstring> #include <iost

HDU(1856),裸的带权并查集

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1856 题意:朋友圈问题,A和B是朋友,B和C是朋友则A和C也是朋友,依次类推,题目的意思就是求最大的朋友圈,即求最大集合中元素的个数.裸的并查集加个秩数组就行了. #include <stdio.h> int father[100050]; int rank[100050]; int Find_Set (int x) { if(x!=father[x]) father[x] = Find_Set(

fzu 2059 并查集+离线处理

题意: There is a array contain N(1<N<=100000) numbers. Now give you M(1<M<10000) query. Every query will be: 1 x : ask longest substring which every number no less than x 2 y x : change the A[y] to x. there are at most change 10 times. For each

源哥每日一题第十八弹 poj 1182 并查集

题目链接:http://poj.org/problem?id=1182 题意:看不懂?退群吧 比平常的并查集加了一个判断集合间关系的操作: 开一个数组记录当前点所在集合的次序(第几个集合)用012表示 比较简单的思路,不过体现了并查集的精妙 #include <iostream> #include <cstring> #include <cstdio> using namespace std; int dad[500005]; int st[50005]; int fi

[POJ1456]Supermarket(贪心 + 优先队列 || 并查集)

传送门 1.贪心 + 优先队列 按照时间排序从前往后 很简单不多说 ——代码 1 #include <queue> 2 #include <cstdio> 3 #include <iostream> 4 #include <algorithm> 5 #define N 10001 6 7 int n, t, ans; 8 std::priority_queue <int, std::vector <int>, std::greater &l

“玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)

题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号最小的城市 输入数据保证正确,每次添加与删除的城市一定是与首都相连的 题解:每次都只需要知道最远且编号最小的城市,所以直接使用优先队列存储 如果是+V就使用并查集(不能路径压缩)添加上然后加入优先队列,接着直接弹出首元素就是结果 如果是-V则把V指向0,接着弹出优先队列的第一个元素 如果他与1相连就

优先队列 + 并查集 + 字典树 + 树状数组 + 线段树 + 线段树点更新 + KMP +AC自动机 + 扫描线

这里给出基本思想和实现代码 . 优先队列 : 曾经做过的一道例题       坦克大战 1 struct node 2 { 3 int x,y,step; 4 friend bool operator <(node s1,node s2) // 定义结构体 的时候 这个 就是 用于 优先队列的 基准排序 5 { 6 return s1.step>s2.step; // 步数小的 在上 为小顶堆 7 } 8 }; 9 priority_queue<node>Q; // 优先队列的结构