The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6424 Accepted Submission(s): 2880
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
Source
Recommend
————————————————————————————————————
题目的意思是给出n组朋友关系,问能否分成2组组内不是朋友,如果可以将朋友两两分组最多分几组
思路:首先用染色法判断是否是二分图,然后最大二分图匹配
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
const int MAXN=1005;
int uN,vN,n; //u,v数目
int g[MAXN][MAXN];
int linker[MAXN];
bool used[MAXN];
int color[MAXN];
bool dfs(int u)
{
int v;
for(v=1; v<=vN; v++)
if(g[u][v]&&!used[v])
{
used[v]=true;
if(linker[v]==-1||dfs(linker[v]))
{
linker[v]=u;
return true;
}
}
return false;
}
int hungary()
{
int res=0;
int u;
memset(linker,-1,sizeof(linker));
for(u=1; u<=uN; u++)
{
memset(used,0,sizeof(used));
if(dfs(u)) res++;
}
return res;
}
bool bfs(int x)
{
queue<int>q;
q.push(x);
color[x]=1;
while(!q.empty())
{
int f=q.front();
q.pop();
for(int i=1;i<=n;i++)
{
if(g[f][i]==1)
{
if(color[i]==color[f])
return 0;
else if(color[i]==0)
{
color[i]=-color[f];
q.push(i);
}
}
}
}
return 1;
}
int main()
{
int m,x,y;
while(~scanf("%d%d",&n,&m))
{
int flag=0;
memset(g,0,sizeof g);
for(int i=0; i<m; i++)
{
scanf("%d%d",&x,&y);
g[x][y]=g[y][x]=1;
}
memset(color,0,sizeof color);
for(int i=1;i<=n;i++)
{
if(color[i]==0)
{
if(!bfs(i))
{
flag=1;
break;
}
}
}
if(flag==1)
{
printf("No\n");
continue;
}
uN=vN=n;
printf("%d\n",hungary()/2);
}
return 0;
}