HDU-3998 网络流 拆点

Problem Description
There is a sequence X (i.e. x[1], x[2], …, x[n]). We define increasing subsequence of X
as x[i1], x[i2],…,x[ik], which satisfies follow conditions:

  1. x[i1] < x[i2],…,<x[ik];
  2. 1<=i1 < i2,…,<ik<=n

As an excellent program designer, you must know how to find the maximum length of the
increasing sequense, which is defined as s. Now, the next question is how many increasing
subsequence with s-length can you find out from the sequence X.

For example, in one case, if s = 3, and you can find out 2 such subsequence A and B from X.

  1. A = a1, a2, a3. B = b1, b2, b3.
  2. Each ai or bj(i,j = 1,2,3) can only be chose once at most.

Now, the question is:

  1. Find the maximum length of increasing subsequence of X(i.e. s).
  2. Find the number of increasing subsequence with s-length under conditions described (i.e. num).

Input
The input file have many cases. Each case will give a integer number n.The next line will
have n numbers.

Output
The output have two line. The first line is s and second line is num.

Sample Input
4
3 6 2 5

Sample Output
2
2

因为每个点只能用一次,
每个点拆为入点和出点,之间容量为1

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e4+5;
namespace NetFlow{
	const int MAXN=2e5+5,MAXM=2e5+5,INF=0x3f3f3f3f;//点数/边数
	struct Edge{
		int to,next,cap,flow;
	}edge[MAXM];
	int tol,head[MAXN],gap[MAXN],dep[MAXN],cur[MAXN],Q[MAXN],S[MAXN];
	void init(){tol=0;memset(head,-1,sizeof(head));}
	void link(int u,int v,int w){
		edge[tol]={v,head[u],w,0},head[u]=tol++;
		edge[tol]={u,head[v],0,0},head[v]=tol++;
	}
	void BFS(int start,int end){
		memset(dep,-1,sizeof(dep));
		memset(gap,0,sizeof(gap));
		gap[0]=1,dep[end]=0;
		int front=0,rear=0;
		Q[rear++]=end;
		while(front!=rear){
			int u=Q[front++];
			for(int i=head[u];i!=-1;i=edge[i].next){
				int v=edge[i].to;
				if(dep[v]!=-1)continue;
				Q[rear++]=v,dep[v]=dep[u]+1;
				++gap[dep[v]];
			}
		}
	}
	int ISAP(int start,int end,int N){//源点/汇点/点数
		BFS(start,end);
		memcpy(cur,head,sizeof(head));
		int top=0,u=start,maxflow=0;
		while(dep[start]<N){
			if(u==end){
				int Min=INF,inser;
				for(int i=0;i<top;++i)if(Min>edge[S[i]].cap-edge[S[i]].flow)
					Min=edge[S[i]].cap-edge[S[i]].flow,inser=i;
				for(int i=0;i<top;++i)edge[S[i]].flow+=Min,edge[S[i]^1].flow-=Min;
				maxflow+=Min,top=inser,u=edge[S[top]^1].to;
				continue;
			}
			bool flag=false;int v;
			for(int i=cur[u];i!=-1;i=edge[i].next){
				v=edge[i].to;
				if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u]){
					flag=true,cur[u]=i;break;
				}
			}if(flag){
				S[top++]=cur[u];
				u=v;continue;
			}
			int Min=N;
			for(int i=head[u];i!=-1;i=edge[i].next)
				if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min)
					Min=dep[edge[i].to],cur[u]=i;
			--gap[dep[u]];
			if(!gap[dep[u]])return maxflow;
			dep[u]=Min+1;++gap[dep[u]];
			if(u!=start)u=edge[S[--top]^1].to;
		}
		return maxflow;
	}
}using namespace NetFlow;
int n,a[N],dp[N],len;
int main(){
	while(~scanf("%d",&n)){
		init();len=0;
		int T=n<<1|1;
		for(int i=1;i<=n;++i)scanf("%d",a+i),link(i,i+n,1),dp[i]=1;
		for(int i=1;i<=n;len=max(len,dp[i++]))
			for(int j=1;j<i;++j)if(a[j]<a[i])dp[i]=max(dp[i],dp[j]+1);
		for(int i=1;i<=n;++i){
			if(dp[i]==1)link(0,i,1);
			if(dp[i]==len)link(i+n,T,1);
			for(int j=i+1;j<=n;++j)if(dp[j]==dp[i]+1&&a[j]>a[i])link(i+n,j,1);
		}
		printf("%d\n%d\n",len,ISAP(0,T,T+1));
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值