• Courses(二分图水题)


    Courses
    Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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

    题意:有P门课,N个学生,求能否求出一个组满足:每门课只能对应一个人,但是单个人可以对应多门课 且人数(or课程)等于P。

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    
    using namespace std;
    
    int map[502][502];
    
    bool visited[502]; //标记男生是否被访问
    int match[502];    //女生和男生的匹配情况
    
    int n,m;
    
    bool find(int i)   //查找当前的i是否可以匹配
    {
        int j;
        for(j=1;j<=n;j++)
        {
            if(map[i][j]&&!visited[j])//在这里很有必要说一下visited这个数组,因为在之后的递归中,就会把这个标记好了的对象默认为
    			                      //已经和上一次的对象匹配过了,这样就不会再访问这个对象了,这在好几个的连续递归中显得尤为重要
            {
                visited[j]=1;
                if(match[j]==-1||find(match[j]))  //在此次查找的时候其实如果已经匹配过了则会在调用的时候即使是已经匹配成功了,由于
    				                               //当时在匹配的时候已经对可以匹配成功的做了match的标记了,就会继续查找他的下一个
    											   //能够匹配的对象
                {
                    match[j]=i;
                    return 1;
                }
            }
        }
        return 0;
    }
    
    int main()
    {
        int k,x,y,ans,tmp;
        int T;
        scanf("%d",&T);
        while(T--)
        {
            ans=0;
            memset(map,0,sizeof(map));
            memset(match,-1,sizeof(match));
            scanf("%d%d",&m,&n);
            for(int i=1;i<=m;i++)//对有意思的进行初始化
            {
                scanf("%d",&tmp);
                for(int j=0;j<tmp;j++){
                   scanf("%d",&y);
                   map[i][y]=1;
                }
            }
            for(int i=1;i<=m;i++)
            {
                memset(visited,0,sizeof(visited));//开始标记为全部没有访问
                if(find(i))    //查找当前的i是否可以匹配成功
                    ans++;
            }
            printf("%s
    ",ans>=m ? "YES" : "NO");
        }
        return 0;
    }
    
    
    
    
    






  • 相关阅读:
    分解让复杂问题简单化:字符串的排列
    分解让复杂问题简单化:二叉搜索树与双向链表
    分解让复杂问题简单化:复杂链表的复制
    举例让抽象问题具体化:二叉树中和为某一值的路径
    举例让抽象问题具体化:二叉搜索树的后序遍历序列
    Java Collection Framework
    Spring Boot 项目部署到本地Tomcat,出现访问路径问题
    happens-before规则
    NoClassDefFoundError
    《Java编程思想》笔记 第十六章 数组
  • 原文地址:https://www.cnblogs.com/zswbky/p/6717978.html
Copyright © 2020-2023  润新知