• 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
  • 相关阅读:
    得到一个文件夹中所有文件的名称的几个方法(命令指示符, C++, python)
    C++ 使用命名规范
    【前端】直击源头的让你3秒理解并且会用Jsonp!!!
    React Native新手入门
    【方法】纯jQuery实现星巴克官网导航栏效果
    【方法】jQuery无插件实现 鼠标拖动切换图片/内容 功能
    【总结】前端框架:react还是vue?
    【总结】2017年当下最值得你关注的前端开发框架,不知道你就OUT了!
    【疑点】<p></p>标签为什么不能包含块级标签?还有哪些特殊的HTML标签?
    【总结】最常用的正则表达式大全,你要找的这里都有!
  • 原文地址:https://www.cnblogs.com/csushl/p/9520559.html
Copyright © 2020-2023  润新知