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

#include<bits/stdc++.h>
using namespace std;
;
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);
        ;i<=N;i++) father[i]=i,visit[i]=,isRoot[i]=;
        ;i<=N;i++) g[i].clear();
        int x,y;
        ;i<M;i++) {
            scanf ("%d %d",&x,&y);
            g[x].push_back(y);
            g[y].push_back(x);
            Union (x,y);
        }
        ;i<=N;i++) isRoot[findfather(i)]++;
        ;
        priority_queue<int,vector<int>,greater<int>> q;
        ;i<=N;i++) {
            if (isRoot[i]) {
                ans++;
                visit[i]=;
                q.push(i);
            }
        }
        printf ("%d\n",ans);
        while (!q.empty()) {
            int u=q.top();
            q.pop();
            printf ("%d",u);
            ;i<g[u].size();i++)
            ;
            if (!q.empty()) printf (" ");
        }
        printf ("\n");
    }
    ;
}

最新文章

  1. 偏移:translate ,旋转:rotate,缩放 scale,不知道什么东东:lineCap 实例
  2. phpMyAdmin如何设置float小数点
  3. hibernate-聚合函数分组统计数据查询
  4. Lotus 迁移到Exchange 2010 POC 之在Exchange 2007安装Transport Suite!
  5. 关于XShell的常见使用和设置以及Linux中的常见命令.
  6. 结构性产品 Structured Product
  7. 解决VTune错误.../lib64/libstdc++.so.6: version `GLIBCXX_3.4.14&amp;#39; not found (required by ...)
  8. 解决因特网和xshell考虑到问题
  9. 使用Jax-rs 开发RESTfull API 入门
  10. VB中的GDI编程-2 画笔
  11. 开源Spring解决方案--lm.solution
  12. Appium--入门demo
  13. [Bash]LeetCode192. 统计词频 | Word Frequency
  14. SpringBoot(十四)-- 整合Swagger2
  15. Python爬虫实战四之抓取淘宝MM照片
  16. idea开发工具安装说明
  17. hashlib 和loggin模块
  18. php中各种http报错的状态码分析
  19. mysql用触发器同步表
  20. 【Leetcode】【Medium】Combination Sum

热门文章

  1. 题解【CJOJ1371】[IOI2002]任务安排
  2. 总结下抽象类Abstract和虚方法Virtual(易混点)
  3. zabbix4.2配置邮件+脚本报警:以QQ邮箱为例
  4. IntelliJ IDEA之如何设置JVM运行参数
  5. Python requests库模拟浏览器行为的一些技巧记录
  6. pgspider http fdw http 相关的几个配置参数
  7. PHP基础学习笔记5
  8. SVN代码迁移到GITlab
  9. Aggregate 聚合用法
  10. 一些封装的php函数