Problem Description
Do you remember the box of Matryoshka dolls last week? Adam just got another box of dolls from Matryona. This time, the dolls have different shapes and sizes: some are skinny, some are fat, and some look as though they were attened. Specifically, doll i can be represented by three numbers wi, li, and hi, denoting its width, length, and height. Doll i can fit inside another doll j if and only if wi < wj , li < lj , and hi < hj .
That is, the dolls cannot be rotated when fitting one inside another. Of course, each doll may contain at most one doll right inside it. Your goal is to fit dolls inside each other so that you minimize the number of outermost dolls.
That is, the dolls cannot be rotated when fitting one inside another. Of course, each doll may contain at most one doll right inside it. Your goal is to fit dolls inside each other so that you minimize the number of outermost dolls.
Input
The input consists of multiple test cases. Each test case begins with a line with a single integer N, 1 ≤ N ≤ 500, denoting the number of Matryoshka dolls. Then follow N lines, each with three space-separated integers wi, li, and hi (1 ≤ wi; li; hi ≤ 10,000) denoting the size of the ith doll. Input is followed by a single line with N = 0, which should not be processed.
Output
For each test case, print out a single line with an integer denoting the minimum number of outermost dolls that can be obtained by optimally nesting the given dolls.
Sample Input
3 5 4 8 27 10 10 100 32 523 3 1 2 1 2 1 1 1 1 2 4 1 1 1 2 3 2 3 2 2 4 4 4 0
Sample Output
1 3 2
思路:给每个娃娃找父亲,如果找到的父亲已经有娃娃,那就看能不能给他现在的娃娃找别的父亲
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
#define maxn 508
int boll[maxn][4];
vector <int> ans[maxn];
bool vis[maxn];
int marry[maxn];
bool dfs(int x)
{
for(int i=0;i<ans[x].size();i++)
{
if(!vis[ans[x][i]])
{
vis[ans[x][i]] = 1;
if(!marry[ans[x][i]] || dfs(marry[ans[x][i]]))
{
marry[ans[x][i]] = x;
return 1;
}
}
}
return 0;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF && n)
{
for(int i=1;i<=n;i++) ans[i].clear();
memset(marry,0,sizeof(marry));
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&boll[i][1],&boll[i][2],&boll[i][3]);
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i == j) continue;
bool flag = true;
for(int k=1;k<=3;k++)
{
if(boll[i][k] <= boll[j][k])
{
flag = false;
break;
}
}
if(flag) ans[i].push_back(j);
}
}
int sum = 0;
for(int i=1;i<=n;i++)
{
memset(vis,0,sizeof(vis));
if(dfs(i))
{
sum++;
}
}
printf("%d\n",n-sum);
}
return 0;
}