# HDU 2444 The Accomodation of Students

2019/7/22 17:41:15 人评论 次浏览 分类：学习教程

http://acm.hdu.edu.cn/showproblem.php?pid=2444

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```

``````#include<bits/stdc++.h>
using namespace std;
const int maxn=205;

int n,m;
int s[maxn][maxn];
int pre[maxn];
int vis[maxn];
int color[maxn];
vector<int> vec[maxn];

bool fdfs(int i,int c)
{
if(vis[i])
{
if(c==color[i])
return 1;
return 0;
}
vis[i]=1;
color[i]=c;
for(int j=0;j<vec[i].size();j++)
{
int to=vec[i][j];
if(!fdfs(to,!c))
return 0;
}
return 1;
}

bool dfs(int x)
{
for(int i=1;i<=n;i++)
{
if(!vis[i]&&s[x][i])
{
vis[i]=1;
if(pre[i]==0||dfs(pre[i]))
{
pre[i]=x;
return 1;
}
}
}
return 0;
}

int main()
{
while(~scanf("%d %d",&n,&m))
{
int u,v;
memset(s,0,sizeof(s));
memset(pre,0,sizeof(pre));
memset(vis,0,sizeof(vis));
memset(color,0,sizeof(color));
for(int i=1;i<=n;i++)
vec[i].clear();
for(int i=0;i<m;i++)
{
scanf("%d %d",&u,&v);
vec[u].push_back(v);
vec[v].push_back(u);
s[u][v]=s[v][u]=1;
}
if(!fdfs(1,0))
{
printf("No\n");
continue;
}
int ans=0;
for(int i=1;i<=n;i++)
{
memset(vis,0,sizeof(vis));
ans+=dfs(i);
}
printf("%d\n",ans/2);
}
return 0;
}
``````

暂无相关的资讯...

-->