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

vector进入优先队列

 背景知识:

priority_queue<int,vector<int>,greater<int> > que;1.greater是从小到大排序

2.less是从大到小。默认是升序序列:priority_queue<int> a;

3.结构体自定义排序:struct tmp1 //运算符重载<
{
    int x;
    tmp1(int a) {x = a;}
    bool operator<(const tmp1& a) const
    {
        return x < a.x; //大顶堆
    }
};
priority_queue<tmp1> d; //升序

题意是:一共有n的人,m组a,b;ai,bi表示两个人是朋友。入场的时候每个人前面如果没有朋友就会不开心,问最小的不开心人数,并且输出字典序最小的入场顺序。

思路: 第一个进来的人肯定是要不开心的,如果要最少的人不开心肯定是选择朋友最多的人先入场,这个题首先用并查集找出每个团体序号最小的人,有几个团体就是最少的不开心人数。然后把他扔进优先队列这样就能保证不开心的人最少,输出的字典序也是最少。

 

#include <iostream>
#include<algorithm>
#include<stdio.h>
#include<string>
#include<string.h>
#include<math.h>
#include<vector>
#include<queue>
using namespace std;
const int maxn=1e6+7;
int t,n,m,cnt=0;
int visit[maxn],keep[maxn],father[maxn];
vector<int> vec[maxn];
int found(int x){
    int p=father[x];
    while(p!=x){
        x=p;p=father[x];
    }
    return x;
}
int unix(int a,int b){
    int fa,fb;
    fa=found(a);fb=found(b);
    if(fa>fb){
        father[fa]=fb;
    }
    else father[fb]=fa;
}
void bfs(int p){
    priority_queue<int,vector<int>,greater<int> > que;
    que.push(p);
    while(!que.empty()){
      int k=que.top();que.pop();
      if(!visit[k]){
        keep[cnt++]=k;visit[k]=1;
        for(int i=0;i<vec[k].size();i++){
        if(!visit[vec[k][i]])
            que.push(vec[k][i]);
            //printf("k=%d,sun=%d\n",k,vec[k][i]);
      }

      }

    }

    return ;
}



int main()
{
   int i,j,k;
   scanf("%d",&t);
   while(t--){
    scanf("%d%d",&n,&m);
    int sum=0;
    for(i=0;i<=n;i++){
       father[i]=i;
       vec[i].clear();
       visit[i]=0;
    }
     //visit[0]=1;
    int a,b;
    for(i=0;i<m;i++){
        scanf("%d%d",&a,&b);
        vec[a].push_back(b);
        vec[b].push_back(a);
        unix(a,b);
    }
    for(i=1;i<=n;i++){
        if(father[i]==i){
          vec[0].push_back(i);sum++;
        }

    }
    bfs(0);
    printf("%d\n",sum);
    for(i=1;i<cnt-1;i++)
        if(keep[i])
        printf("%d ",keep[i]);
    printf("%d\n",keep[cnt-1]);
    cnt=0;

   }


    return 0;
}

 

上一篇:ZOJ 4109 Welcome Party 并查集+优先队列+bfs


下一篇:golang中一个interface类型潜在问题