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:
. 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"); } } }}
阅读全文
0 0
- hdu1083 Java 二分图
- hdu1083二分图匹配
- java实现二分图匹配匈牙利算法(HDU1083)
- hdu1083(二分图匹配)
- hdu1083【二分图】【最大匹配】
- Courses HDU1083 -二分图匹配
- HDU1083 Courses(二分图匹配)
- Courses HDU1083(二分图的匹配)
- HDU1083(二分图+最大匹配+匈牙利算法)
- hdu1083 Courses ( 二分图最大匹配)
- hdu1083(二分图,匈牙利模板题)
- 【二分图匹配】HDU1083-Courses【模板题】
- HDU1083--Courses(二分图最大匹配)
- hdu1083培训班 ~~二分图最大匹配
- HDU1083---Courses (二分图匹配)
- 【二分匹配】 hdu1083 Courses
- hdu1083二分匹配基础
- 【二分匹配】HDU1083-Courses
- Java中Http连接的两种方式
- 详解易经64卦-傅佩荣有声系列2
- 集合(三)泛型
- NYOJ 18 The Triangle
- Rust :公钥、私钥与keypair、signature、verify 三部曲
- hdu1083 Java 二分图
- 百度语音——语音识别(简易教程)
- Js对象的学习
- http请求 405错误 方法不被允许 (Method not allowed)
- 好数
- CIFS文件系统
- Android中几种数据结构使用
- Unity多线程资源下载方案
- RPM软件管理程序介绍