ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

zoj 4109 Welcome Party (优先队列)

2019-05-05 18:48:12  阅读:323  来源: 互联网

标签:int zoj Welcome number participants 4109 include friends hall


 


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;
}

 

标签:int,zoj,Welcome,number,participants,4109,include,friends,hall
来源: https://blog.csdn.net/intelligentgirl/article/details/89852620

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有