2021年度训练联盟热身训练赛第五场F题Group Project
題意:
有n個(gè)人,其中有m組,兩兩互斥,現(xiàn)在要分成兩個(gè)班,但最終求的確是最多有多少對(duì)不互斥的。
題目:
鏈接:https://ac.nowcoder.com/acm/contest/16741/F
來(lái)源:牛客網(wǎng)
The big day has fifinally arrived: today you are going to form groups of two in which you will do the end-of-the-year project. When you arrive at school, you learn that the teacher of the other class is sick, and that your teacher, Mr. B.A.P. Cee, will also have to make groups for the other class. Mr. B.A.P. Cee is a smart guy and realizes that he can use these unfortunate circumstances to his advantage.
Ending up with groups of one should be avoided at all cost, so mixing the students of the two classes may avoid this situation. However, while it is easy to pair up two students from the same class, it is more diffiffifficult to match up students from difffferen classes. Throughout the years there has been a lot of rivalry between the two groups, and many students dislike students in the other class. Mr. B.A.P. Cee knows which pairs of students will result in a fifight and a failed project.
You are given a list of pairs of students who cannot work together. How many disjoint groups of two can Mr. B.A.P. Cee make that will not result in a failed project?
輸入描述:
The input consists of:
? A line with two integers n (1 ≤ n ≤ 105), the number of students, and m (0 ≤ m ≤ 2 · 105), the number of pairs of students who cannot work together.
? m lines, each with two distinct integers i and j (1 ≤ i, j ≤ n, i = j), giving a pair of students who cannot work together.
Students are identifified by the numbers 1 through n. It is guaranteed that it is possible to split the students into two classes in such a way that all students from the same class get along.
輸出描述:
Output the number of pairs of students Mr. B.A.P. Cee can make without making any pair of students who cannot work together.
示例1
輸入
3 2
1 2
3 1
輸出
1
示例2
輸入
5 6
1 4
2 4
3 4
1 5
2 5
3 5
輸出
2
示例3
輸入
6 6
1 4
2 5
3 6
1 5
3 5
2 6
輸出
3
分析:
1.因?yàn)檫@道題求的是兩個(gè)不互斥的對(duì)數(shù),最大能有多少對(duì)不互斥的。所以我們只需要保證區(qū)分后,兩個(gè)班里面不存在不互斥的就可以了。
2.這里我直接用vector數(shù)組處理,兩兩互斥的狀況,建圖,即相鄰互斥,隔代在同一個(gè)班即可
3. 分完班之后,按理來(lái)說(shuō),直接每個(gè)班成對(duì)即可,即su/2+sm/2;但當(dāng)兩個(gè)并不是都互斥時(shí),兩個(gè)班里只要存在一對(duì)不互斥時(shí),此時(shí)這兩個(gè)就可以成為一對(duì)不互斥,此時(shí)的對(duì)數(shù)就是n/2,判斷條件即為su*sm==m
4. 前面說(shuō)了題意并不需要可以用唯一方法分出兩個(gè)班,還有一種是用并查集擴(kuò)展域,用并查集及擴(kuò)展域存儲(chǔ)同類(lèi)關(guān)系和異類(lèi)關(guān)系;也貼在下面
AC代碼:
#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #include<vector> #include<iostream> using namespace std; typedef long long ll; const int M=2e5+10; int n,m,a,b; vector<int>ve[M]; int vis[M]; void dfs(int u,int pre,int color){vis[pre]=color;for(int i=0;i<ve[pre].size();i++){int k=ve[pre][i];if(u==k||vis[k]!=-1) continue;dfs(pre,k,color^1);} } int main(){cin>>n>>m;for(int i=1;i<=n;i++)vis[i]=-1;for(int i=0;i<m;i++){cin>>a>>b;ve[a].push_back(b);ve[b].push_back(a);}int su=0,sm=0;for(int i=1;i<=n;i++){if(vis[i]==-1){dfs(0,i,0);}if(vis[i]==0) ++su;else ++sm;}if(su*sm==m) printf("%d\n",su/2+sm/2);else printf("%d\n",n/2);return 0; } /**有一種情況需要特判: 如果兩班均是奇數(shù)個(gè)人,且有一班中某人的敵人數(shù)小于另一班人數(shù),則說(shuō)明此人可以與另一班某同學(xué)組組;*/ #include <bits/stdc++.h> using namespace std; typedef long long ll; vector<int> vec,tn[100005];//vec存一班的同學(xué)編號(hào),tn[i]存儲(chǔ)i號(hào)同學(xué)的敵人 int fa[200005];//擴(kuò)展域并查集 int find(int x)//此題中,find(x)的返回值為1或0 {if(fa[x]!=x)return fa[x]=find(fa[x]);return x; } int main() {int n,m;int a,b;cin>>n>>m;for(int i=1;i<=n;i++)fa[i]=1;for(int i=1;i<=m;i++){scanf("%d%d",&a,&b);if(a>b)swap(a,b);tn[a].push_back(b);tn[b].push_back(a);fa[b]=a+n;fa[b+n]=a;}int s,l;s=l=0;//l與s存儲(chǔ)兩班人數(shù)for(int i=1;i<=n;i++){if(find(i))l++;else s++,vec.push_back(i);}if(s&1&&l&1){for(int i=0;i<vec.size();i++)if(tn[vec[i]].size()!=l){s--,l++;break;}}printf("%d",s/2+l/2);return 0; }總結(jié)
以上是生活随笔為你收集整理的2021年度训练联盟热身训练赛第五场F题Group Project的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 一加 Buds 3 耳机现身印度 BIS
- 下一篇: 食物链 POJ - 1182