[HDOJ1068]Girls and Boys(二分图最大匹配,最大独立集)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1068
题意:最大独立集,n-最大匹配
1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 */ 17 #include <algorithm> 18 #include <iostream> 19 #include <iomanip> 20 #include <cstring> 21 #include <climits> 22 #include <complex> 23 #include <fstream> 24 #include <cassert> 25 #include <cstdio> 26 #include <bitset> 27 #include <vector> 28 #include <deque> 29 #include <queue> 30 #include <stack> 31 #include <ctime> 32 #include <set> 33 #include <map> 34 #include <cmath> 35 using namespace std; 36 #define fr first 37 #define sc second 38 #define cl clear 39 #define BUG puts("here!!!") 40 #define W(a) while(a--) 41 #define pb(a) push_back(a) 42 #define Rint(a) scanf("%d", &a) 43 #define Rs(a) scanf("%s", a) 44 #define Cin(a) cin >> a 45 #define FRead() freopen("in", "r", stdin) 46 #define FWrite() freopen("out", "w", stdout) 47 #define Rep(i, len) for(int i = 0; i < (len); i++) 48 #define For(i, a, len) for(int i = (a); i < (len); i++) 49 #define Cls(a) memset((a), 0, sizeof(a)) 50 #define Clr(a, x) memset((a), (x), sizeof(a)) 51 #define Full(a) memset((a), 0x7f7f7f, sizeof(a)) 52 #define lrt rt << 1 53 #define rrt rt << 1 | 1 54 #define pi 3.14159265359 55 #define RT return 56 #define lowbit(x) x & (-x) 57 #define onecnt(x) __builtin_popcount(x) 58 typedef long long LL; 59 typedef long double LD; 60 typedef unsigned long long ULL; 61 typedef pair<int, int> pii; 62 typedef pair<string, int> psi; 63 typedef pair<LL, LL> pll; 64 typedef map<string, int> msi; 65 typedef vector<int> vi; 66 typedef vector<LL> vl; 67 typedef vector<vl> vvl; 68 typedef vector<bool> vb; 69 70 const int maxn = 1530; 71 const int inf = 0x3f3f3f3f; 72 int n; 73 int nx, ny, dist; 74 int Mx[maxn], My[maxn], dx[maxn], dy[maxn], vis[maxn], G[maxn][maxn]; 75 76 bool dfs(int u){ 77 for(int v = 0; v < ny; v++) 78 if(!vis[v] && G[u][v] && dy[v] == dx[u] + 1){ 79 vis[v] = 1; 80 if(My[v] != -1 && dy[v] == dist) 81 continue; 82 if(My[v] == -1|| dfs(My[v])){ 83 My[v] = u; 84 Mx[u] = v; 85 return 1; 86 } 87 } 88 return 0; 89 } 90 bool bfs(){ 91 queue<int> Q; 92 dist = inf; 93 memset(dx, -1, sizeof(dx)); 94 memset(dy, -1, sizeof(dy)); 95 for(int i = 0; i < nx; i++) 96 if(Mx[i] == -1){ 97 Q.push(i); 98 dx[i] = 0; 99 } 100 while(!Q.empty()){ 101 int u = Q.front(); 102 Q.pop(); 103 if(dx[u] > dist) 104 break; 105 for(int v = 0; v < ny; v++) 106 if(G[u][v] && dy[v] == -1){ 107 dy[v] = dx[u] + 1; 108 if(My[v] == -1) 109 dist = dy[v]; 110 else{ 111 dx[My[v]] = dy[v] + 1; 112 Q.push(My[v]); 113 } 114 } 115 } 116 return dist != inf; 117 } 118 int hk() { 119 int res = 0; 120 memset(Mx, -1, sizeof(Mx)); 121 memset(My, -1, sizeof(My)); 122 while(bfs()){ 123 memset(vis, 0, sizeof(vis)); 124 for(int i = 0; i < nx; i++) 125 if(Mx[i] == -1&& dfs(i)) 126 res++; 127 } 128 return res; 129 } 130 131 132 int main() { 133 // FRead(); 134 int u, v, k; 135 while(~Rint(n)) { 136 nx = ny = n; Cls(G); 137 Rep(i, n) { 138 scanf("%d: (%d)", &u, &k); 139 Rep(i, k) { 140 Rint(v); 141 G[u][v] = G[v][u] = 1; 142 } 143 } 144 printf("%d\n", n-hk()/2); 145 } 146 RT 0; 147 }