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): 13402    Accepted Submission(s): 6293

    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
    裸的二分匹配,但是我却去写最大流了2333333,真的傻,还调试了一阵子。。
    借这个题学习了一下匈牙利算法。。现在写题都注重板子了23333
    建两个集合,跑一遍匈牙利算法就行了
     1 #include <algorithm>
     2 #include <iostream>
     3 #include <iomanip>
     4 #include <cstring>
     5 #include <climits>
     6 #include <complex>
     7 #include <fstream>
     8 #include <cassert>
     9 #include <cstdio>
    10 #include <bitset>
    11 #include <vector>
    12 #include <deque>
    13 #include <queue>
    14 #include <stack>
    15 #include <ctime>
    16 #include <set>
    17 #include <map>
    18 #include <cmath>
    19 using namespace std;
    20 typedef struct Edge {
    21     int u, v, c, next;
    22 }Edge;
    23 const int inf = 0x7f7f7f7f;
    24 const int maxn = 10000;
    25 #define maxm 30000
    26 #define ll long long
    27 #define debug(x) cout<<"x="<<x<<endl
    28 #define mem(a,b) memset(a,b,sizeof a)
    29 inline int read()
    30 {
    31     int x=0,f=1;
    32     char ch=getchar();
    33     while(ch<'0'||ch>'9')
    34     {
    35         if(ch=='-') f=-1;
    36         ch=getchar();
    37     }
    38     while(ch>='0'&&ch<='9')
    39     {
    40         x=10*x+ch-'0';
    41         ch=getchar();
    42     }
    43     return x*f;
    44 }
    45 int  n,m,s,t,tot=0,head[maxn],l,r,match[maxn];
    46 bool vis[maxn];
    47 struct edge{int to,next;}e[maxm];
    48 void ins(int x,int y){e[tot].to=y;e[tot].next=head[x];head[x]=tot++;}
    49 bool dfs(int u)
    50 {
    51     for(int i=head[u];i!=-1;i=e[i].next)
    52     {
    53         int v=e[i].to;
    54         if(vis[v]) continue;
    55         vis[v]=true;
    56         if(match[v]==-1||dfs(match[v]))
    57         {
    58             match[v]=u;
    59             return true;
    60         }
    61     }
    62     return false;
    63 }
    64 int hungary(int x,int y)
    65 {
    66     l=x;r=y;
    67     int ans=0;
    68     mem(match,-1);
    69     for(int u=1;u<=l;++u)
    70     {
    71         mem(vis,0);
    72         if(dfs(u)) ans++;
    73     }
    74     return ans;
    75 }
    76 void init()
    77 {
    78    mem(head,-1);
    79    tot=0;
    80 }
    81 int main() {
    82     int k;
    83     while(~scanf("%d",&k)&&k){
    84     init();
    85     for(int i=0;i<k;++i)
    86     {
    87         int index,num,x;
    88         scanf("%d: (%d) ",&index,&num);
    89         for(int j=1;j<=num;++j) x=read(),
    90         ins(i+1,x+1+k);
    91     }
    92     int hh=hungary(k,k);
    93     //debug(hh);
    94     hh/=2;
    95     cout<<k-hh<<endl;
    96     }
    97     return 0;
    98 }
  • 相关阅读:
    是什么阻止你达成人生的目标?
    安装cocoa pods
    Mac安装mysql
    Mac安装jee开发环境,webservice环境搭建
    Mac使用Github
    从一个app开始学iOS
    Macbook Pro开机黑屏了。
    U盘安装OSX
    数据分析有时候的结果并不令人满意
    vmvare fusion 8
  • 原文地址:https://www.cnblogs.com/TYH-TYH/p/9437526.html
Copyright © 2011-2022 走看看