当前位置:   article > 正文

ZOJ - 4109 Welcome Party(并查集+优先队列)

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代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int MAX=2000010;
  4. int f[MAX],n,m;
  5. int ans[MAX],cnt,book[MAX];
  6. vector<int> v[MAX];
  7. int getf(int v)
  8. {
  9. return f[v]==v?v:f[v]=getf(f[v]);
  10. }
  11. void merge(int u,int v)
  12. {
  13. int t1=getf(u);
  14. int t2=getf(v);
  15. if(t1>t2) f[t1]=t2;
  16. else f[t2]=t1;
  17. }
  18. void bfs(int x)
  19. {
  20. priority_queue<int,vector<int>,greater<int> > pq;
  21. pq.push(x);
  22. while(!pq.empty())
  23. {
  24. int now=pq.top();
  25. pq.pop();
  26. if(book[now]==0)
  27. {
  28. book[now]=1;
  29. ans[cnt++]=now;
  30. for(int i=0;i<v[now].size();i++)
  31. {
  32. if(book[v[now][i]]==0)
  33. {
  34. pq.push(v[now][i]);
  35. }
  36. }
  37. }
  38. }
  39. }
  40. int main()
  41. {
  42. int t;
  43. scanf("%d",&t);
  44. while(t--)
  45. {
  46. scanf("%d%d",&n,&m);
  47. for(int i=0;i<=n;i++)
  48. {
  49. f[i]=i;
  50. v[i].clear();
  51. book[i]=0;
  52. }
  53. int x,y;
  54. for(int i=1;i<=m;i++)
  55. {
  56. scanf("%d%d",&x,&y);
  57. v[x].push_back(y);
  58. v[y].push_back(x);
  59. merge(x,y);
  60. }
  61. int ans1=0;
  62. for(int i=1;i<=n;i++)
  63. {
  64. if(f[i]==i)
  65. {
  66. v[0].push_back(i);
  67. ans1++;
  68. }
  69. }
  70. cnt=0;
  71. bfs(0);
  72. printf("%d\n",ans1);
  73. for(int i=1;i<cnt;i++)
  74. {
  75. printf("%d%c",ans[i],i==cnt-1?'\n':' ');
  76. }
  77. }
  78. return 0 ;
  79. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/471326
推荐阅读
相关标签
  

闽ICP备14008679号