hrbeuTLt4

The Accomodation of Students

TimeLimit: 1 Second   MemoryLimit: 32 Megabyte

Totalsubmit: 26   Accepted: 9  

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

Source

2008 Asia Harbin Regional Contest Online

最近一直没写blog

发现二分图还是不太会做
这个题是先判断是不是二分图,然后求最大匹配
题意

有n个学生,有m对人是认识的,每一对认识的人能分到一间房,问能否把n个学生分成两

部分,每部分内的学生互不认识,而两部分之间的学生认识。如果可以分成两部分,就

算出房间最多需要多少间,否则就输出No。

#include<stdio.h>
#include
<string.h>
#include
<math.h>
#define maxn 205
int n,m;
int g[maxn][maxn];
int color[maxn];
int cx[maxn],cy[maxn];
int mk[maxn];
int ans;
bool flag;
int path(int u)
{
    
int v;
    
for(v=1; v<=n; v++)
    
{
        
if(g[u][v]&&!mk[v])
        
{
            mk[v]
=1;
            
if(cy[v]==-1||path(cy[v]))
            
{
                cx[u]
=v;
                cy[v]
=u;
                
return 1;
            }

        }

    }

    
return 0;
}

int match()
{
    
int res,i;
    res
=0;
    memset(cx,
-1,sizeof(cx));
    memset(cy,
-1,sizeof(cy));
    
for(i=1; i<=n; i++)
        
if(cx[i]==-1)
        
{
            memset(mk,
0,sizeof(mk));
            res
+=path(i);
        }

    
return res;
}

void dfs(int u,int col)
{
    
int i;
    color[u]
=col;
    
for(i=1;i<n;i++)
        
if((g[u][i]||g[i][u])&&color[i]==-1&&flag)
    
{
        dfs(i,
1-col);
    }

    
else if((g[u][i]||g[i][u])&&color[i]==col)
    
{
        flag
=false;
        
return;
    }

}

int main()
{
    
int i,j;
    
int p1,p2;
    
while(scanf("%d%d",&n,&m)!=EOF)
    
{
        memset(g,
0,sizeof(g));
        
for(i=1; i<=m; i++)
        
{
            scanf(
"%d%d",&p1,&p2);
            g[p1][p2]
=1;
        }

        ans
=0;
        flag
=true;
        memset(color,
-1,sizeof(color));
        dfs(
1,0);
        
if (!flag) printf("No\n");
        
else 
        
{
            ans
=match();
            printf(
"%d\n",ans);
        }

    }

    
return 0;
}


//二分图判断+最大匹配



 

posted on 2012-04-24 15:18 jh818012 阅读(75) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年7月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

导航

统计

常用链接

留言簿

文章档案(85)

搜索

最新评论