zoukankan      html  css  js  c++  java
  • POJ 1466

    Girls and Boys
    Time Limit: 5000MS   Memory Limit: 10000K
    Total Submissions: 10008   Accepted: 4427

    Description

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

    Input

    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 (n <=500 ), for n subjects.

    Output

    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

    Source

     
    因为可以分为男女两个集合,所以可以构成一个二分图,然后求二分图的最大独立集
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 
     6 using namespace std;
     7 
     8 #define maxn 505
     9 
    10 int n;
    11 bool f[maxn][maxn],vis[maxn];
    12 int match[maxn];
    13 int ans;
    14 
    15 bool dfs(int u) {
    16         for(int i = 0; i < n; ++i) {
    17                 if(vis[i] || !f[u][i]) continue;
    18                 vis[i] = 1;
    19                 if(match[i] == -1 || dfs(match[i])) {
    20                         match[i] = u;
    21                         return true;
    22                 }
    23         }
    24 
    25         return false;
    26 }
    27 void solve() {
    28         for(int i = 0; i < n; ++i) match[i] = -1;
    29 
    30         for(int i = 0; i < n; ++i) {
    31                 memset(vis,0,sizeof(vis));
    32                 if(dfs(i)) ++ans;
    33         }
    34 }
    35 
    36 int main()
    37 {
    38    // freopen("sw.in","r",stdin);
    39 
    40     while(~scanf("%d",&n)) {
    41             memset(f,0,sizeof(f));
    42             ans = 0;
    43 
    44             for(int i = 0; i < n; ++i) {
    45                     int id,num;
    46                     scanf("%d: (%d)",&id,&num);
    47                    // puts(ch);
    48                     for(int j = 1; j <= num; ++j) {
    49                             int a;
    50                             scanf("%d",&a);
    51                             //printf("id = %d
    ",a);
    52                             f[id][a] = 1;
    53                             //f[a][id] = 1;
    54 
    55                     }
    56             }
    57 
    58 
    59             solve();
    60 
    61             //printf("ans = %d
    ",ans);
    62 
    63             printf("%d
    ",n - ans / 2);
    64     }
    65 
    66     return 0;
    67 }
    View Code
  • 相关阅读:
    linux系统分析工具续-SystemTap和火焰图(Flame Graph)
    如何使用strace+pstack利器分析程序性能
    MySQL数据类型-decimal详解
    服务器端数据合法性验证:签名sign和口令token原理
    linux系统下php通过php_oci8扩展连接oracle数据库 Nginx
    redis开启远程访问
    cURL函数库错误码说明之PHP curl_errno函数
    权限控制相关模块
    otool
    路由器
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3602148.html
Copyright © 2011-2022 走看看