日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【2019浙江省赛 - J】Welcome Party(并查集,bfs,优先队列,建图)

發(fā)布時間:2023/12/10 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【2019浙江省赛 - J】Welcome Party(并查集,bfs,优先队列,建图) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題干:

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個人m個關(guān)系,每一對關(guān)系代表這兩人認(rèn)識,關(guān)系不具有傳遞性,現(xiàn)在你要安排這n個人進(jìn)入一個舞會,如果第i個人進(jìn)入了舞會大廳但是發(fā)現(xiàn)沒有一個認(rèn)識的人,那就會不高興。問你怎么安排順序這n個人進(jìn)入舞會大廳,使得不開心的人數(shù)最少。如果有多種方案,輸出字典序最小的方案。

解題報告:

? 首先每一個連通塊肯定有一個人是不開心的,并且可以做到只有一個人是不開心的,所以有多少連通塊,就有多少人不開心。對于字典序,只需要搞個優(yōu)先隊列遍歷就可以了。然后因為有多個連通塊,肯定不能一個塊一個塊的遍歷,所以需要弄一個超級源點連到所有的起點上,然后再遍歷,注意連到每個聯(lián)通塊的起點,這個起點需要是整個塊的最小值,這樣肯定使得結(jié)果最優(yōu)。而讓塊的祖先節(jié)點是最小值,這一點可以用并查集稍作修改實現(xiàn)。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e6 + 5; int f[MAX],n,m; 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; } int ans[MAX],tot,vis[MAX]; vector<int> vv[MAX]; void bfs(int st) {priority_queue<int,vector<int>,greater<int> > pq;pq.push(st);while(!pq.empty()) {int cur = pq.top();pq.pop();if(vis[cur] == 1) continue;vis[cur] = 1;ans[++tot] = cur;int up = vv[cur].size();for(int i = 0; i<up; i++) {int v = vv[cur][i];if(vis[v]) continue;pq.push(v);}} } int main() {int t;cin>>t;while(t--) {tot=0;scanf("%d%d",&n,&m);for(int i = 0; i<=n; i++) f[i] = i,vv[i].clear(),vis[i]=0;for(int u,v,i = 1; i<=m; i++) {scanf("%d%d",&u,&v);vv[u].pb(v);vv[v].pb(u);merge(u,v);}int ans1 = 0;for(int i = 1; i<=n; i++) {if(f[i] == i) vv[0].pb(i),ans1++;}bfs(0);printf("%d\n",ans1);for(int i = 2; i<=tot; i++) {printf("%d%c",ans[i],i==tot ? '\n': ' ');}}return 0 ; }

?

總結(jié)

以上是生活随笔為你收集整理的【2019浙江省赛 - J】Welcome Party(并查集,bfs,优先队列,建图)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。