1. 程式人生 > >UVALive3415 Guardian of Decency —— 最大獨立集

UVALive3415 Guardian of Decency —— 最大獨立集

題目 class hide img cnblogs true problem -s uvalive

題目鏈接:https://vjudge.net/problem/UVALive-3415

技術分享

技術分享

題解:

代碼如下:

技術分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <string>
 6 #include <vector>
 7 #include <map>
 8 #include <set>
 9 #include <queue>
10
#include <sstream> 11 #include <algorithm> 12 using namespace std; 13 const int INF = 2e9; 14 const int MOD = 1e9+7; 15 const int MAXN = 1e3+10; 16 17 int n; 18 int M[MAXN][MAXN], link[MAXN]; 19 bool vis[MAXN]; 20 21 struct Node 22 { 23 int h; 24 string sex, music, sport; 25 }student[MAXN];
26 27 bool dfs(int u) 28 { 29 for(int i = 1; i<=n; i++) 30 if(M[u][i] && !vis[i]) 31 { 32 vis[i] = true; 33 if(link[i]==-1 || dfs(link[i])) 34 { 35 link[i] = u; 36 return true; 37 } 38 } 39 return false; 40 } 41 42
int hungary() 43 { 44 int ret = 0; 45 memset(link, -1, sizeof(link)); 46 for(int i = 1; i<=n; i++) 47 { 48 memset(vis, 0, sizeof(vis)); 49 if(dfs(i)) ret++; 50 } 51 return ret; 52 } 53 54 bool judge(Node x, Node y) 55 { 56 return (abs(x.h-y.h)<=40 && x.sex!=y.sex 57 && x.music==y.music && x.sport!=y.sport); 58 } 59 60 int main() 61 { 62 int T; 63 scanf("%d", &T); 64 while(T--) 65 { 66 scanf("%d", &n); 67 for(int i = 1; i<=n; i++) 68 cin>>student[i].h>>student[i].sex>>student[i].music>>student[i].sport; 69 70 memset(M, 0, sizeof(M)); 71 for(int i = 1; i<=n; i++) 72 for(int j = 1; j<=n; j++) 73 if(i!=j && judge(student[i], student[j])) 74 M[i][j] = 1; 75 76 int cnt = hungary()/2; 77 printf("%d\n", n-cnt); 78 } 79 }
View Code

UVALive3415 Guardian of Decency —— 最大獨立集