zoukankan      html  css  js  c++  java
  • HDU1083(KB10-C 二分图最大匹配)

    Courses

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

    Problem Description

    Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is possible to form a committee of exactly P students that satisfies simultaneously the conditions:

    . every student in the committee represents a different course (a student can represent a course if he/she visits that course)

    . each course has a representative in the committee

    Your program should read sets of data from a text file. The first line of the input file contains the number of the data sets. Each data set is presented in the following format:

    P N
    Count1 Student1 1 Student1 2 ... Student1 Count1
    Count2 Student2 1 Student2 2 ... Student2 Count2
    ...... 
    CountP StudentP 1 StudentP 2 ... StudentP CountP

    The first line in each data set contains two positive integers separated by one blank: P (1 <= P <= 100) - the number of courses and N (1 <= N <= 300) - the number of students. The next P lines describe in sequence of the courses . from course 1 to course P, each line describing a course. The description of course i is a line that starts with an integer Count i (0 <= Count i <= N) representing the number of students visiting course i. Next, after a blank, you'll find the Count i students, visiting the course, each two consecutive separated by one blank. Students are numbered with the positive integers from 1 to N.

    There are no blank lines between consecutive sets of data. Input data are correct.

    The result of the program is on the standard output. For each input data set the program prints on a single line "YES" if it is possible to form a committee and "NO" otherwise. There should not be any leading blanks at the start of the line.

    An example of program input and output:
     

    Sample Input

    2 3 3 3 1 2 3 2 1 2 1 1 3 3 2 1 3 2 1 3 1 1
     

    Sample Output

    YES NO
     

    Source

     
     1 //2017-08-21
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <iostream>
     5 #include <algorithm>
     6 
     7 using namespace std;
     8 
     9 int p, n;
    10 const int N = 1010;
    11 int head[N], tot;
    12 struct Edge{
    13     int to, next;
    14 }edge[N*N];
    15 
    16 void init(){
    17     tot = 0;
    18     memset(head, -1, sizeof(head));
    19 }
    20 
    21 void add_edge(int u, int v){
    22     edge[tot].to = v;
    23     edge[tot].next = head[u];
    24     head[u] = tot++;
    25 
    26     edge[tot].to = u;
    27     edge[tot].next = head[v];
    28     head[v] = tot++;
    29 }
    30 
    31 string G[N];
    32 int matching[N];
    33 int check[N];
    34 
    35 bool dfs(int u){
    36     for(int i =  head[u]; i != -1; i = edge[i].next){
    37         int v = edge[i].to;
    38         if(!check[v]){//要求不在交替路
    39             check[v] = 1;//放入交替路
    40             if(matching[v] == -1 || dfs(matching[v])){
    41                 //如果是未匹配点,说明交替路为增广路,则交换路径,并返回成功
    42                 matching[u] = v;
    43                 matching[v] = u;
    44                 return true;
    45             }
    46         }
    47     }
    48     return false;//不存在增广路
    49 }
    50 
    51 //hungarian: 二分图最大匹配匈牙利算法
    52 //input: null
    53 //output: ans 最大匹配数
    54 int hungarian(){
    55     int ans = 0;
    56     memset(matching, -1, sizeof(matching));
    57     for(int u = 0; u < p; u++){
    58         if(matching[u] == -1){
    59             memset(check, 0, sizeof(check));
    60             if(dfs(u))
    61               ans++;
    62         }
    63     }
    64     return ans;
    65 }
    66 
    67 int main()
    68 {
    69     //freopen("inputC.txt", "r", stdin);
    70     int T;
    71     scanf("%d", &T);
    72     while(T--){
    73         scanf("%d%d", &p, &n);
    74         init();
    75         int m, v;
    76         for(int i = 0; i < p; i++){
    77             scanf("%d", &m);
    78             while(m--){
    79                 scanf("%d", &v);
    80                 add_edge(i, p+v);
    81             }
    82         }
    83         int ans = hungarian();
    84         if(ans < p)printf("NO
    ");
    85         else printf("YES
    ");
    86     }
    87     return 0;
    88 }
  • 相关阅读:
    python实现七段数码管显示
    词频统计实例
    分形几何中科赫雪花的绘制
    脚本实现自动化绘制
    Android查看数据库方法及工具(转)
    2011年度总结
    Bad NPObject as private data 解决方案
    LINQ学习之旅——LINQ TO SQL继承支持(转载)
    我记录开源系统1.6源码解析(一)
    2011年度总结
  • 原文地址:https://www.cnblogs.com/Penn000/p/7404093.html
Copyright © 2011-2022 走看看