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
题意:这个题先问了最开始分成两批,组内互相不认识,问是否能做到;能做到,分成两个房间,房间中每对都是认识的,问最多有多少对。
做法:和刚刚做的题几乎一样,第一问认识的连边,求是否是二分图。第二问也是认识的连边,问最大匹配对数
代码写的好恶心==,那两个判断的函数是可以写在一起的 多WA了一次居然是因为No写成NO
#include <iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
vector<int>G[209];
int col[209];
bool flag[209][209];
int n,m,x,y;
const int MAXN=550;
bool dfs(int u,int color)
{
col[u]=color;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(col[v]==color) return false;
if(col[v]==-1)
{
if(!dfs(v,!color)) return false;
}
}
return true;
}
bool solve()
{
memset(col,-1,sizeof(col));
// puts("solve");
for(int i=1;i<=n;i++)
{
if(col[i]==-1)
{
if(!dfs(i,0))return false;
}
}
return true;
}
///====================================================
#define M 550
#define inf 0x3f3f3f3f
int uN,vN;//u,v数目
int g[MAXN][MAXN];
int linker[MAXN];
bool used[MAXN];
bool dfs(int u)//从左边开始找增广路径
{
int v;
for(v=0;v<vN;v++)//这个顶点编号从0开始,若要从1开始需要修改
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()
{
// puts("hungary");
int res=0;
int u;
memset(linker,-1,sizeof(linker));
for(u=0;u<uN;u++)
{
memset(used,0,sizeof(used));
if(dfs(u)) res++;
}
return res;
}
///============================================================
int main()
{
// freopen("cin.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
// printf("%d%d ",n,m);
memset(flag,0,sizeof(flag));
for(int i=1;i<=n;i++) G[i].clear();
memset(g,0,sizeof(g));
uN=vN=n;
for(int i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
flag[x][y]=1;
// flag[y][x]=1;
G[x].push_back(y);
G[y].push_back(x);
g[x-1][y-1]=1;
g[y-1][x-1]=1;
}
if(!solve())
{
puts("No");
continue;
}
printf("%d\n",hungary()/2);
}
return 0;
}