[HDOJ2444]The Accomodation of Students(二分图染色判定,最大匹配,匈牙利算法)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444
题意:求这个图是否是二分图,如果是则求最大匹配。
数据就是图,kuangbin的匈牙利算法板子。
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 Rll(a) scanf("%I64d", &a) 44 #define Rs(a) scanf("%s", a) 45 #define Cin(a) cin >> a 46 #define FRead() freopen("in", "r", stdin) 47 #define FWrite() freopen("out", "w", stdout) 48 #define Rep(i, len) for(int i = 0; i < (len); i++) 49 #define For(i, a, len) for(int i = (a); i < (len); i++) 50 #define Cls(a) memset((a), 0, sizeof(a)) 51 #define Clr(a, x) memset((a), (x), sizeof(a)) 52 #define Full(a) memset((a), 0x7f7f7f, sizeof(a)) 53 #define lrt rt << 1 54 #define rrt rt << 1 | 1 55 #define pi 3.14159265359 56 #define RT return 57 #define lowbit(x) x & (-x) 58 #define onecnt(x) __builtin_popcount(x) 59 typedef long long LL; 60 typedef long double LD; 61 typedef unsigned long long ULL; 62 typedef pair<int, int> pii; 63 typedef pair<string, int> psi; 64 typedef pair<LL, LL> pll; 65 typedef map<string, int> msi; 66 typedef vector<int> vi; 67 typedef vector<LL> vl; 68 typedef vector<vl> vvl; 69 typedef vector<bool> vb; 70 71 typedef struct Edge { 72 int u, v; 73 int next; 74 }Edge; 75 76 const int maxn = 220; 77 const int maxm = maxn*maxn; 78 int n, m, ecnt; 79 int color[maxn]; 80 int head[maxn]; 81 Edge edge[maxm]; 82 int q[maxm], front, tail; 83 84 void init() { 85 ecnt = 0; 86 Clr(head, -1); Clr(color, -1); 87 } 88 void adde(int u, int v) { 89 edge[ecnt].u = u; 90 edge[ecnt].v = v; 91 edge[ecnt].next = head[u]; 92 head[u] = ecnt++; 93 } 94 95 bool bfs(int u) { 96 front = tail = 0; 97 color[u] = 0; 98 q[tail++] = u; 99 int p = -1; 100 while(front < tail) { 101 int t = q[front++]; 102 for(int i = head[t]; ~i; i=edge[i].next) { 103 int v = edge[i].v; 104 if(v == t) continue; 105 if(color[v] == -1) { 106 color[v] = 1 ^ color[t]; 107 q[tail++] = v; 108 } 109 else if(color[v] == color[t]) return 0; 110 } 111 } 112 return 1; 113 } 114 115 int nu,nv;//u,v的数目,使用前面必须赋值 116 int G[maxn][maxn];//邻接矩阵 117 int linker[maxn]; 118 bool used[maxn]; 119 120 bool dfs(int u) { 121 for(int v = 0; v < nv;v++) 122 if(G[u][v] && !used[v]) { 123 used[v] = true; 124 if(linker[v] == -1 || dfs(linker[v])) { 125 linker[v] = u; 126 return true; 127 } 128 } 129 return false; 130 } 131 132 int hungary() { 133 int ret = 0; 134 memset(linker, -1, sizeof(linker)); 135 for(int u = 0; u < nu; u++) { 136 memset(used, 0, sizeof(used)); 137 if(dfs(u)) ret++; 138 } 139 return ret; 140 } 141 142 int main() { 143 // FRead(); 144 int u, v; 145 while(~Rint(n) && ~Rint(m)) { 146 init(); Cls(G); 147 Rep(i, m) { 148 Rint(u); Rint(v); 149 adde(u, v); adde(v, u); 150 G[u-1][v-1] = G[v-1][u-1] = 1; 151 } 152 bool exflag = 0; 153 For(i, 1, n+1) { 154 if(color[i] == -1) { 155 if(bfs(i) == 0) { 156 exflag = 1; 157 break; 158 } 159 } 160 } 161 if(exflag) { 162 printf("No\n"); 163 continue; 164 } 165 nu = nv = n; 166 printf("%d\n", hungary()/2); 167 } 168 RT 0; 169 }