Welcome Party(ZOJ--4109)

本文探讨了在国际大学生程序设计竞赛世界总决赛中举办欢迎派对的问题,旨在找到一种最优的参与者进入顺序,以最小化不开心的参与者数量。文章详细介绍了问题背景、输入输出格式及示例,适合算法爱好者和竞赛选手阅读。

                                                    Welcome Party


Time Limit: 2 Seconds      Memory Limit: 131072 KB


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 <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#define eps 0.0000000001
#define mem(a) memset(a,0,sizeof(a))
#define maxx 1e10
#define inf 0x3f3f3f3f
#define PI acos(-1)
using namespace std;
typedef long long ll;

#define mod 1000000007
const int maxn=1000100;

int n,m;
vector<int> v[maxn];
int vis[maxn],vv[maxn];
priority_queue<int,vector<int>,greater<int> > q;

void Union(int x,int y)
{
    v[x].push_back(y);
    v[y].push_back(x);
    return;
}
void BFS(int x)
{
    queue<int> q1;
    q1.push(x);
    while(!q1.empty())
    {
        int top=q1.front();
        q1.pop();
        for(int i=0;i<v[top].size();i++)
        {
            int t=v[top][i];
            if(vis[t]==0)
            {
                vis[t]=1;
                q1.push(t);
            }
        }
    }
    return;
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        for(int i=0;i<=n;i++)
        {
            v[i].clear();
            vis[i]=vv[i]=0;
        }
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d %d",&x,&y);
            Union(x,y);
        }
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==0)
            {
                ans++;
                BFS(i);
                q.push(i);
                vis[i]=1;
                vv[i]=1;
            }
        }
        cout<<ans<<endl;
        int flag=0;
        while(!q.empty())
        {
            int top=q.top();
            q.pop();
            for(int i=0;i<v[top].size();i++)
            {
                int t=v[top][i];
                if(vv[t]==0)
                {
                    vv[t]=1;
                    q.push(t);
                }
            }
            if(flag==0)
            {
                cout<<top;
                flag++;
            }
            else
                cout<<" "<<top;
        }
        cout<<endl;
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值