zoukankan      html  css  js  c++  java
  • hdu2444The Accomodation of Students【判断二分图+最大匹配】

    我觉得有必要粘一下英文:

    The Accomodation of Students

    Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2502    Accepted Submission(s): 1190

    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
    Source
     
    题意开始读错了,就是一个二分图判断和求最大匹配
     
    代码:
     1 #include <iostream>
     2 #include <set>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <vector>
     6 using namespace std;
     7 
     8 const int maxn = 205;
     9 const int INF = 1000000000;
    10 int n;
    11 
    12 int col[maxn];
    13 vector<int> G[maxn];
    14 bool is_bi(int u) {
    15     for(int i = 0; i < G[u].size(); i++) {
    16         int v = G[u][i];
    17         if(col[u] == col[v]) return false;
    18         if(!col[v]) {
    19             col[v] = 3 - col[u];
    20             if(!is_bi(v)) return false;
    21         }
    22     }
    23     return true;
    24 }
    25 bool check() {
    26     for(int i = 1; i <= n; i++) {
    27         if(G[i].size()) {
    28             memset(col, 0, sizeof(col));
    29             col[i] = 1;
    30             if(!is_bi(i)) return false;
    31         }
    32     }
    33     return true;
    34 }
    35 
    36 int vis[maxn];
    37 int Left[maxn];
    38 int Link[maxn];
    39 vector<int> V[maxn];
    40 bool Find(int u) {
    41     for(int i = 0; i < V[u].size(); i++) {
    42         int v = V[u][i];
    43         if(!vis[v]) {
    44             vis[v] = 1;
    45             if(Link[v] == -1 || Find(Link[v])) {
    46                 Link[v] = u;
    47                 return true;
    48             }
    49         }
    50     }
    51     return false;
    52 }
    53 int solve() {
    54     int cnt = 0;
    55     memset(Link, -1, sizeof(Link));
    56     for(int i = 1; i <= n; i++) {
    57         if(V[i].size()) {
    58             memset(vis, 0, sizeof(vis));
    59             if(Find(i)) cnt++;
    60         }
    61     }
    62     return cnt;
    63 }
    64 
    65 int main() {
    66     int m;
    67     int u, v;
    68     while(EOF != scanf("%d %d",&n, &m)) {
    69         for(int i = 0; i <= n; i++) {
    70             G[i].clear();
    71             V[i].clear();
    72         }
    73         while(m--) {
    74             scanf("%d %d",&u, &v);
    75             G[u].push_back(v);
    76             G[v].push_back(u);
    77             V[u].push_back(v);
    78         }
    79         if(!check()) {
    80             puts("No");
    81             continue;
    82         }
    83         printf("%d
    ",solve()) ;
    84     }
    85     return 0;
    86 }
    View Code
  • 相关阅读:
    不高级不能发帖的WPS论坛
    打不开盖子的酸奶
    无意中发现的一个好设计:不浸水的香皂盒
    几件小事
    解决ios微信页面回退不刷新
    require.js
    前端遇到的坑
    gulp详细入门教程
    js 获取当前日期
    模仿微信朋友圈 图片浏览 h5 html5 js
  • 原文地址:https://www.cnblogs.com/zhanzhao/p/3908291.html
Copyright © 2011-2022 走看看