套娃和矩形嵌套一样是典型的DAG
把嵌套关系用有向边表示可以得到一个DAG
可以把DAG上的点i拆成两个点 i 和 i' 分别放在两个集合中 如果原图i-j有边 二分图里就连一条i-j'的边 跑匈牙利求解
最小路径覆盖数==DAG的点数n(拆点前)-最大匹配数(拆点后的新图) 即答案
//拆点保证不重复
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N = 507;
struct edge {
int v, next;
}e[1000001];
int p[N<<1], eid = 0;
struct tw {
int x, y, z;
}pool[N<<1]; //要拆点
int n;
inline int read() {
char c = getchar();
int s = 0, f = 1;
while (c<'0' || c>'9') {
if (c == '-') f = -1; c = getchar();
}
while (c >= '0'&&c <= '9') {
s *= 10;
s += c - 48;
c = getchar();
}
return s*f;
}
inline void ins(int u,int v){
e[eid].v = v; e[eid].next = p[u]; p[u] = eid++;
}
inline void init() { memset(p, -1, sizeof(p)); eid = 0; }
inline bool check(tw i, tw j) {
return (i.x > j.x&&i.y > j.y&&i.z > j.z);
}
int link[N << 1];
bool vis[N << 1];
bool dfs(int u) {
int v;
for (int i = p[u]; ~i; i = e[i].next) {
v = e[i].v;
if (!vis[v]) {
vis[v] = true;
if (link[v] == -1 || dfs(link[v])){
link[v] = u;
return true;
}
}
}
return false;
}
int hungary() {
int res = 0;
memset(link, -1, sizeof(link));
for (int i = 1; i <= n; i++)
{
memset(vis, 0, sizeof(vis));
res+=dfs(i);
}
return res;
}
int main() {
n = read();
init();
for (int i = 1; i <= n; i++)
{
pool[i].x = read(); pool[i].y = read(); pool[i].z = read();
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
if (check(pool[i], pool[j])) ins(i, j+n);//i>j
else if (check(pool[j], pool[i])) ins(j, i+n);
}
printf("%d", n-hungary()); //最小路径覆盖为DAG点数-匹配数
//getchar();
return 0;
}