zoukankan      html  css  js  c++  java
  • HD2444The Accomodation of Students(并查集判断二分图+匹配)

    The Accomodation of Students

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


    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
     
    题意:A认识B,B认识C但是A不认识C,把ABC分成两个集合,每个集合中的人都认识,不能分成输出No,能的话然后从两个集合中选两个认识的组一个房间也就是在两个集合中求最大匹配,用到了种类并查集
     1 #include <iostream>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <cstdio>
     5 
     6 using namespace std;
     7 const int MAX = 210;
     8 int father[MAX],a[MAX];
     9 int g[MAX][MAX],vis[MAX],link[MAX];
    10 int n,m;
    11 int find_father(int x)
    12 {
    13     if(x == father[x])
    14         return x;
    15     int t = find_father(father[x]);
    16     a[x] = (a[father[x]] + a[x]) % 2;
    17     return father[x] = t;
    18 }
    19 int dfs(int x)
    20 {
    21     for(int i = 1; i <= n; i++)
    22     {
    23         if(vis[i] == 0 && g[x][i])
    24         {
    25             vis[i] = 1;
    26             if(link[i] == 0 || dfs(link[i]))
    27             {
    28                 link[i] = x;
    29                 return true;
    30             }
    31         }
    32     }
    33     return false;
    34 }
    35 int main()
    36 {
    37     while(scanf("%d%d",&n,&m) != EOF)
    38     {
    39         for(int i = 0; i <= n; i++)
    40         {
    41             father[i] = i;
    42             a[i] = 0;
    43         }
    44         memset(g,0,sizeof(g));
    45         memset(link,0,sizeof(link));
    46         int flag = 0,ans = 0;
    47         while(m--)
    48         {
    49             int x,y,fx,fy;
    50             scanf("%d%d",&x,&y);
    51             g[x][y] = g[y][x] = 1;
    52             fx = find_father(x);
    53             fy = find_father(y);
    54             if(fx != fy)
    55             {
    56                 father[fy] = fx;
    57                 if(a[x] == 0)
    58                 {
    59                     a[fy] = 1 - a[y];
    60                 }
    61                 else
    62                     a[fy] = a[y];
    63             }
    64             else
    65             {
    66                 if(a[x] == a[y])
    67                 {
    68                     flag = 1;
    69                 }
    70             }
    71         }
    72         if(flag)
    73         {
    74             printf("No
    ");
    75         }
    76         else
    77         {
    78             for(int i = 1; i <= n; i++)
    79             {
    80                 memset(vis,0,sizeof(vis));
    81                 if(dfs(i))
    82                     ans++;
    83             }
    84             printf("%d
    ",ans/2);
    85         }
    86     }
    87 
    88     return 0;
    89 }
    View Code
  • 相关阅读:
    UVA 12307 Smallest Enclosing Rectangle
    UVALive 4728 Squares
    扩栈代码
    uva 10256 The Great Divide
    uva 11168 Airport
    uva 10625 Board Wrapping
    bzoj千题计划206:bzoj1076: [SCOI2008]奖励关
    NOIP2017 列队
    bzoj3529: [Sdoi2014]数表
    bzoj1966: [Ahoi2005]VIRUS 病毒检测
  • 原文地址:https://www.cnblogs.com/zhaopAC/p/5011690.html
Copyright © 2011-2022 走看看