题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=16
DAG上的动态规划
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int e[1005][1005];
int N;
int n;
int a[1005],b[1005];
int d[1005];
int dp(int i)
{
if(d[i] > 0)
return d[i];
d[i] = 1;
for(int j = 0; j < n; ++j)
if(e[i][j])
{
d[i] = max(d[i],dp(j) + 1);
}
return d[i];
}
int main()
{
scanf("%d",&N);
while(N--)
{
memset(d,0,sizeof(d));
memset(e,0,sizeof(e));
scanf("%d",&n);
for(int i = 0; i < n; ++i)
scanf("%d%d",&a[i],&b[i]);
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < n; ++j)
if(i != j)
{
if((a[i] < a[j] && b[i] < b[j])||(a[i] < b[j] && b[i] < a[j]))
{
e[i][j] = 1;
}
}
}
int ans = 0;
for(int i = 0;i < n;++i)
if(dp(i) > ans)
ans = dp(i);
printf("%d\n",ans);
}
return 0;
}