首页 > 代码库 > HDU 3729 二分匹配匈牙利算法
HDU 3729 二分匹配匈牙利算法
I‘m Telling the Truth
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1482 Accepted Submission(s): 740
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 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.
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, Xi and Yi (1 <= Xi <= Yi <= 100000), means the i-th student’s rank is between Xi and Yi, 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
题目意思:
每个学生有个排名,然后有一部分学生说的是真话,有一部分说的是假话,求说真话的学生最多的数目,并按照从小到大排序输出(若最多的数目有多个,那么学生编号字典序最大的输出)
思路:
一个学生和一个排名对应,在学生和排名之间连线,那么就是求二分图最大匹配了,因为多个答案相同需要字典序最大的输出,那么匹配时按照学生编号降序匹配。
代码:
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <iostream> 5 #include <vector> 6 #include <queue> 7 using namespace std; 8 9 #define N 100000510 11 int from[N], l[65], r[65];12 bool visited[N];13 int n;14 vector<int>ve[65];15 16 int march(int u){17 int v, i;18 for(i=0;i<ve[u].size();i++){19 v=ve[u][i];20 if(!visited[v]){21 visited[v]=true;22 if(from[v]==-1||march(from[v])){23 from[v]=u;24 return 1;25 }26 }27 }28 return 0;29 }30 31 main()32 {33 int t, i, j, k, x, y;34 cin>>t;35 while(t--){36 scanf("%d",&n);37 for(i=1;i<=n;i++){38 scanf("%d %d",&l[i],&r[i]);39 }40 for(i=1;i<=n;i++) ve[i].clear();41 for(i=1;i<=n;i++){42 for(j=l[i];j<=r[i];j++){43 ve[i].push_back(j);44 }45 }46 int num=0;47 int ans[65];48 k=0;49 memset(from,-1,sizeof(from));50 for(i=n;i>=1;i--){51 memset(visited,false,sizeof(visited));52 if(march(i)){ //若有增广路径 说明匹配成功 53 num++;54 ans[k++]=i;55 }56 }57 sort(ans,ans+k);58 printf("%d\n%d",num,ans[0]);59 for(i=1;i<k;i++) printf(" %d",ans[i]);60 cout<<endl;61 }62 }
HDU 3729 二分匹配匈牙利算法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。