[SWUST1759] 骑士共存问题(最大流,最大独立集)
题目链接:https://www.oj.swust.edu.cn/problem/show/1759
和方格取数一样建模就行了。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef struct Edge { 5 int u, v, w, next; 6 }Edge; 7 8 const int inf = 0x7f7f7f7f; 9 const int maxn = 44444; 10 11 int cnt, dhead[maxn]; 12 int cur[maxn], dd[maxn]; 13 Edge dedge[800001]; 14 int S, T, N; 15 16 void init() { 17 memset(dhead, -1, sizeof(dhead)); 18 for(int i = 0; i < maxn; i++) dedge[i].next = -1; 19 S = 0; cnt = 0; 20 } 21 22 void adde(int u, int v, int w, int c1=0) { 23 dedge[cnt].u = u; dedge[cnt].v = v; dedge[cnt].w = w; 24 dedge[cnt].next = dhead[u]; dhead[u] = cnt++; 25 dedge[cnt].u = v; dedge[cnt].v = u; dedge[cnt].w = c1; 26 dedge[cnt].next = dhead[v]; dhead[v] = cnt++; 27 } 28 29 bool bfs(int s, int t, int n) { 30 queue<int> q; 31 for(int i = 0; i < n; i++) dd[i] = inf; 32 dd[s] = 0; 33 q.push(s); 34 while(!q.empty()) { 35 int u = q.front(); q.pop(); 36 for(int i = dhead[u]; ~i; i = dedge[i].next) { 37 if(dd[dedge[i].v] > dd[u] + 1 && dedge[i].w > 0) { 38 dd[dedge[i].v] = dd[u] + 1; 39 if(dedge[i].v == t) return 1; 40 q.push(dedge[i].v); 41 } 42 } 43 } 44 return 0; 45 } 46 47 int dinic(int s, int t, int n) { 48 int st[maxn], top; 49 int u; 50 int flow = 0; 51 while(bfs(s, t, n)) { 52 for(int i = 0; i < n; i++) cur[i] = dhead[i]; 53 u = s; top = 0; 54 while(cur[s] != -1) { 55 if(u == t) { 56 int tp = inf; 57 for(int i = top - 1; i >= 0; i--) { 58 tp = min(tp, dedge[st[i]].w); 59 } 60 flow += tp; 61 for(int i = top - 1; i >= 0; i--) { 62 dedge[st[i]].w -= tp; 63 dedge[st[i] ^ 1].w += tp; 64 if(dedge[st[i]].w == 0) top = i; 65 } 66 u = dedge[st[top]].u; 67 } 68 else if(cur[u] != -1 && dedge[cur[u]].w > 0 && dd[u] + 1 == dd[dedge[cur[u]].v]) { 69 st[top++] = cur[u]; 70 u = dedge[cur[u]].v; 71 } 72 else { 73 while(u != s && cur[u] == -1) { 74 u = dedge[st[--top]].u; 75 } 76 cur[u] = dedge[cur[u]].next; 77 } 78 } 79 } 80 return flow; 81 } 82 83 typedef pair<int, int> pii; 84 const int dx[11] = {-1,1,2,2,1,-1,-2,-2}; 85 const int dy[11] = {-2,-2,-1,1,2,2,1,-1}; 86 const int maxm = 1010; 87 int n, m; 88 int G[maxm][maxm], icnt; 89 map<pii, int> shawanyiera; 90 91 inline int id(int x, int y) { 92 return shawanyiera[pii(x, y)]; 93 } 94 95 inline bool ok(int x, int y) { 96 return x >= 1 && x <= n && y >= 1 && y <= n; 97 } 98 99 int main() { 100 // freopen("in", "r", stdin); 101 int u, v; 102 scanf("%d%d",&n,&m); 103 memset(G, 0, sizeof(G)); 104 init(); shawanyiera.clear(); icnt = 0; 105 for(int i = 0; i < m; i++) { 106 scanf("%d%d",&u,&v); 107 G[u][v] = 1; 108 } 109 for(int i = 1; i <= n; i++) { 110 for(int j = 1; j <= n; j++) { 111 if(G[i][j]) continue; 112 shawanyiera[pii(i, j)] = ++icnt; 113 } 114 } 115 S = 0, T = 2 * icnt + 1, N = T + 1; 116 for(int i = 1; i <= icnt; i++) { 117 adde(S, i, 1); 118 adde(i+icnt, T, 1); 119 } 120 for(int i = 1; i <= n; i++) { 121 for(int j = 1; j <= n; j++) { 122 if(G[i][j]) continue; 123 for(int k = 0; k < 8; k++) { 124 int x = i + dx[k]; 125 int y = j + dy[k]; 126 if(!ok(x, y)) continue; 127 if(G[x][y]) continue; 128 adde(id(i,j), icnt+id(x,y), inf); 129 } 130 } 131 } 132 // cout << icnt << endl; 133 printf("%d\n", icnt-dinic(S, T, N)/2); 134 135 return 0; 136 }