Returns the largest independent set.
The number of independence in a bipartite graph is equal to the number of vertices minus the maximum number of matches.
Since men and women are unknown in this question, we need to divide everyone into two sets, so we need to subtract half of the maximum number of matches.
View code
1/* 2 Author: zhaofa Fang 3 LANG: c ++ 4 */5 # include <cstdio> 6 # include <cstdlib> 7 # include <iostream> 8 # include <cmath> 9 # include <cstring> 10 # include <algorithm> 11 # include <string> 12 # include <vector> 13 # include <queue> 14 # include <stack> 15 # include <map> 16 # include <set> 17 # define Pb push_back18 using namespace STD; 19 20 // ============================================ ======= 21 // Maximum Independent point set = number of vertices-half of the maximum number of matches 22 23 const int maxn = 500; 24 bool vist [maxn]; 25 int match [maxn], n; 26 struct edge27 {28 int V, next; 29} edge [maxn * maxn]; 30 int num, P [maxn]; 31 32 void addedge (int u, int v) 33 {34 edge [num]. V = V; 35 edge [num]. next = P [u]; 36 p [u] = num ++; 37} 38 bool DFS (int u) 39 {40 for (INT I = P [u]; i; I = edge [I]. next) 41 {42 int v = edge [I]. v; 43 If (! Vist [v]) 44 {45 vist [v] = true; 46 If (Match [v] =-1 | DFS (Match [v]) 47 {48 match [v] = u; 49 return true; 50} 51} 52} 53 return false; 54} 55 56 int match () 57 {58 int CNT = 0; 59 for (INT I = 0; I <n; I ++) Match [I] =-1; 60 for (INT u = 0; U <n; U ++) 61 {62 for (INT I = 0; I <n; I ++) vist [I] = 0; 63 If (DFS (u) CNT ++; 64} 65 return CNT; 66} 67 68 int main () 69 {70 while (~ Scanf ("% d", & N) 71 {72 for (INT I = 0; I <n; I ++) P [I] = 0; 73 num = 1; 74 for (INT I = 0; I <n; I ++) 75 {76 int U, M; 77 scanf ("% d: (% d) ", & U, & M); 78 int V; 79 for (Int J = 0; j <m; j ++) 80 {81 scanf (" % d ", & V); 82 addedge (u, v); 83} 84} 85 printf ("% d \ n", N-(MATCH ()> 1 )); 86} 87 return 0; 88}