java实现二分图匹配匈牙利算法(HDU1083)
来源:互联网 发布:电子双缝干涉实验知乎 编辑:程序博客网 时间:2024/06/06 00:35
Courses
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8443 Accepted Submission(s): 4114
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
YESNOimport java.lang.reflect.Array;import java.util.List;import java.util.ArrayList;import java.util.Arrays;import java.util.Scanner;import java.util.Stack;public class Main { public static int n = 0, p = 0; public boolean dfs(int[][] graph, boolean[] used, int[] linked, int start) { for (int i = 1; i <= n; i++) { if (used[i] == false && graph[start][i] == 1) { used[i] = true; if (linked[i] == -1 || dfs(graph, used, linked, linked[i])) { linked[i] = start; return true; } } } return false; } public int getMaxNum(int[][] graph) { int count = 0; int[] linked = new int[503]; Arrays.fill(linked, -1); for (int i = 1; i <= p; i++) { boolean[] used = new boolean[503]; if (dfs(graph, used, linked, i)) count++; } return count; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for (int k = 0; k < T; k++) { p = in.nextInt(); n = in.nextInt(); if (p > n){ System.out.println("NO"); continue; } int[][] graph = new int[503][503]; Main test = new Main(); for (int i = 1; i <= p; i++) {//构图 int a = in.nextInt(); for (int j = 1; j <= a; j++) { int b = in.nextInt(); graph[i][b] = 1; } } if (test.getMaxNum(graph) == p) { System.out.println("YES"); } else { System.out.println("NO"); } } }}
阅读全文
0 0
- java实现二分图匹配匈牙利算法(HDU1083)
- HDU1083(二分图+最大匹配+匈牙利算法)
- HDU1083+POJ1469 (匈牙利算法+最大二分匹配)
- hdu1083(二分图匹配)
- hdu1083(二分图,匈牙利模板题)
- hdu1083二分图匹配
- HDU1083--Courses(二分图最大匹配)
- HDU1083---Courses (二分图匹配)
- 二分图匹配(匈牙利算法DFS实现)
- 二分图最大匹配(匈牙利算法)
- 二分图最大匹配(匈牙利算法)
- 二分图最大匹配(匈牙利算法)
- 匈牙利算法(二分图最大匹配)
- 二分图最大匹配(匈牙利算法)
- 二分图匹配(匈牙利算法)
- 二分图最大匹配(匈牙利算法)
- 二分图最大匹配(匈牙利算法)
- 二分图匹配(匈牙利算法)
- 莫比乌斯反演知识点及其理解
- SSL CA客户端证书双向认证IIS配置及代码验证
- 微商城的魅力
- 【备忘】传智播客最新大数据第3期实战培训完整版视频教程
- C++后端开发面试题
- java实现二分图匹配匈牙利算法(HDU1083)
- Failed to complete obtain psql count Master gp_segment_configuration Script Exiti
- Spring Boot干货系列:(一)优雅的入门篇
- QNX驱动开发—中断处理,中断响应机制
- 最大总和
- 加油站
- 51Nod-1008-N的阶乘 mod P
- python 实现分页显示从es中获取的数据
- Python2编码问题