I'm Telling the Truth
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 939 Accepted Submission(s): 472Problem DescriptionAfter 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 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 Xi and 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 Input245004 50055005 50065004 50065004 500674 52 31 22 24 42 33 4Sample Output32 3 451 3 5 6 7
此题考虑的是最大匹配,然后要按字典序从大到小输出方案。
那匹配的时候就就从num->1,逐个进行匹配,这样能够保证字典序的最大值。
1 #include2 #include 3 #include 4 #include 5 #include 6 using namespace std; 7 8 #define INF 99999999 9 struct Node 10 { 11 int estart; 12 int eend; 13 }fpoint[100001]; 14 int cx[100001]; 15 int cy[100001]; 16 int mk[100001]; 17 int n; 18 int m; 19 20 void init() 21 { 22 memset(cx,0xff,sizeof(cx)); 23 memset(cy,0xff,sizeof(cy)); 24 memset(mk,0,sizeof(mk)); 25 } 26 27 int path(int u) 28 { 29 int v; 30 for(v=fpoint[u].estart;v<=fpoint[u].eend;v++) 31 { 32 if(!mk[v]) 33 { 34 mk[v]=1; 35 if(cy[v]==-1||path(cy[v])) 36 { 37 cx[u]=v; 38 cy[v]=u; 39 return 1; 40 } 41 } 42 } 43 return 0; 44 } 45 46 int maxmatch() 47 { 48 int i; 49 int sum=0; 50 for(i=m;i>=1;i--) 51 { 52 if(cx[i]==-1) 53 { 54 memset(mk,0,sizeof(mk)); 55 sum+=path(i); 56 } 57 } 58 return sum; 59 } 60 61 int t; 62 63 int main() 64 { 65 scanf("%d",&t); 66 while(t--) 67 { 68 init(); 69 int num; 70 int s,e; 71 scanf("%d",&num); 72 int cnt=1; 73 int mmax=-1; 74 int mmin=INF; 75 m=num; 76 while(num--) 77 { 78 scanf("%d%d",&s,&e); 79 if(mmax s)mmin=s; 82 if(mmin>e)mmin=e; 83 fpoint[cnt].estart=s; 84 fpoint[cnt++].eend=e; 85 } 86 int ans=maxmatch(); 87 printf("%d\n",ans); 88 int i; 89 cnt=1; 90 for(i=1;i<=m;i++) 91 { 92 if(cx[i]!=-1) 93 { 94 if(cnt