http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4109

题意:n个人参加会议,如果当参与者进入大厅时,如果他或她发现他或她的朋友都不在大厅中,那么即使他或她的朋友稍后将进入大厅,该参与者也会感到不快乐。求最小化不满意参与者数量,并给出一个入场顺序,如果存在多个答案,输出字典序最小的顺序

题解:并查集+优先队列

不要用memset,TLE警告

C++版本一

链式向前星边存

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=1000000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
int ans[N],cnt,flag,temp,sum;
int vis[N],pre[N],head[N];
char str;
struct node{};
struct Edge {
    int to;
    int next;
} edge[N * 2];
int cnt_edge = 0;
void add_edge(int from, int to)
{
    edge[cnt_edge].to = to;
    edge[cnt_edge].next = head[from];
    head[from] = cnt_edge++;
}
int find(int x){return pre[x]==x?x:pre[x]=find(pre[x]);}
priority_queue<int,vector<int>,greater<int> >q;
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++){
            vis[i]=0;
            head[i]=-1;
            pre[i]=i;//G[i].clear();
        }
        cnt=0;sum=0;
        while(!q.empty()){
            q.pop();
        }
        //memset(vis,0,sizeof(vis));
        //memset(head, -1, sizeof(head));
        cnt_edge = 0;
        //memset(pre,0,sizeof(pre));
        while(m--){
            scanf("%d%d",&u,&v);
            //G[u].push_back(v);
            //G[v].push_back(u);
            add_edge(u,v);
            add_edge(v,u);
            int tu=find(u);
            int tv=find(v);
            if(tu!=tv){
                if(tu<tv)
                    pre[tv]=tu;
                else
                    pre[tu]=tv;
            }
        }
        for(int i=1;i<=n;i++){
            if(pre[i]==i){
                sum++;
                q.push(i);
                vis[i]=1;
            }
        }



        while(!q.empty()){
            int u=q.top();
            q.pop();
            ans[++cnt]=u;
            for(int i=head[u];~i;i=edge[i].next){
                int v=edge[i].to;
                if(vis[v]==0){
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
        cout<<sum<<endl;
        for(int i=1;i<=n;i++)
            printf("%d%c",ans[i]," \n"[i==n]);
    }

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}

 C++版本二

vector边存

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=1000000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
int ans[N],cnt,flag,temp,sum;
int vis[N],pre[N];
char str;
struct node{};
vector<int>G[N];
int find(int x){return pre[x]==x?x:pre[x]=find(pre[x]);}
priority_queue<int,vector<int>,greater<int> >q;
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++){
            vis[i]=0;
            pre[i]=i;
            G[i].clear();
        }
        cnt=0;sum=0;
        while(!q.empty()){
            q.pop();
        }
        while(m--){
            scanf("%d%d",&u,&v);
            G[u].push_back(v);
            G[v].push_back(u);
            int tu=find(u);
            int tv=find(v);
            if(tu!=tv){
                if(tu<tv)
                    pre[tv]=tu;
                else
                    pre[tu]=tv;
            }
        }
        for(int i=1;i<=n;i++){
            if(pre[i]==i){
                sum++;
                q.push(i);
                vis[i]=1;
            }
        }
        while(!q.empty()){
            int u=q.top();
            q.pop();
            ans[++cnt]=u;
            for(int i=0,j=G[u].size();i<j;i++){
                int v=G[u][i];
                if(vis[v]==0){
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
        cout<<sum<<endl;
        for(int i=1;i<=n;i++)
            printf("%d%c",ans[i]," \n"[i==n]);
    }

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}