zoukankan      html  css  js  c++  java
  • *HDU 1068 二分图

    Girls and Boys

    Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 10929    Accepted Submission(s): 5100


    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
     
    Source
     
    代码:
    找二分图最大独立集。题目中描述的是无向边。
    代码:
     1 // 模板 匈牙利算法dfs 最大独立集=总结点数-最大匹配(有向图);最大独立集=总结点数-最大匹配/2(无向图),(图中任意两个顶点都不相连的顶点集合。)
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 using namespace std;
     6 int mp[1003][1003],vis[1003],link[1003];
     7 int Mu,Mv,n;  //左集合点数,右集合点数,总点数
     8 int dfs(int x)      //找增广路。
     9 {
    10     for(int i=0;i<Mv;i++)
    11     {
    12         if(!vis[i]&&mp[x][i])
    13         {
    14             vis[i]=1;
    15             if(link[i]==-1||dfs(link[i]))
    16             {
    17                 link[i]=x;
    18                 return 1;
    19             }
    20         }
    21     }
    22     return 0;
    23 }
    24 int Maxcon()
    25 {
    26     int ans=0;
    27     memset(link,-1,sizeof(link));  
    28     for(int i=0;i<Mu;i++)
    29     {
    30         memset(vis,0,sizeof(vis));
    31         if(dfs(i)) ans++;
    32     }
    33     return ans;
    34 }
    35 int main()
    36 {
    37     int a,b,c;
    38     while(scanf("%d",&n)!=EOF)
    39     {
    40         memset(mp,0,sizeof(mp));
    41         for(int i=0;i<n;i++)
    42         {
    43             scanf("%d: (%d)",&a,&c);
    44             while(c--)
    45             {
    46                 scanf("%d",&b);
    47                 mp[a][b]=mp[b][a]=1;
    48             }
    49         }
    50         Mu=Mv=n;
    51         printf("%d
    ",n-Maxcon()/2);
    52     }
    53     return 0;
    54 }
  • 相关阅读:
    《PostgreSQL 指南:内幕探索》之基础备份与时间点恢复(下)
    数据和云,半年文章精选(文末赠书)
    js for 循环跳过undefined值
    js for循环,会遇到undefined
    洛谷P3018 [USACO11MAR]树装饰Tree Decoration
    洛谷P3018 [USACO11MAR]树装饰Tree Decoration
    洛谷P3018 [USACO11MAR]树装饰Tree Decoration
    洛谷P3018 [USACO11MAR]树装饰Tree Decoration
    js特效:鼠标滑过图片时切换为动图
    js特效:鼠标滑过图片时切换为动图
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6066555.html
Copyright © 2011-2022 走看看