当前位置: 代码迷 >> 综合 >> HDU2444 The Accomodation of Students(二分图匹配+匈牙利算法)
  详细解决方案

HDU2444 The Accomodation of Students(二分图匹配+匈牙利算法)

热度:35   发布时间:2024-02-06 00:50:50.0

HDU2444 The Accomodation of Students(二分图匹配+匈牙利算法)

Description
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

题意

判断所有人可不可以分成两个集合,并且集合中的人都互相不认识。如果可以分成就输出两个集合种最多互相认识的对数。先用BFS染色判断是否存在奇数环,若存在则这个图不构成二分图,也就是不可以分成题意中说的两个集合,输出No。

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<functional>
#include<map>
#include<unordered_map>
//#include<bits/stdc++.h>
#define lowbit(x) ((x)&-(x));
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+10,NN=2e3+10,INF=0x3f3f3f3f,LEN=20;
const ll MOD=1e9+7;
const ull seed=31;
struct Edge{int next,to;
}edge[N]; 
int n,m,num_edge;
int vis[N],head[N],match[N];
bool reserve_boy[NN];
void add_edge(int from,int to){edge[num_edge].next=head[from];edge[num_edge].to=to;head[from]=num_edge++;
}
bool dfs(int u){for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;if(!reserve_boy[v]){reserve_boy[v]=true;if(!match[v]||dfs(match[v])){match[v]=u;return true;}}}return false;
}
bool bfs(){queue<int>q;q.push(1);vis[1]=1;while(!q.empty()){int u=q.front();q.pop();for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;if(!vis[v]){if(vis[u]==1) vis[v]=2;else if(vis[u]==2) vis[v]=1; q.push(v);}else if(vis[u]==vis[v]) return false;}}return true;
}
void init(){num_edge=0;memset(head,-1,sizeof head);memset(vis,0,sizeof vis);memset(match,0,sizeof match);
}
int main(){while(~scanf("%d%d",&n,&m)){init();for(int i=1;i<=m;i++){int u,v;scanf("%d%d",&u,&v);add_edge(u,v);add_edge(v,u);}bool flag=bfs();if(!flag){printf("No\n");continue;}int sum=0;for(int i=1;i<=n;i++){memset(reserve_boy,false,sizeof reserve_boy);if(dfs(i)) ++sum;}printf("%d\n",sum/2);//除2是因为对称,建立的图是无向的,1认识2与2认识1是同种情况}return 0;
}