2019浙江省赛j welcome Party

The 44th World Finals of the International Collegiate Programming Contest (ICPC 2020) will be held in Moscow, Russia. To celebrate this annual event for the best competitive programmers around the world, it is decided to host a welcome party for all  participants of the World Finals, numbered from  to  for convenience.

The party will be held in a large hall. For security reasons, all participants must present their badge to the staff and pass a security check in order to be admitted into the hall. Due to the lack of equipment to perform the security check, it is decided to open only one entrance to the hall, and therefore only one person can enter the hall at a time.

Some participants are friends with each other. There are  pairs of mutual friendship relations. Needless to say, parties are more fun with friends. When a participant enters the hall, if he or she finds that none of his or her friends is in the hall, then that participant will be unhappy, even if his or her friends will be in the hall later. So, one big problem for the organizer is the order according to which participants enter the hall, as this will determine the number of unhappy participants. You are asked to find an order that minimizes the number of unhappy participants. Because participants with smaller numbers are more important (for example the ICPC director may get the number 1), if there are multiple such orders, you need to find the lexicographically smallest one, so that important participants enter the hall first.

Please note that if participant  and  are friends, and if participant  and  are friends, it's NOT necessary that participant  and  are friends.

Input

There are multiple test cases. The first line of the input contains a positive integer , indicating the number of cases. For each test case:

The first line contains two integers  and  (), the number of participants and the number of friendship relations.

The following  lines each contains two integers  and  (), indicating that the -th and the -th participant are friends. Each friendship pair is only described once in the input.

It is guaranteed that neither the sum of  nor the sum of  of all cases will exceed .

Output

For each case, print a single integer on the first line, indicating the minimum number of unhappy participants. On the second line, print a permutation of  to  separated by a space, indicating the lexicographically smallest ordering of participants entering the hall that achieves this minimum number.

Consider two orderings  and , we say  is lexicographically smaller than , if there exists an integer  (), such that  holds for all , and .

Please, DO NOT output extra spaces at the end of each line, or your solution may be considered incorrect!

Sample Input

2
4 3
1 2
1 3
1 4
4 2
1 2
3 4

Sample Output

1
1 2 3 4
2
1 2 3 4

用并查集记录连通分量

将每个连通分量中取一个点加入优先队列

然后bfs就行了

不过此题卡时间非常严格

使用前向星t了

用vector卡时间过 玄学

#include<cstdio>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<map>
using namespace std;
vector<int>ve[1000005];
int vis1[1000005];
int vis[1000005];
int ansq[1000005];
int father[1000005];
priority_queue<int,vector<int>,greater<int> > q;
int iq=0;
int fi(int x)
{
    if(x==father[x]) return x;
    return father[x]=fi(father[x]);
}
int n,m;
void tupu()
{
    while(q.size())
    {
        int tmp=q.top();
        iq++;
        ansq[iq]=tmp;
        q.pop();
        //cout<<tmp<<endl;
        for(int i=0;i<ve[tmp].size();i++)
        {
            int temp=ve[tmp][i];
            if(vis[temp]==0)
            {
                vis[temp]=1;
                q.push(temp);
            }
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        iq=0;
        while(q.size()) q.pop();
        int ans=0;
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)
        {
            ve[i].clear();
            father[i]=i;
            vis[i]=0;
            vis1[i]=0;
        }
        int cnt=0;
        for(int i=1; i<=m; i++)
        {
            int temp1,temp2;
            scanf("%d%d",&temp1,&temp2);
            ve[temp1].push_back(temp2);
            ve[temp2].push_back(temp1);
            father[fi(temp2)]=fi(temp1);
        }
        for(int i=1; i<=n; i++)
        {
            if(fi(i)==i) ans++;
            if(vis1[fi(i)]==0) q.push(i),vis1[fi(i)]=1,vis[i]=1;
        }
        printf("%d
",ans);
        tupu();
        for(int i=1;i<=n;i++)
        {
            if(i!=n) printf("%d ",ansq[i]);
            else printf("%d
",ansq[i]);
        }
    }
}
原文地址:https://www.cnblogs.com/caowenbo/p/11852279.html