您的位置:首页 > 其它

uva 10131 Is Bigger Smarter? dag 最长路 加路径还原

2015-04-27 23:17 483 查看
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#define INF 100000000

using namespace std;
struct node{
	int x,y;
};

node a[1005];
int n;
int ma[1005][1005];
int dp[1005];

void print(int max,int q){
	if(max == 1){
		printf("%d\n",q+1);
		return ;
	}
	printf("%d\n",q+1);
	for(int i = 0;i < n;i++){
		if(ma[q][i] && dp[i] == max-1){
			print(max-1,i);
			break;
		}
	}
	
}
int fun(node &x,node &y){
	if(x.x < y.x && x.y > y.y) return 1;
	return 0;
}
int dfs(int cur){
	int& ret = dp[cur];
	if(ret > 0) return ret;
	ret = 1;
	for(int i = 0;i < n;i++){
		if(ma[cur][i]){
			ret = max(dfs(i)+1,ret);
		}
	}
	return ret;
}
int main(){
	while(scanf("%d%d",&a
.x,&a
.y)!=EOF) n++;
	memset(ma,0,sizeof(ma));
	
	
	for(int i = 0;i < n;i++){
		for(int j = 0;j < n;j++){
			if(fun(a[i],a[j]) > 0){
				ma[i][j] = 1;
			}
		}
	}	
	
	for(int i = 0;i < n;i++){
		dfs(i);	
	}
	
	int maxn;
	int max = -1;
	for(int i = 0;i < n;i++){
		if(dp[i] > max){
			maxn = i;
			max = dp[i];
		}
	} 
	cout << max << endl;
	print(max,maxn);
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: