HDU 3729 I'm Telling the Truth (最小路径覆盖=顶点数-最大匹配数)
I'm Telling the Truth
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1050 Accepted Submission(s): 520
After asking all the students, the teacher found that some students didn’t tell the truth. For example, Student1 said he was between 5004th and 5005th, Student2 said he was between 5005th and 5006th, Student3 said he was between 5004th and 5006th, Student4 said he was between 5004th and 5006th, too. This situation is obviously impossible. So at least one told a lie. Because the teacher thinks most of his students are honest, he wants to know how many students told the truth at most.
InputThere is an integer in the first line, represents the number of cases (at most 100 cases). In the first line of every case, an integer n (n <= 60) represents the number of students. In the next n lines of every case, there are 2 numbers in each line, Xi and Yi (1 <= Xi <= Yi <= 100000), means the i-th student’s rank is between Xiand Yi, inclusive.
OutputOutput 2 lines for every case. Output a single number in the first line, which means the number of students who told the truth at most. In the second line, output the students who tell the truth, separated by a space. Please note that there are no spaces at the head or tail of each line. If there are more than one way, output the list with maximum lexicographic. (In the example above, 1 2 3;1 2 4;1 3 4;2 3 4 are all OK, and 2 3 4 with maximum lexicographic)
Sample Input
245004 50055005 50065004 50065004 500674 52 31 22 24 42 33 4
Sample Output
32 3 451 3 5 6 7
import java.io.*;import java.util.*;public class Main {int t,n;int MAX=100010;int link[]=new int[MAX];int map[]=new int[MAX];boolean[] mark=new boolean[MAX];Node node[];public static void main(String[] args) {new Main().work();}void work(){Scanner sc=new Scanner(new BufferedInputStream(System.in));t=sc.nextInt();while(t--!=0){n=sc.nextInt();node=new Node[n+1];for(int i=1;i<=n;i++){int min=sc.nextInt();int max=sc.nextInt();node[i]=new Node(min,max);}hungary();}}//匈牙利算法void hungary(){Arrays.fill(link,0);Arrays.fill(map,0);int ans=0;for(int i=n;i>=1;i--){Arrays.fill(mark,false);if(DFS(i))map[ans++]=i;}//输出System.out.println(ans);for(int i=ans-1;i>=0;i--){System.out.print(map[i]);if(i!=0)System.out.print(" ");}System.out.println();}boolean DFS(int x){for(int i=node[x].min;i<=node[x].max;i++){if(!mark[i]){mark[i]=true;if(link[i]==0||DFS(link[i])){link[i]=x;return true;}}}return false;}class Node{int min;int max;Node(int min,int max){this.min=min;this.max=max;}}}