题目链接:传送门
The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6608 Accepted Submission(s): 2964
Problem 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.
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.
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
解题思路:匈牙利算法模板
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <map>
using namespace std;
#define size1 100006
#define size2 505
struct Edge
{
int node;
Edge*next;
}m_edge[size1];
int girl[size2];
Edge*head[size2];
int Flag[size2];
int Ecnt,cnt;
//mp1内的点指向mp2内的点
map<int,int>mp1,mp2;
void init()
{
Ecnt = cnt = 0;
mp1.clear(); mp2.clear();
fill( girl , girl+size2 , 0 );
fill( head , head+size2 , (Edge*)0 );
}
//b能和g匹配
void mkEdge( int b , int g )
{
m_edge[Ecnt].node = g;
m_edge[Ecnt].next = head[b];
head[b] = m_edge+Ecnt++;
}
bool find( int x )
{
for( Edge*p = head[x] ; p ; p = p->next ){
int s = p->node; //有好感的女生
if( !Flag[s] ){
Flag[s] = true; //该女生在本轮匹配中被访问
if( girl[s] == 0 || find(girl[s]) ){
//女生没有对象或者另外一个男生能把这个妹子让给x男
girl[s] = x;
return true;
}
}
}
return false;
}
//构建二分图
bool Build( int m )
{
int a,b,flag = 0;
for( int i = 0 ; i < m ; ++i ){
scanf("%d%d",&a,&b);
if( mp1[a] != 0 && mp1[b] != 0 ) flag = 1;
if( mp2[a] != 0 && mp2[b] != 0 ) flag = 1;
if( mp1[a] != 0 && mp2[b] == 0 ){
mp2[b] = 1; mkEdge(a,b);
}
else if( mp1[a] == 0 && mp2[b] != 0 ){
mp1[a] = 1; mkEdge(a,b);
}
else if( mp1[b] != 0 && mp2[a] == 0 ){
mp2[a] = 1; mkEdge(b,a);
}
else if( mp1[b] == 0 && mp2[a] != 0 ){
mp1[b] = 1; mkEdge(b,a);
}
else if( (mp1[a] == 0 && mp2[b] == 0) || (mp1[b] == 0 && mp2[a] == 0) ){
mp1[a] = 1; mp2[b] = 1; mkEdge(a,b);
}
}
if( flag ) return false;
else return true;
}
int solve()
{
for( map<int,int>::iterator it = mp1.begin() ; it != mp1.end() ; ++it ){
fill( Flag , Flag+size2 , 0 );
if( find(it->first) ) ++cnt;
}
return cnt;
}
int main()
{
int n,m;
while( ~scanf("%d%d",&n,&m) ){
init();
if( !Build(m) ){printf("No\n");continue;}
printf("%d\n",solve());
}
return 0;
}
之前不知道二分图判定,于是用map模拟了两个点集怼过去了,感觉不是太方便,于是又跑回来用染色法判定二分图写了个
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
const int N = 40900;
const int M = 209;
const int INF = 0x3fffffff;
const double eps = 1e-8;
const double PI = acos(-1.0);
struct Edge{
int node;
Edge*next;
}m_edge[N*2];
int girl[M];
Edge*head[M];
int Flag[M],color[M],Ecnt,cnt;
void init()
{
Ecnt = cnt = 0;
fill( girl , girl+M , 0 );
fill( color , color+M , -1 );
fill( head , head+M , (Edge*)0 );
}
//染色法判断是否为二分图
bool judge( int u )
{
for( Edge*p = head[u] ; p ; p = p->next ){
int v = p->node;
if( color[v] == color[u] ) return false;
if( color[v] == -1 ){
color[v] = color[u]^1;
if( !judge(v) ) return false;
}
}
return true;
}
//b对g有好感
void mkEdge( int b , int g )
{
m_edge[Ecnt].node = g;
m_edge[Ecnt].next = head[b];
head[b] = m_edge+Ecnt++;
}
bool find( int x )
{
for( Edge*p = head[x] ; p ; p = p->next ){
int s = p->node; //有好感的女生
if( !Flag[s] ){
Flag[s] = true; //该女生在本轮匹配中被访问
if( girl[s] == 0 || find(girl[s]) ){
//女生没有对象或者另外一个男生能把这个妹纸让给x男
girl[s] = x;
return true;
}
}
}
return false;
}
//构建二分图
void Build( int m )
{
int a,b;
for( int i = 0 ; i < m ; ++i ){
scanf("%d%d",&a,&b);
//建立双倍的边
//假如一般的二分图点集为x,y
//那么现在点集x'和y'都包含了x,y
//即可以将这个二分图看成两个部分,从x->y,从y->x
//最后得到的最大匹配数加倍
mkEdge(a,b);
mkEdge(b,a);
}
}
void solve( int n )
{
for( int i = 1 ; i <= n ; ++i ){
fill( Flag , Flag+M , 0 );
if( find(i) ) ++cnt;
}
}
int main()
{
int n,m;
while( ~scanf("%d%d",&n,&m) ){
init();
Build(m);
int flag = 0;
for( int i = 1 ; i <= n ; ++i ){
if( color[i] != -1 ) continue;
color[i] = 1;
if( !judge(i) ){
flag = 1; break;
}
}
if( flag ) printf("No\n");
else{
solve(n);
printf("%d\n",cnt/2);
}
}
return 0;
}