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
| #include <bits/stdc++.h> #define MAX_N 5000 #define MAX_M 500000 #define INF 0x7f7f7f7f using namespace std; struct P {int x, y;} p[MAX_N+5]; int n, s, ss, t, cnt, pr[MAX_N+5], cr[MAX_N+5], mxf, mxc; struct node {int v, c, w, nxt;} E[MAX_M+5]; void init() {s = 0, ss = n*2+1, t = n*2+2, memset(pr, -1, sizeof pr);} bool cmp (const P &a, const P &b) {return a.x == b.x ? a.y < b.y : a.x < b.x;} void insert(int u, int v, int c, int w) {E[cnt] = (node){v, c, w, pr[u]}, pr[u] = cnt++;} void addedge(int u, int v, int c, int w) {insert(u, v, c, w), insert(v, u, 0, -w);} bool SPFA() { queue <int> que; bool inq[MAX_N+5]; int d[MAX_N+5], cr[MAX_N+5]; memset(inq, false, sizeof inq), memset(cr, -1, sizeof cr); memset(d, -1, sizeof d); d[s] = 0, que.push(s), inq[s] = true; while (!que.empty()) { int u = que.front(); que.pop(), inq[u] = false; for (int i = pr[u]; ~i; i = E[i].nxt) { int v = E[i].v, c = E[i].c, w = E[i].w; if (c && d[u]+w > d[v]) { d[v] = d[u]+w, cr[v] = i; if (!inq[v]) que.push(v), inq[v] = true; } } } if (d[t] <= 0) return false; int flow = INF; for (int i = cr[t]; ~i; i = cr[E[i^1].v]) flow = min(flow, E[i].c); for (int i = cr[t]; ~i; i = cr[E[i^1].v]) E[i].c -= flow, E[i^1].c += flow; mxf += flow, mxc += d[t]; return true; } int main() { scanf("%d", &n); init(), addedge(s, ss, 2, 0); for (int i = 1; i <= n; i++) addedge(ss, i, 2, 0), addedge(i+n, t, 2, 0); for (int i = 1; i <= n; i++) addedge(i, i+n, 1, 1), addedge(i, i+n, 1, 0); for (int i = 1; i <= n; i++) scanf("%d%d", &p[i].x, &p[i].y); sort(p+1, p+n+1, cmp); for (int i = 1, mi = INF; i <= n; i++, mi = INF) for (int j = i+1; j <= n; j++) if (p[i].y <= p[j].y && p[j].y <= mi) addedge(i+n, j, 2, 0), mi = p[j].y; while (SPFA()) ; return printf("%d", mxc), 0; }
|