1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| ##include "bits/stdc++.h" using namespace std;
##define INF 0x3f3f3f3f
struct Dinic { static const int N = 1e5 + 10, M = 2e5 + 10; int n, m, s, t, maxflow; int deep[N], cur[N];
struct Edge { int v, next; int cap; }e[M << 1]; int head[M << 1], cnt;
void init() { mem(head, -1); cnt = maxflow = 0; }
inline void add(int u, int v, int cap) { e[cnt].v = v; e[cnt].cap = cap; e[cnt].next = head[u]; head[u] = cnt++;
e[cnt].v = u; e[cnt].cap = 0; e[cnt].next = head[v]; head[v] = cnt++; }
bool bfs() { for(int i = 0;i <= t; i++) { deep[i] = -1; cur[i] = head[i]; } queue<int> q; q.push(s); deep[s] = 0; while(!q.empty()) { int u = q.front(); q.pop(); for(int i = head[u]; ~i; i = e[i].next) { int v = e[i].v; if(deep[v] == -1 && e[i].cap) { deep[v] = deep[u] + 1; q.push(v); } } } if(deep[t] >= 0) return true; else return false; }
int dfs(int u, int mx) { int a; if(u == t) return mx; for(int i = cur[u]; ~i; i = e[i].next) { cur[u] = i; int v = e[i].v; if(e[i].cap && deep[v] == deep[u] + 1 && (a = dfs(v, min(mx, e[i].cap)))) { e[i].cap -= a; e[i ^ 1].cap += a; return a; } } return 0; }
void dinic() { int res; while(bfs()) { while(1) { res = dfs(s, INF); if(!res) break; maxflow += res; } } } }mf;
void solve() { int _; cin >> _; while(_--) { mf.init(); int n; cin >> n; vector<int> v1, v2; vector<int> x(n), y(n); for(int i = 0;i < n; i++) { int a, b; cin >> a >> b; x[i] = a + b; y[i] = a - b; v1.push_back(x[i]); v2.push_back(y[i]); } sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); v1.erase(unique(v1.begin(), v1.end()), v1.end()); v2.erase(unique(v2.begin(), v2.end()), v2.end()); int len = v1.size(); mf.s = 0; mf.t = v1.size() + v2.size() + 1; for(int i = 1;i <= len; i++) { mf.add(mf.s, i, 1); } for(int i = len + 1;i <= mf.t - 1; i++) { mf.add(i, mf.t, 1); } for(int i = 0;i < n; i++) { int u = lower_bound(v1.begin(), v1.end(), x[i]) - v1.begin() + 1; int v = lower_bound(v2.begin(), v2.end(), y[i]) - v2.begin() + 1; mf.add(u, v + len, 1); } mf.dinic(); cout << mf.maxflow << endl; } }
signed main() { solve(); }
|