hdu1083 Java 二分图

来源:互联网 发布:日本留学的好专业知乎 编辑:程序博客网 时间:2024/06/05 23:47

Courses

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


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
23 33 1 2 32 1 21 13 32 1 32 1 31 1
 

Sample Output
YESNO
 

import java.util.Scanner;public class Main {    public static int p = 0, n = 0;    public boolean dfs(int[][] map, boolean[] used, int[] linked, int start) {        for (int i = 1; i <= n; i++) {            if (used[i] == false && map[start][i] == 1) {                used[i] = true;                if (linked[i] == -1 || dfs(map, used, linked, linked[i])) {                    linked[i] = start;                    return true;                }            }        }        return false;    }    public int getMaxNum(int[][] map) {        int count = 0;        int[] linked = new int[303];        for (int i = 0; i < 303; i++)            linked[i] = -1;        for (int i = 1; i <= p; i++) {            boolean[] used = new boolean[303];            if (dfs(map, used, linked, i))                count++;        }        return count;    }    @SuppressWarnings("resource")    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        while (sc.hasNext()) {            int t = sc.nextInt();                    for (int i = 0; i < t; i++) {                p = sc.nextInt();                n = sc.nextInt();                if (p > n) {                    System.out.println("NO");                    continue;                }    Main q = new Main();                int map[][] = new int[303][303];                for (int j = 0; j < p; j++) {                    int x = sc.nextInt();                    for (int k = 0; k < x; k++) {                        int y = sc.nextInt();                        map[j + 1][y] = 1;                    }                }                int qq = q.getMaxNum(map);                if (qq == p) {                    System.out.println("YES");                } else                    System.out.println("NO");            }        }    }}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 期望薪资说高了怎么办 面试工资说低了怎么办 期望薪资谈低了怎么办 请年假公司不批怎么办 期望工资填低了怎么办 面试工资要高了怎么办 找工作期望薪资写低了怎么办 期望工资写少了怎么办 不给工人发工资怎么办 天亮了怎么办我好想你 亲爱的我想你我怎么办 人在澳大利亚悉尼找不到了怎么办 红米手机忘记手势密码怎么办 捡到苹果手机怎么办才能自己用 日语会读不会写怎么办 手术后nbp过低怎么办 我的手破了怎么办英文 平板手机屏坏了怎么办 他很优秀我该怎么办 洗澡的花洒漏水怎么办 高三了文科成绩很差怎么办 骑缝章最后一页没盖全怎么办 机票取早了没有登机口怎么办 机票早订比晚订贵怎么办? 孩子考差了父母怎么办 保险公司不给业务员办退司怎么办 我不习惯没有你我怎么办 锁坏了打不开了怎么办 要上班老人生病无人照顾怎么办 苹果手机一直说英文怎么办 公司很抠门怎么办英文怎么说 过了截港时间怎么办 截关日期是假日怎么办 恒温阀冷水进水堵塞怎么办 缺氧液泵管道堵塞怎么办 货物包装大集装箱装不下怎么办 微信收藏的视频格式错误怎么办 乙方被刑拘房租未付清怎么办 房贷银行卡号弄错怎么办 社保卡号弄错了怎么办 社保名字写错了怎么办