zoukankan      html  css  js  c++  java
  • hdu 1068 Girls and Boys (最大独立集)

    Girls and Boys
    Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 13556    Accepted Submission(s): 6385

    Problem Description
    the second year of the university somebody started a study on the romantic relations between the students. The relation “romantically involved” is defined between one girl and one boy. For the study reasons it is necessary to find out the maximum set satisfying the condition: there are no two students in the set who have been “romantically involved”. The result of the program is the number of students in such a set.
    The input contains several data sets in text format. Each data set represents one set of subjects of the study, with the following description:
    the number of students
    the description of each student, in the following format
    student_identifier:(number_of_romantic_relations) student_identifier1 student_identifier2 student_identifier3 ...
    or
    student_identifier:(0)
    The student_identifier is an integer number between 0 and n-1, for n subjects.
    For each given data set, the program should write to standard output a line containing the result.
     
    Sample Input
    7
    0: (3) 4 5 6
    1: (2) 4 6
    2: (0)
    3: (0)
    4: (2) 0 1
    5: (1) 0
    6: (2) 0 1
    3
    0: (2) 1 2
    1: (1) 0
    2: (1) 0
     
    Sample Output
    5
    2

    C/C++:

     1 #include <map>
     2 #include <queue>
     3 #include <cmath>
     4 #include <vector>
     5 #include <string>
     6 #include <cstdio>
     7 #include <cstring>
     8 #include <climits>
     9 #include <iostream>
    10 #include <algorithm>
    11 #define INF 0xffffff
    12 using namespace std;
    13 const int my_max = 1005;
    14 
    15 int n, m, a, b, my_G[my_max][my_max], my_book[my_max], my_right[my_max];
    16 
    17 bool my_dfs(int x)
    18 {
    19     for (int i = 0; i < n; ++ i)
    20     {
    21         if (my_G[x][i] && !my_book[i])
    22         {
    23             my_book[i] = 1;
    24             if (!my_right[i] || my_dfs(my_right[i]))
    25             {
    26                 my_right[i] = x;
    27                 return true;
    28             }
    29         }
    30     }
    31     return false;
    32 }
    33 
    34 int my_hungarian()
    35 {
    36     int my_cnt = 0;
    37     for (int i = 0; i < n; ++ i)
    38     {
    39         memset (my_book, 0, sizeof(my_book));
    40         if (my_dfs(i))
    41             ++ my_cnt;
    42     }
    43     return my_cnt / 2;
    44 }
    45 
    46 int main()
    47 {
    48     while (~scanf("%d", &n))
    49     {
    50         memset(my_right, 0, sizeof(my_right));
    51         memset(my_G, 0, sizeof(my_G));
    52         for (int i = 0; i < n; ++ i)
    53         {
    54             scanf("%d: (%d)", &a, &m);
    55             while (m --)
    56             {
    57                 scanf("%d", &b);
    58                 my_G[a][b] = 1;
    59             }
    60         }
    61 
    62         printf("%d
    ", n - my_hungarian());
    63     }
    64     return 0;
    65 }
  • 相关阅读:
    程序打印的日志哪里去了?结合slf4j来谈谈面向接口编程的重要性
    vue项目用npm安装sass包遇到的问题及解决办法
    nginx反向代理配置及常见指令
    你以为你以为的就是你以为的吗?记一次服务器点对点通知的联调过程
    jeecg逆向工程代码的生成及常见问题
    java注解
    终于有了,史上最强大的数据脱敏处理算法
    SpringBoot项目下的JUnit测试
    递归方法
    练习题
  • 原文地址:https://www.cnblogs.com/GetcharZp/p/9462268.html
Copyright © 2011-2022 走看看