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
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.
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 1000005 10 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 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。