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
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int maxn=5000050;
const int maxm=5000050;
struct edge{
    int nxt,to;
};
edge e[maxm];
int head[maxn],ans[maxn],pre[maxn];
bool beused[maxn];
priority_queue<int,vector<int>,greater<int> > pq;
int n,m,cnt,res;

int find(int x){
    return x==pre[x]?x:pre[x]=find(pre[x]);
}
bool join(int x,int y){
    x=find(x);y=find(y);
    if(x==y)    return 0;
    if(x>y) pre[x]=y;
    else    pre[y]=x;
}
void init(){
    while(!pq.empty())  pq.pop();
    for(int i=0;i<=n;i++){
        pre[i]=i;head[i]=-1;beused[i]=0;
    }
    cnt=res=0;
}
void add(int u,int v){
    e[cnt].to=v;e[cnt].nxt=head[u];head[u]=cnt++;
}
void solve(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d %d",&n,&m);
        init();
        for(int i=1;i<=m;i++){
            int u,v;
            scanf("%d %d",&u,&v);
            add(u,v);add(v,u);
            join(u,v);
        }
        int sum=0;
        for(int i=1;i<=n;i++){
            if(pre[i]==i){
                beused[i]=1;sum++;
                pq.push(i);
            }
        }
        while(!pq.empty()){
            int u=pq.top();pq.pop();
            ans[res++]=u;
            for(int i=head[u];i!=-1;i=e[i].nxt){
                int v=e[i].to;
                if(beused[v])   continue;
                beused[v]=1;pq.push(v);
            }
        }
        printf("%d\n",sum);
        for(int i=0;i<res;i++){
            if(i==0)    printf("%d",ans[i]);
            else    printf(" %d",ans[i]);
        }
        printf("\n");
    }
}
int main(){
    solve();
    return 0;
}

 

全部评论

相关推荐

07-15 12:24
重庆大学 运营
坏消息:和好工作擦肩而过
给点吧求求了:怎么可能因为差几秒,估计就是简历更好看婉拒了
点赞 评论 收藏
分享
程序员小白条:主要没亮点,项目也是网上的,平平无奇,那只能海投了,奖项总得有一些,然后就是现在最好是前后端都会,自己能做项目并且运维的,要么找星球项目改改,要么找个开源项目改改,自己能拓展功能才是主要的,跟做效率很低很低
点赞 评论 收藏
分享
06-28 22:48
已编辑
广东金融学院 Java
小浪_Coding:学院本+这俩项目不是buff叠满了嘛
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务