Input
The first line of the input contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains N (1 <= N <= 150) and M (1 <= M <= 50) followed by N lines, each for one student. The format of these lines is Ri, Mi, K, Fi1, 锟斤拷, FiK in this order. In this line, that is for student i, Ri is his/her region number, Mi is his/her score in the entrance exam, K is the number of FDUs in his/her priority list (0 <= K <= M), and his/her priority list containing the FDU numbers in order of interest. Then there are M lines, one for each FDU. Each line contains Ri, and Ci in that order, which respectively is region number of Fi (the ith FDU) and the capacity of Fi. Note that region numbers are arbitrary integers.
Output
Outputs for different test cases are separated by exactly one blank line. For each test case, you should write N lines, one for each of the N students. If student i has been accepted to FDU Fj, then ith line should contain j, and not accepted, if that student has not been accepted in any FDU of his/her interest.
Sample Input
1
9 2
1 100 2 1 2
2 80 2 2 1
1 90 1 1
2 40 1 2
2 50 1 1
1 60 1 2
2 75 1 1
1 95 1 1
2 30 1 2
1 3
2 4
Sample Output
1
2
1
2
not accepted
2
not accepted
1
2
此题相比一般的稳定婚姻问题来说,唯一的区别是一个学校可以接纳多个学生,从算法角度来讲,只需要对我们的Gale_Shapley算法做一点小小的改动即可。
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define N 200 #define eps 1e-6 using namespace std; struct node { int id,region,score,k; }stu[N]; struct node1 { int region,num; }sch[N]; struct node2 { int cnt; int manname[N]; }woman[N]; int r,n,m; int map1[N][N],map2[N][N],man[N],times[N],vis[N]; int cmp(node a,node b) { if(a.region==r) { if(b.region==r) return a.score>b.score; if(b.region!=r) { if(abs(a.score-0.7*b.score)<eps) return 0; else return a.score>0.7*b.score; } } else if(b.region==r) { if(a.region==r) return a.score>b.score; if(a.region!=r) { if(abs(b.score-0.7*a.score)<eps) return 1; else return b.score<0.7*a.score; } } else return a.score>b.score; } int cmp1(node a,node b) { return a.id<b.id; } void makerank() { int i,j; for(i=1;i<=m;i++) { r=sch[i].region; sort(stu+1,stu+n+1,cmp); for(j=1;j<=n;j++) map2[i][stu[j].id]=j; } sort(stu+1,stu+n+1,cmp1); } void Gale_Shapley() { int flag=1,i,j,worstrank,worstrankj; memset(man,0,sizeof(man)); for(i=1;i<=n;i++) times[i]=1; for(i=1;i<=n;i++) vis[i]=0; for(i=1;i<=m;i++) woman[i].cnt=0; while(flag) { flag=0; for(i=1;i<=n;i++) { while(!man[i]&&!vis[i]) { flag=1; if(times[i]>stu[i].k) { vis[i]=1; break; } int name=map1[i][times[i]]; if(woman[name].cnt<sch[name].num) { woman[name].manname[++woman[name].cnt]=i; man[i]=name; times[i]++; } else if(woman[name].cnt==sch[name].num) { worstrank=0; for(j=1;j<=woman[name].cnt;j++) { if(map2[name][woman[name].manname[j]]>worstrank) { worstrank=map2[name][woman[name].manname[j]]; worstrankj=j; } } if(map2[name][i]<worstrank) { man[woman[name].manname[worstrankj]]=0; woman[name].manname[worstrankj]=i; man[i]=name; times[i]++; } else times[i]++; } } } } } int main() { int T,j,i; scanf("%d",&T); while(T--) { memset(map1,0,sizeof(map1)); memset(map2,0,sizeof(map2)); scanf("%d%d",&n,&m); for(i=1;i<=n;i++) { stu[i].id=i; scanf("%d%d%d",&stu[i].region,&stu[i].score,&stu[i].k); for(j=1;j<=stu[i].k;j++) { scanf("%d",&map1[i][j]); } } for(i=1;i<=m;i++) { scanf("%d%d",&sch[i].region,&sch[i].num); } makerank(); Gale_Shapley(); for(i=1;i<=n;i++) if(vis[i]) printf("not accepted\n"); else printf("%d\n",man[i]); if(T) printf("\n"); } return 0; }