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
  • 相关阅读:
    Jquery-EasyUI学习2~
    IIS——发布网站
    一致性哈希算法
    利用ZTree链接数据库实现 [权限管理]
    Form表单提交的简要方式
    Redis学习之5种数据类型操作、实现原理及应用场景
    redis对比其余数据库
    ZooKeeper概述(转)
    Zookeeper-Zookeeper可以干什么
    Java内存分配及变量存储位置实例讲解
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3602148.html
Copyright © 2011-2022 走看看