zoukankan      html  css  js  c++  java
  • HDU 2444 The Accomodation of Students (二分图存在的判定以及最大匹配数)

    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. 

    InputFor 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. 

    OutputIf 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
    题解:
    题目是二分图模板题,判断是否是二分图的方法是:对于两部分的点,不存在一条边两个点在同一个集合里;BFS搜索即可,对于一条变的两个点大不同的标记,如果有矛盾,则不是二分图,否则是:
    参考代码:
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 210;
     4 int g[maxn][maxn];
     5 int match[maxn],vis[maxn];
     6 int col[maxn];
     7 
     8 bool bfs(int s,int n)
     9 {
    10     queue<int> q;
    11     q.push(s);
    12     col[s] = -1;
    13     while(!q.empty())
    14     {
    15         int u = q.front(); q.pop();
    16         for(int i=1;i<=n;i++)
    17         {
    18             if(g[u][i])
    19             {
    20                 if(col[i]==col[u]) return false;
    21                 else if(col[i]==0)
    22                 {
    23                     col[i] = -col[u];
    24                     q.push(i);
    25                 }
    26             }
    27         }
    28     }
    29     return true;
    30 }
    31 
    32 bool judge(int n)
    33 {
    34     memset(col,0,sizeof(col));
    35     for(int i=1;i<=n;i++)
    36     {
    37         if(col[i]==0)
    38             if(!bfs(i,n)) return false;
    39     }
    40     return true;
    41 }
    42 
    43 bool dfs(int u,int n)
    44 {
    45     for(int i=1;i<=n;i++)
    46     {
    47         if(vis[i] || !g[u][i]) continue;
    48         vis[i] = 1;
    49         if(!match[i] || dfs(match[i],n))
    50         {
    51             match[i] = u; match[u] = i;
    52             return true;
    53         }
    54     }
    55     return false;
    56 }
    57 
    58 int main(void)
    59 {
    60     int n,m;
    61     while(~scanf("%d %d",&n,&m))
    62     {
    63         memset(match,0,sizeof(match));
    64         memset(g,0,sizeof(g));
    65         for(int i=0;i<m;i++)
    66         {
    67             int x,y;
    68             scanf("%d%d",&x,&y);
    69             g[x][y]=1;
    70         }
    71         if(!judge(n)) puts("No");
    72         else
    73         {
    74             int ans=0;
    75             for(int i=1;i<=n;i++)
    76             {
    77                 memset(vis,0,sizeof(vis));
    78                 if(dfs(i,n)) ans++;
    79             }
    80             printf("%d
    ",ans);
    81         }
    82     }
    83     return 0;
    84 }
    View Code
  • 相关阅读:
    php 对输入信息的过滤代码
    svn命令备忘
    php下载文件的代码示例
    html中meta的用法
    程序员技术练级攻略
    JavaScript输出对象的内部结构
    转载Web架构师成长之路
    php代码执行效率测试工具xhprof安装&使用
    [译]Pro ASP.NET MVC 3 Framework 3rd Edition (Chapter 20 JQuery) 5.Using jQuery Events 使用jQuery事件
    [译]Pro ASP.NET MVC 3 Framework 3rd Edition (Chapter 20 JQuery) 6.Using jQuery Visual Effects 使用jQuery特效
  • 原文地址:https://www.cnblogs.com/csushl/p/9520559.html
Copyright © 2011-2022 走看看