There are a group of students. Some of them may know each other, while others don’t. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other.
Now you are given all pairs of students who know each other. Your task is to divide the students into two groups so that any two students in the same group don’t know each other.If this goal can be achieved, then arrange them into double rooms. Remember, only paris appearing in the previous given set can live in the same room, which means only known students can live in the same room.
Calculate the maximum number of pairs that can be arranged into these double rooms.
Input
For each data set:
The first line gives two integers, n and m(1<n<=200), indicating there are n students and m pairs of students who know each other. The next m lines give such pairs.
Proceed to the end of file.
Output
If these students cannot be divided into two groups, print “No”. Otherwise, print the maximum number of pairs that can be arranged in those rooms.
Sample Input
4 4
1 2
1 3
1 4
2 3
6 5
1 2
1 3
1 4
2 5
3 6
Sample Output
No
3
有好多学生,你知道他们之间谁和谁是认识的。注意,如果 A 认识 B ,且 B 认识 C ,但不能确定 A 认识 C。现在要确定是否能将这些学生分成两组,每一组的学生中互相都不认识。如果可以分成这样的两组,再求最多有多少对学生可以组成相互认识的一对。
其实这个题题意我看的也很萌,但是这玩意分类是二分图匹配,那就求个最大匹配吧。。。。
首先是判断是否可以分组。根据题意,每组中的学生互相都不认识,那么如果将第一个学生放在第一个(左边)集合中,那么这个学生认识的人一定在另一个(右边)集合中;在右边集合中的学生认识的人,一定都在左边的集合中,这样循环交替进行判断。当出现在左边集合中某个学生认识的人没有在右边集合,而是已经在左边集合中,那么这样就不能分成两组;如果遍历了所有学生都没有问题,那么就可以分成两组。这个程序可以使用DFS 或 BFS 实现,在这里我用的 BFS 实现的,注意图不连通的情况,不能只压入一个点。
判断完成之后套用求二分图最大匹配算法,就得到了答案。
输出注意大小写。。
Ps: 这个题的数据好像是默认连通图的
#include <iostream>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define Push(a) push_back(a)
#define rep(index,star,finish) for(register int index=star;index<finish;index++)
#define drep(index,star,finish) for(register int index=star;index>=finish;index--)
int pos[205]; //left 1 right 0
queue<int> Q;
bool used[205];
int match[205];
vector<int> adj[205];
bool dfs(int v);
int main(){
ios::sync_with_stdio(false);
int n,m;
while(cin>>n>>m){
rep(i,0,m){
int commonA,commonB;
cin>>commonA>>commonB;
adj[commonA].Push(commonB);
adj[commonB].Push(commonA);
}
rep(i,0,n+1)
pos[i]=-1;
bool isbinary=true;
rep(i,1,n+1){
if(pos[i]>=0)
continue;
pos[i]=1;
Q.push(i);
int now,nowPos,nextPos;
while(!Q.empty()){
now=Q.front();
Q.pop();
nowPos=pos[now];
nextPos=(nowPos+1)%2;
rep(j,0,adj[now].size()){
int u=adj[now][j];
if(pos[u]<0){
pos[u]=nextPos;
Q.push(u);
}else{
if(pos[u]!=nextPos){
isbinary=false;
while(!Q.empty())
Q.pop();
break;
}
}
}
}
if(!isbinary)
break;
}
if(!isbinary){
cout<<"No"<<endl;
}else{
rep(i,1,n+1)
match[i]=-1;
int ans=0;
rep(i,1,n+1){
if(pos[i] && match[i]<0){ //select left set
rep(i,1,n+1)
used[i]=false;
if(dfs(i))
ans++;
}
}
cout<<ans<<endl;
}
rep(i,1,n+1)
adj[i].erase(adj[i].begin(),adj[i].end());
}
return 0;
}
bool dfs(int v){
used[v]=true;
rep(i,0,adj[v].size()){
int u=adj[v][i],matched=match[u];
if(matched<0 || !used[matched] && dfs(matched)){
match[v]=u;
match[u]=v;
return true;
}
}
return false;
}