ZOJ - 4109 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

 

题意:
给出n个人,用1~n标号。

m行表示m个关系,每行a,b表示a和b是朋友,朋友的关系不可传递。

如果一个人进场的时候场内没有自己认识的人则他会不开心,求一个合适的入场顺序使不开心的人数最少并且序列字典序最小。

 

解题思路:

用并查集可以找到总共有多少块,每块只有祖宗结点不开心。在合并的时候将merge()函数稍作修改,将块中的最小的那个数设为每一块的根结点。设置一个超级源点0将每一块的根结点都看作在同一块。使用bfs和优先队列遍历每个结点,能够拓展到的状态就是每个结点的朋友,出队列的顺序就是最小顺序。

 

AC代码:

#include<bits/stdc++.h>
using namespace std;

const int MAX=2000010;

int f[MAX],n,m;
int ans[MAX],cnt,book[MAX];
vector<int> v[MAX];

int getf(int v)
{
	return f[v]==v?v:f[v]=getf(f[v]);
}

void merge(int u,int v)
{
	int t1=getf(u);
	int t2=getf(v);
	if(t1>t2) f[t1]=t2;
	else f[t2]=t1;
}

void bfs(int x)
{
	priority_queue<int,vector<int>,greater<int> > pq;
	pq.push(x);
	while(!pq.empty())
	{
		int now=pq.top();
		pq.pop();
		if(book[now]==0)
		{
			book[now]=1;
			ans[cnt++]=now;
			for(int i=0;i<v[now].size();i++)
			{
				if(book[v[now][i]]==0)
				{
					pq.push(v[now][i]);
				}
			}
		}
		
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		for(int i=0;i<=n;i++)
		{
			f[i]=i;
			v[i].clear();
			book[i]=0;
		}
		
		int x,y;
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d",&x,&y);
			v[x].push_back(y);
			v[y].push_back(x);
			merge(x,y);
		}
		
		int ans1=0;
		for(int i=1;i<=n;i++)
		{
			if(f[i]==i) 
			{
				v[0].push_back(i);
				ans1++;
			}
		}
		
		cnt=0;
		bfs(0);
		
		printf("%d\n",ans1);
		for(int i=1;i<cnt;i++)
		{
			printf("%d%c",ans[i],i==cnt-1?'\n':' ');
		}
	}
	return 0 ;
}

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值