I'm Telling the Truth
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1033 Accepted Submission(s): 510
Problem Description
After this year’s college-entrance exam, the teacher did a survey in his class on students’ score. There are n students in the class. The students didn’t want to tell their
teacher their exact score; they only told their teacher their rank in the province (in the form of intervals).
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
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.
Input
There 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, X
i
and Y
i
(1 <= X
i
<= Y
i
<= 100000), means the i-th student’s rank is between X
i
and Y
i, inclusive.
Output
Output 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
2
4
5004 5005
5005 5006
5004 5006
5004 5006
7
4 5
2 3
1 2
2 2
4 4
2 3
3 4
Sample Output
3
2 3 4
5
1 3 5 6 7
一个水的二分匹配题,我用的是DFS找增广路算法:
1 #include <iostream> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <algorithm> 6 #include <string.h> 7 #include <set> 8 using namespace std; 9 typedef struct abcd 10 { 11 int x,y; 12 }abcd; 13 abcd a[100]; 14 int b[100001]; 15 int c[100001]; 16 set<int> s; 17 int dfs(int t) 18 { 19 int i; 20 for(i=a[t].x;i<=a[t].y;i++) 21 { 22 if(!c[i]) 23 { 24 c[i]=1; 25 if(!b[i]||dfs(b[i])) 26 { 27 b[i]=t; 28 s.insert(t); 29 return 1; 30 } 31 } 32 } 33 return 0; 34 } 35 int main() 36 { 37 int n,m,i,j; 38 cin>>n; 39 for(i=0;i<n;i++) 40 { 41 s.clear(); 42 memset(b,0,sizeof(b)); 43 cin>>m; 44 for(j=1;j<=m;j++) 45 { 46 cin>>a[j].x>>a[j].y; 47 } 48 int sum=0; 49 for(j=m;j>0;j--) 50 { 51 memset(c,0,sizeof(c)); 52 if(dfs(j)) 53 sum++; 54 } 55 cout<<sum<<endl; 56 set<int> ::iterator it; 57 for(it=s.begin();it!=s.end();it++) 58 { 59 sum--; 60 if(sum) 61 cout<<(*it)<<' '; 62 else cout<<(*it)<<endl; 63 } 64 } 65 }