标签:else can S3 items sep ble lin ems int
题目链接:
http://poj.org/problem?id=2771
Description
Input
Output
Sample Input
2 4 35 M classicism programming 0 M baroque skiing 43 M baroque chess 30 F baroque soccer 8 27 M romance programming 194 F baroque programming 67 M baroque ping-pong 51 M classicism programming 80 M classicism Paintball 35 M baroque ping-pong 39 F romance ping-pong 110 M romance Paintball
Sample Output
3 7
1 /* 2 问题 3 输入n个人的信息,如果他们满足几个条件就视为存在成为一对的关系,求不能成为一对的最大子独立集的顶点个数 4 5 解题思路 6 图论的题很绕,让我们理清关系,求最大子图,就是挑几个人,他们相互之间都存在一种不能成为一对的关系,最多有多少人 7 做图的时候,将存在能成为一对的关系的置为1,否则都置为0,跑一边匈牙利算法得到最大匹配数 8 那么顶点数n减去最大匹配数就是最大子独立集的个数了。 9 */ 10 #include<cstdio> 11 #include<iostream> 12 #include<string> 13 #include<cstring> 14 #include<cmath> 15 16 using namespace std; 17 const int maxn=1100; 18 int n,e[maxn][maxn],fn,mn; 19 int cx[maxn],cy[maxn]; 20 int book[maxn]; 21 struct PER{ 22 int age; 23 string sex,music,sport; 24 }fper[maxn],mper[maxn]; 25 26 27 int ok(struct PER a, struct PER b); 28 int maxmatch(); 29 int dfs(int u); 30 31 int main() 32 { 33 int T,i,j; 34 int x; 35 char s1[100],s2[100],s3[100]; 36 scanf("%d",&T); 37 while(T--){ 38 scanf("%d",&n); 39 fn=mn=0; 40 for(i=0;i<n;i++){ 41 scanf("%d%s%s%s",&x,s1,s2,s3); 42 if(strcmp(s1,"F") == 0){ 43 fper[fn].age=x; 44 fper[fn].sex=s1; 45 fper[fn].music=s2; 46 fper[fn].sport=s3; 47 fn++; 48 } 49 else{ 50 mper[mn].age=x; 51 mper[mn].sex=s1; 52 mper[mn].music=s2; 53 mper[mn].sport=s3; 54 mn++; 55 } 56 } 57 58 memset(e,0,sizeof(int)*maxn*maxn); 59 for(i=0;i<mn;i++){ 60 for(j=0;j<fn;j++){ 61 if(ok(mper[i],fper[j])) 62 e[i][j]=1; 63 } 64 } 65 printf("%d\n",n-maxmatch()); 66 } 67 return 0; 68 } 69 70 int ok(struct PER a, struct PER b){ 71 if(abs(a.age - b.age) > 40) 72 return 0; 73 if(a.sex == b.sex) 74 return 0; 75 if(a.music != b.music) 76 return 0; 77 if(a.sport == b.sport) 78 return 0; 79 80 return 1; 81 } 82 83 int dfs(int u) 84 { 85 for(int i=0;i<fn;i++){ 86 if(book[i] == 0 && e[u][i]){ 87 book[i]=1; 88 89 if(cy[i] == -1 || dfs(cy[i])){ 90 cx[u]=i; 91 cy[i]=u; 92 return 1; 93 } 94 } 95 } 96 return 0; 97 } 98 99 int maxmatch() 100 { 101 int i; 102 memset(cx,-1,sizeof(cx)); 103 memset(cy,-1,sizeof(cy)); 104 int ans=0; 105 for(i=0;i<mn;i++){ 106 if(cx[i] == -1){ 107 memset(book,0,sizeof(book)); 108 if(dfs(i)) ans++; 109 } 110 } 111 return ans; 112 }
POJ 2771 Guardian of Decency(最大独立集数=顶点数-最大匹配数)
标签:else can S3 items sep ble lin ems int
原文地址:https://www.cnblogs.com/wenzhixin/p/9074056.html