zoukankan      html  css  js  c++  java
  • BNUOJ 1585 Girls and Boys

    Girls and Boys

    Time Limit: 5000ms
    Memory Limit: 10000KB
    This problem will be judged on PKU. Original ID: 1466
    64-bit integer IO format: %lld      Java class name: Main
     
     
    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

    解题:找出相互没关系的一个人数最大的集合。首先,是点!让点多,我们可以求点小!最小点覆盖!那么我们可以求最大匹配数!无向图的最大匹配数要处以2.。。。就这样了

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 const int maxn = 1005;
    18 struct arc{
    19     int to,next;
    20 };
    21 arc g[200100];
    22 int head[maxn],from[maxn],tot,n;
    23 bool vis[maxn];
    24 void add(int u,int v){
    25     g[tot].to = v;
    26     g[tot].next = head[u];
    27     head[u] = tot++;
    28 }
    29 bool dfs(int u){
    30     for(int i = head[u]; i != -1; i = g[i].next){
    31         if(!vis[g[i].to]){
    32             vis[g[i].to] = true;
    33             if(from[g[i].to] == -1 || dfs(from[g[i].to])){
    34                 from[g[i].to] = u;
    35                 return true;
    36             }
    37         }
    38 
    39     }
    40     return false;
    41 }
    42 int main() {
    43     int i,j,k,u,v,ans;
    44     while(~scanf("%d",&n)){
    45         memset(head,-1,sizeof(head));
    46         memset(from,-1,sizeof(from));
    47         tot = 0;
    48         for(i = 0; i < n; i++){
    49             scanf("%d: (%d)",&j,&k);
    50             for(j = 0; j < k; j++){
    51                 scanf("%d",&v);
    52                 add(i,v);
    53                 add(v,i);
    54             }
    55         }
    56         for(ans = i = 0; i < n; i++){
    57             memset(vis,false,sizeof(vis));
    58             if(dfs(i)) ans++;
    59         }
    60         ans >>= 1;
    61         printf("%d
    ",n-ans);
    62     }
    63     return 0;
    64 }
    View Code
  • 相关阅读:
    Python学习(五)函数 —— 自定义函数
    Python学习(四)数据结构 —— dict
    Python学习(四)数据结构 —— set frozenset
    C++ —— 构建开源的开发环境
    Oracle 数据库 Database Express Edition 11g Release 2 (11.2) 错误解决集锦(使用语法)
    Web —— tomcat 问题解决
    web —— jsp笔记
    Web 前端 —— javaScript
    编程杂谈—— 浮点数
    Web —— java web 项目开发 笔记
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3907066.html
Copyright © 2011-2022 走看看