您的位置:首页 > 其它

HDU 2444

2015-08-09 20:43 281 查看
The Accomodation of Students

Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3561 Accepted Submission(s): 1656

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.

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 <stdio.h>
#include <string.h>
#include <queue>
using namespace std;

struct Node
{
int v;
int next;
}Edge[200*200*2+10];

int pre[210];

void add(int u,int v,int index)
{
Edge[index].v=v;
Edge[index].next=pre[u];
pre[u]=index;
}
int flag[210],vis[210];
int link[210];
int c[210];

int bfs(int v)
{
queue <int> q;
vis[v]=1;
flag[v]=1;
q.push(v);
while(!q.empty())
{
int b=q.front();
q.pop();
for(int i=pre[b];i!=-1;i=Edge[i].next)
{
int e=Edge[i].v;
if(flag[e]==flag[b])
return 0;
if(!flag[e])
{
flag[e]=flag[b]==1?2:1;
q.push(e);
}
}
}
return 1;
}

bool Find(int v)
{
for(int i=pre[v];i!=-1;i=Edge[i].next)
{
int e=Edge[i].v;
if(!vis[e])
{
vis[e]=1;
if(link[e]==-1||Find(link[e]))
{
link[e]=v;
return true;
}
}
}
return false;
}

int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
int a,b,index=1;
memset(flag,0,sizeof(flag));
memset(pre,-1,sizeof(pre));
memset(link,-1,sizeof(link));
memset(c,0,sizeof(c));
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
add(a,b,index++);
add(b,a,index++);
c[a]++,c[b]++;
}
int state=0;
for(int i=1;i<=n;i++)
{
if(!flag[i]&&c[i])
{
if(!bfs(i))
{
state=1;
break;
}
}
}
int cnt=0;
if(state)
printf("No\n");
else
{
for(int i=1;i<=n;i++)
{
memset(vis,0,sizeof(vis));
if(Find(i))
cnt++;
}
printf("%d\n",cnt/2);  //除以2
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: