[codevs 1922] 骑士共存问题
[codevs 1922] 骑士共存问题
题解:
二分图最大独立集问题。
二分图的最大独立集:
选出一些点,让两两之间没有边相连。
二分图最大独立集问题一般转化为它的对偶问题——最小覆盖集,因为最大独立集要求每条边所连接的两个点最多有一个被选中,而最小覆盖集要求每条边所连接的两个点最少有一个被选中。那么点的个数-最小覆盖集点的个数=最大独立集(显然减掉的越少剩下的越多)。
那么最小覆盖怎么求呢?答案就是建立二分图求最大基数匹配。可以简单证明一下:
1)充分性:如果还有一条边没有被覆盖,那么此时一定可以把这条边作为一条新的匹配,说明此时还不是最大匹配。
2)必要性:如果可以删去一条边,二分图匹配边中是没有交点的,那么删去后这条边一定不会被覆盖。
算法就到此结束。
原题中记得要把总点数减去不能走的格子数。
代码:
dinic算法过了,ISAP死活不对...应该是我写的问题。
Dinic:
总时间耗费: 3172ms
总内存耗费: 11 kB
#include<cstdio> #include<iostream> #include<vector> #include<cstring> #include<string> #include<queue> #include<algorithm> using namespace std; const int maxn = 200 * 200 + 10; const int INF = 1e9 + 7; const int dx[] = {-1, -2, -2, -1, 1, 2, 2, 1}; const int dy[] = {-2, -1, 1, 2, 2, 1, -1, -2}; int n, m, s, t; struct Edge { int from, to, cap, flow; }; vector<Edge> edges; vector<int> G[maxn]; void AddEdge(int from, int to, int cap) { edges.push_back((Edge){from, to, cap, 0}); edges.push_back((Edge){to, from, 0, 0}); int sz = edges.size(); G[from].push_back(sz-2); G[to].push_back(sz-1); } int id[222][222]; void init() { cin >> n >> m; s = 0; t = n*n - m + 1; for(int i = 0; i < m; i++) { int x, y; cin >> x >> y; id[x][y] = -1; } int c = 0; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) if(!id[i][j]) id[i][j] = ++c; for(int x = 1; x <= n; x++) for(int y = 1; y <= n; y++) if(id[x][y] > 0) { int ID = id[x][y]; if((x+y)%2 == 0) { AddEdge(s, ID, 1); for(int i = 0; i < 8; i++) { int newx = x + dx[i], newy = y + dy[i]; int newID = id[newx][newy]; if(newx <= n && newy <= n && newx >= 1 && newy >= 1 && newID > 0) AddEdge(ID, newID, INF); } } else { AddEdge(ID, t, 1); } } } bool vis[maxn]; int d[maxn]; bool BFS() { queue<int> Q; Q.push(s); memset(vis, 0, sizeof(vis)); d[s] = 0; vis[s] = 1; while(!Q.empty()) { int u = Q.front(); Q.pop(); for(int i = 0; i < G[u].size(); i++) { Edge& e = edges[G[u][i]]; if(!vis[e.to] && e.cap > e.flow) { vis[e.to] = 1; d[e.to] = d[u] + 1; Q.push(e.to); } } } return vis[t]; } int cur[maxn]; int DFS(int u, int a) { if(a == 0 || u == t) return a; int f, flow = 0; for(int& i = cur[u]; i < G[u].size(); i++) { Edge& e = edges[G[u][i]]; if(d[u] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap-e.flow))) > 0) { flow += f; e.flow += f; a -= f; edges[G[u][i]^1].flow -= f; if(a == 0) break; } } return flow; } void Dinic() { int flow = 0; while(BFS()) { memset(cur, 0, sizeof(cur)); flow += DFS(s, INF); } cout << n*n-m-flow << endl; } int main() { init(); Dinic(); return 0; }
ISAP:
TLE(哪错了呢?)
#include<cstdio> #include<iostream> #include<vector> #include<queue> #include<algorithm> using namespace std; const int maxn = 200 * 200 + 10; const int INF = 1e9 + 7; const int dx[] = {-1, -2, -2, -1, 1, 2, 2, 1}; const int dy[] = {-2, -1, 1, 2, 2, 1, -1, -2}; int n, m, s, t; struct Edge { int from, to, cap, flow; }; vector<Edge> edges; vector<int> G[maxn]; void AddEdge(int from, int to, int cap) { edges.push_back((Edge){from, to, cap, 0}); edges.push_back((Edge){to, from, 0, 0}); int sz = edges.size(); G[from].push_back(sz-2); G[to].push_back(sz-1); } int id[222][222]; void init() { cin >> n >> m; s = 0; t = n*n - m + 1; for(int i = 0; i < m; i++) { int x, y; cin >> x >> y; id[x][y] = -1; } int c = 0; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) if(!id[i][j]) id[i][j] = ++c; for(int x = 1; x <= n; x++) for(int y = 1; y <= n; y++) if(id[x][y] > 0) { int ID = id[x][y]; if((x+y)%2 == 0) { AddEdge(s, ID, 1); for(int i = 0; i < 8; i++) { int newx = x + dx[i], newy = y + dy[i]; int newID = id[newx][newy]; if(newx <= n && newy <= n && newx >= 1 && newy >= 1 && newID > 0) AddEdge(ID, newID, INF); } } else { AddEdge(ID, t, 1); } } } bool vis[maxn]; int d[maxn], p[maxn], cur[maxn], num[maxn]; int Augment() { int x = t, a = INF; while(x != 1) { Edge& e = edges[p[x]]; a = min(a, e.cap-e.flow); x = e.from; } x = t; while(x != 1) { edges[p[x]].flow += a; edges[p[x]^1].flow -= a; x = edges[p[x]].from; } return a; } void BFS() { queue<int> Q; bool vis[maxn]; memset(vis, 0, sizeof(vis)); d[t] = 0; Q.push(t); while(!Q.empty()) { int x = Q.front(); Q.pop(); for(int i = 0; i < G[x].size(); i++) { Edge& e = edges[G[x][i]]; if(e.cap == 0 && !vis[e.to]) { d[e.to] = d[x] + 1; vis[e.to] = 1; Q.push(e.to); } } } } void Maxflow() { int flow = 0; BFS(); memset(num, 0, sizeof(num)); for(int i = 0; i <= t; i++) num[d[i]]++; int x = s; memset(cur, 0, sizeof(cur)); while(d[s] < t) { if(x == t) { flow += Augment(); x = s; } int ok = 0; for(int i = cur[x]; i < G[x].size(); i++) { Edge& e = edges[G[x][i]]; if(e.cap > e.flow && d[x] == d[e.to] + 1) { ok = 1; p[e.to] = G[x][i]; cur[x] = i; x = e.to; break; } } if(!ok) { int _m = t - 1; for(int i = 0; i < G[x].size(); i++) { Edge& e = edges[G[x][i]]; if(e.cap > e.flow) _m = min(_m, d[e.to]); } if(--num[d[x]] == 0) break; num[d[x] = _m+1]++; cur[x] = 0; if(x != s) x = edges[p[x]].from; } } cout << n*n-m-flow << endl; } int main() { init(); Maxflow(); return 0; }我的ISAP连草地排水都WA一个点。。。
dinic算法过了,ISAP死活不对...应该是我写的问题。
TLE