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.
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.
4 4 1 2 1 3 1 4 2 3 6 5 1 2 1 3 1 4 2 5 3 6
No 3
判断是否为二分图即可;
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<string>
using namespace std;
const int MAXN=500+10;
int line[MAXN][MAXN];
int n,m,k;
char s[MAXN][MAXN];
int vx[MAXN],vy[MAXN],flag[MAXN];
int x[MAXN],y[MAXN];
int vast[MAXN];
int color[MAXN];
int judge(int v)//染色法判断是否为二分图
{
queue<int>s;
s.push(v);
color[v]=1;
while(!s.empty())
{
int from=s.front();
s.pop();
for(int i=1;i<=n;i++)
{
if(line[from][i]&&color[i]==-1)
{
s.push(i);
color[i]=!color[from];//cout<<color[i]<<endl;
}
if(line[from][i]&&color[from]==color[i])//颜色有相同,则不是二分图
return 0;
}
}
return 1;
}
int Find(int v)
{
for(int i=1;i<=n;i++)
{
if(line[v][i]&&!vast[i])
{
vast[i]=1;
if(!flag[i]||Find(flag[i]))
{
flag[i]=v;
return 1;
}
}
}
return 0;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(color,-1,sizeof(color));
memset(line,0,sizeof(line));
int a,b;
while(m--)
{
scanf("%d%d",&a,&b);
line[a][b]=1;
line[b][a]=1;
}
int kk=0;
for(int i=1;i<=n;i++)
{
if(color[i]==-1&&!judge(i))
{
kk=1;
break;
}
}
if(kk) {printf("No\n");continue;}
memset(flag,0,sizeof(flag));
int ans=0;
for(int i=1;i<=n;i++)
{
memset(vast,0,sizeof(vast));
if(Find(i))
ans++;
}
printf("%d\n",ans/2);
}
return 0;
}