ACM 第五天
匈牙利算法(二分图匹配)
C - Courses
Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is possible to form a committee of exactly P students that satisfies simultaneously the conditions:
. every student in the committee represents a different course (a student can represent a course if he/she visits that course)
. each course has a representative in the committee
Your program should read sets of data from a text file. The first line of the input file contains the number of the data sets. Each data set is presented in the following format:
P N
Count1 Student1 1 Student1 2 ... Student1 Count1
Count2 Student2 1 Student2 2 ... Student2 Count2
......
CountP StudentP 1 StudentP 2 ... StudentP CountP
The first line in each data set contains two positive integers separated by one blank: P (1 <= P <= 100) - the number of courses and N (1 <= N <= 300) - the number of students. The next P lines describe in sequence of the courses . from course 1 to course P, each line describing a course. The description of course i is a line that starts with an integer Count i (0 <= Count i <= N) representing the number of students visiting course i. Next, after a blank, you'll find the Count i students, visiting the course, each two consecutive separated by one blank. Students are numbered with the positive integers from 1 to N.
There are no blank lines between consecutive sets of data. Input data are correct.
The result of the program is on the standard output. For each input data set the program prints on a single line "YES" if it is possible to form a committee and "NO" otherwise. There should not be any leading blanks at the start of the line.
An example of program input and output:
. every student in the committee represents a different course (a student can represent a course if he/she visits that course)
. each course has a representative in the committee
Your program should read sets of data from a text file. The first line of the input file contains the number of the data sets. Each data set is presented in the following format:
P N
Count1 Student1 1 Student1 2 ... Student1 Count1
Count2 Student2 1 Student2 2 ... Student2 Count2
......
CountP StudentP 1 StudentP 2 ... StudentP CountP
The first line in each data set contains two positive integers separated by one blank: P (1 <= P <= 100) - the number of courses and N (1 <= N <= 300) - the number of students. The next P lines describe in sequence of the courses . from course 1 to course P, each line describing a course. The description of course i is a line that starts with an integer Count i (0 <= Count i <= N) representing the number of students visiting course i. Next, after a blank, you'll find the Count i students, visiting the course, each two consecutive separated by one blank. Students are numbered with the positive integers from 1 to N.
There are no blank lines between consecutive sets of data. Input data are correct.
The result of the program is on the standard output. For each input data set the program prints on a single line "YES" if it is possible to form a committee and "NO" otherwise. There should not be any leading blanks at the start of the line.
An example of program input and output:
Input
2 3 3 3 1 2 3 2 1 2 1 1 3 3 2 1 3 2 1 3 1 1Output
YES NOSample Input
2 3 3 3 1 2 3 2 1 2 1 1 3 3 2 1 3 2 1 3 1 1Sample Output
YES NO
1 #include<stdio.h> 2 #include<string.h> 3 #define INF 0x3f3f3f3f 4 5 int G[310][301],vis[310],used[310]; 6 int n,p; 7 bool find(int u)//核心部分:寻找匈牙利算法的增广路 8 { 9 int i; 10 for(i=1;i<=n;i++) 11 { 12 if(!vis[i] && G[u][i]) 13 { 14 vis[i]=1; 15 if(!used[i] || find(used[i])) 16 { 17 used[i]=u; 18 return true; 19 } 20 } 21 } 22 return false; 23 } 24 int main() 25 { 26 int a,b,i,ans,T; 27 scanf("%d",&T); 28 while(T--) 29 { 30 scanf("%d%d",&p,&n); 31 memset(G,0,sizeof(G)); 32 ans=0; 33 for(i=1;i<=p;i++) 34 { 35 scanf("%d",&a); 36 while(a--) 37 { 38 scanf("%d",&b); 39 G[i][b]=1; 40 } 41 } 42 memset(used,0,sizeof(used)); 43 for(i=1;i<=p;i++) 44 { 45 memset(vis,0,sizeof(vis)); 46 if(find(i)) ans++; 47 } 48 if(ans==p) printf("YES\n"); 49 else printf("NO\n"); 50 } 51 return 0; 52 }
B - The Accomodation of Students
There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other.
Now you are given all pairs of students who know each other. Your task is to divide the students into two groups so that any two students in the same group don't know each other.If this goal can be achieved, then arrange them into double rooms. Remember, only paris appearing in the previous given set can live in the same room, which means only known students can live in the same room.
Calculate the maximum number of pairs that can be arranged into these double rooms.
Now you are given all pairs of students who know each other. Your task is to divide the students into two groups so that any two students in the same group don't know each other.If this goal can be achieved, then arrange them into double rooms. Remember, only paris appearing in the previous given set can live in the same room, which means only known students can live in the same room.
Calculate the maximum number of pairs that can be arranged into these double rooms.
InputFor each data set:
The first line gives two integers, n and m(1<n<=200), indicating there are n students and m pairs of students who know each other. The next m lines give such pairs.
Proceed to the end of file.
OutputIf these students cannot be divided into two groups, print "No". Otherwise, print the maximum number of pairs that can be arranged in those rooms.
Sample Input
4 4 1 2 1 3 1 4 2 3 6 5 1 2 1 3 1 4 2 5 3 6Sample Output
No 3
1 #include<stdio.h> 2 #include<string.h> 3 #include <queue> 4 using namespace std; 5 bool vis[210]; 6 int G[210][201],used[210]; 7 int n,m; 8 int find(int u)//核心部分:寻找匈牙利算法的增广路 9 { 10 int i,n; 11 for(i=1; i<=n; i++) 12 { 13 if(!vis[i] && G[u][i]) 14 { 15 vis[i]=true; 16 if(!used[i] || find(used[i])) 17 { 18 used[i]=u; 19 return 1; 20 } 21 } 22 } 23 return 0; 24 } 25 26 int judge[210]; 27 bool bfs()//广度优先搜索进行 28 { 29 memset(judge,-1,sizeof(judge)); 30 queue<int> q; 31 q.push(1); 32 judge[1]=0; 33 while(!q.empty()) 34 { 35 int v=q.front(); 36 q.pop(); 37 for(int i=1; i<=n; i++) 38 { 39 if(G[v][i]) 40 { 41 if(judge[i]==-1) 42 { 43 judge[i]=(judge[v]+1)%2; 44 q.push(i); 45 } 46 else 47 { 48 if(judge[i]==judge[v]) 49 return false; 50 } 51 52 } 53 } 54 } 55 return true; 56 } 57 58 int main() 59 { 60 61 while(~scanf("%d%d",&n,&m)) 62 { 63 memset(G,0,sizeof(G)); 64 memset(used,0,sizeof(used)); 65 int a,b; 66 for(int i=0; i<m; i++) 67 { 68 scanf("%d%d",&a,&b); 69 G[a][b]=1; 70 G[b][a]=1; 71 } 72 if(!bfs()) 73 { 74 puts("No"); 75 continue; 76 } 77 int ans=0; 78 for(int i=1; i<=n; i++) 79 { 80 memset(vis,0,sizeof(vis)); 81 if(find(i)); 82 ans++; 83 } 84 printf("%d\n",ans/2); 85 } 86 return 0; 87 }
雪儿言