zoukankan      html  css  js  c++  java
  • 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;
    }
    
    
    
    
    






  • 相关阅读:
    nullptr 与 null
    nullptr 与 null
    UNREFERENCED_PARAMETER 的作用
    Kali-Linux无线网络渗透测试-李亚伟-第3章-监听WiFi网络--虚拟机使用无线网卡
    探寻Linux背后的“美丽心灵”——Linux创始人Linus Torvalds访谈录
    Angular2(Beta)入门
    angular2 will be kiiled by react in the future?
    《Linux设备驱动开发详解(第3版)》(即《Linux设备驱动开发详解:基于最新的Linux 4.0内核》)前言
    Angular vs React – so which do I chose?
    2015-2016前端知识体系
  • 原文地址:https://www.cnblogs.com/zswbky/p/6717978.html
Copyright © 2011-2022 走看看