码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ4109 Welcome Party

时间:2020-02-13 19:20:12      阅读:48      评论:0      收藏:0      [点我收藏+]

标签:empty   clu   swap   bit   nbsp   std   队列   连通块   include   

并查集算连通块的数量,集合的个数就是必然不开心的人数,再跑bfs,用优先队列维护~

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+14;
vector<int> g[maxn];
int father[maxn];
int N,M,T;
int isRoot[maxn];
int visit[maxn];
int findfather (int x) {
    int a=x;
    while (x!=father[x]) x=father[x];
    while (a!=father[a]) {
        int z=a;
        a=father[a];
        father[z]=x;
    }
    return x;
} 
void Union (int a,int b) {
    int faA=findfather (a);
    int faB=findfather (b);
    if (faA<faB) swap(faA,faB);
    father[faA]=faB;
}
int main () {
    scanf ("%d",&T);
    while (T--) {
        scanf ("%d %d",&N,&M);
        for (int i=1;i<=N;i++) father[i]=i,visit[i]=0,isRoot[i]=0;
        for (int i=1;i<=N;i++) g[i].clear();
        int x,y;
        for (int i=0;i<M;i++) {
            scanf ("%d %d",&x,&y);
            g[x].push_back(y);
            g[y].push_back(x);
            Union (x,y);
        }
        for (int i=1;i<=N;i++) isRoot[findfather(i)]++;
        int ans=0;
        priority_queue<int,vector<int>,greater<int>> q;
        for (int i=1;i<=N;i++) {
            if (isRoot[i]) {
                ans++;
                visit[i]=1;
                q.push(i);
            }
        }
        printf ("%d\n",ans);
        while (!q.empty()) {
            int u=q.top();
            q.pop();
            printf ("%d",u);
            for (int i=0;i<g[u].size();i++) 
            if (!visit[g[u][i]]) q.push(g[u][i]),visit[g[u][i]]=1;
            if (!q.empty()) printf (" ");
        }
        printf ("\n");
    }
    return 0;
}

 

ZOJ4109 Welcome Party

标签:empty   clu   swap   bit   nbsp   std   队列   连通块   include   

原文地址:https://www.cnblogs.com/zhanglichen/p/12304583.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!