POJ 1466 Girls and Boys
Girls and Boys
Time Limit: 1 Sec Memory Limit: 256 MB
题目连接
http://poj.org/problem?id=1466
Description
In the second year of the university somebody started a study on the romantic relations between the students. The relation "romantically involved" is defined between one girl and one boy. For the study reasons it is necessary to find out the maximum set satisfying the condition: there are no two students in the set who have been "romantically involved". The result of the program is the number of students in such a set.
Input
The input contains several data sets in text format. Each data set represents one set of subjects of the study, with the following description:
the number of students
the description of each student, in the following format
student_identifier:(number_of_romantic_relations) student_identifier1 student_identifier2 student_identifier3 ...
or
student_identifier:(0)
The student_identifier is an integer number between 0 and n-1 (n <=500 ), for n subjects.
the number of students
the description of each student, in the following format
student_identifier:(number_of_romantic_relations) student_identifier1 student_identifier2 student_identifier3 ...
or
student_identifier:(0)
The student_identifier is an integer number between 0 and n-1 (n <=500 ), for n subjects.
Output
For each given data set, the program should write to standard output a line containing the result.
Sample Input
7
0: (3) 4 5 6
1: (2) 4 6
2: (0)
3: (0)
4: (2) 0 1
5: (1) 0
6: (2) 0 1
3
0: (2) 1 2
1: (1) 0
2: (1) 0
0: (3) 4 5 6
1: (2) 4 6
2: (0)
3: (0)
4: (2) 0 1
5: (1) 0
6: (2) 0 1
3
0: (2) 1 2
1: (1) 0
2: (1) 0
Sample Output
5
2
2
HINT
要求找到一个点集合,让这个集合中并没有人互相喜欢
找相互喜欢的人的最大匹配,答案为总人数-匹配数/2
做的时候又忘了数据初始化,悲伤
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cmath> 5 #include<cstring> 6 #include<queue> 7 #include<vector> 8 using namespace std; 9 const int mxn=800; 10 int n; 11 vector<int>mp[mxn]; 12 int vis[mxn]; 13 int mc[mxn]; 14 int x; 15 inline int read(){ 16 int x=0; 17 char ch=getchar(); 18 while(ch<'0'||ch>'9')ch=getchar(); 19 while(ch>='0' && ch<='9'){ x=x*10+ch-'0';ch=getchar(); } 20 return x; 21 } 22 int dfs(int u){ 23 for(int i=0;i<mp[u].size();i++){ 24 int v=mp[u][i]; 25 if(!vis[v]){ 26 vis[v]=1; 27 if(mc[v]==-1 || dfs(mc[v])){ 28 mc[v]=u; 29 return 1; 30 } 31 } 32 } 33 return 0; 34 } 35 int Maxmatch(){ 36 int res=0; 37 memset(mc,-1,sizeof(mc)); 38 int i; 39 for(i=0;i<n;i++){ 40 { 41 memset(vis,0,sizeof(vis)); 42 res+=dfs(i); 43 } 44 } 45 return res; 46 } 47 int main(){ 48 while(scanf("%d",&n)!=EOF){ 49 //read 50 for(int i=0;i<=n;i++) mp[i].clear(); 51 for(int L=1;L<=n;L++){ 52 int u,v; 53 u=read(); 54 int i,j; 55 x=read(); 56 for(i=0;i<x;i++){ 57 v=read(); 58 mp[u].push_back(v); 59 } 60 } 61 //read finished 62 int ans=Maxmatch(); 63 printf("%d\n",n-ans/2); 64 } 65 return 0; 66 }
本文为博主原创文章,转载请注明出处。