并查集算连通块的数量,集合的个数就是必然不开心的人数,再跑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");
    }
    ;
}

ZOJ4109 Welcome Party(2019浙江省赛)的更多相关文章

  1. 2019浙江省赛B zoj4101 Element Swapping(推公式)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=6003 题意 \(数组a通过交换一对数字,得到了b数组,给出x=\sum^n_{ ...

  2. 2019浙江省赛K zoj4110 Strings in the Pocket(manachar)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=6012 题意 给你两个串,可以翻转a串的一个区间,问有多少对l,r使得翻转后的a ...

  3. 2019浙江省赛 Strings in the Pocket【manacher】

    Strings in the Pocket 题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=6012 题意 给你两个字符 ...

  4. ZOJ4110 Strings in the Pocket(2019浙江省赛)

    给出两个字符串,询问有多少种反转方法可以使字符串1变成字符串2. 如果两个串相同,就用马拉车算法找回文串的数量~ 如果两个串不同,从前往后找第一个不同的位置l,从后往前找第二个不同的位置r,反转l和r ...

  5. ZOJ4104 Sequence in the Pocket(2019浙江省赛)

    思维~ #include<bits/stdc++.h> using namespace std; ; int a[maxn]; int b[maxn]; int N; int main ( ...

  6. ZOJ4103 Traveler(2019浙江省赛)

    构造+思维~ #include<bits/stdc++.h> using namespace std; ; int N,M,T; int visit[maxn]; stack<int ...

  7. ZOJ4102 Array in the Pocket(2019浙江省赛)

    贪心~ #include<bits/stdc++.h> using namespace std; ; int a[maxn]; int b[maxn]; int vis[maxn]; se ...

  8. hdu6578 2019湖南省赛D题Modulo Nine 经典dp

    目录 题目 解析 AC_Code @ 题目 第一题题意是一共有{0,1,2,3}四种数字供选择,问有多少个长度为n的序列满足所有m个条件,每个条件是说区间[L,R]内必须有恰好x个不同的数字. 第二题 ...

  9. ZOJ 3879 Capture the Flag 15年浙江省赛K题

    每年省赛必有的一道模拟题,描述都是非常的长,题目都是蛮好写的... sigh... 比赛的时候没有写出这道题目 :( 题意:首先输入4个数,n,q,p,c代表有n个队伍,q个服务器,每支队伍的初始分数 ...

随机推荐

  1. 题解 【洛谷P4290】 [HAOI2008]玩具取名

    这道题很明显是区间DP. 为了方便表示,我们可以将'W'.'I'.'N'.'G'分别设为1.2.3.4. 另外,DP可能有点丑,记忆化搜索可能写起来更容易理解. AC代码: #include < ...

  2. Java替换字符串中的\r\n

    public static void main(String[] args) { String str = "啊\r\n啊"; str = str.replaceAll(" ...

  3. Iris项目结构

  4. python SMTP发邮件

    # from email.mime.text import MIMEText from email.header import Header import smtplib # sender = 'zc ...

  5. 【vue】 router.beforeEach

    import store from '@/store' const Vue = require('vue') const Router = require('vue-router') Vue.use( ...

  6. HTTP状态码详解(上)

    HTTP状态码的英文为 HTTP Status Code.下面是常见的HTTP状态码: 200 - 请求成功 301 - 资源(网页等)被永久转移到其它URL 404 - 请求的资源(网页等)不存在 ...

  7. [Vue源码]一起来学Vue双向绑定原理-数据劫持和发布订阅

    有一段时间没有更新技术博文了,因为这段时间埋下头来看Vue源码了.本文我们一起通过学习双向绑定原理来分析Vue源码.预计接下来会围绕Vue源码来整理一些文章,如下. 一起来学Vue双向绑定原理-数据劫 ...

  8. 联想ideapad关闭Fn

    1.打开bios 开启/重启电脑的时候长按Fn+F2,就可以打开bios面板 2.切换到configuration菜单 使用键盘的右箭头将切换到configuration 3.关闭Fn 使用键盘下箭头 ...

  9. ASP.NET/C# Razor视图引擎深入浅出

    在ASPX中我们使用 <% %>在里面编写C#代码在Razor中我们将会使用@{}编写C#代码1.基础——@+属性显示计算1+2的值:<span>1+2=@(1+2)</ ...

  10. 吴裕雄 python 机器学习——支持向量机线性回归SVR模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm fr ...