1. 程式人生 > >CodeCraft-19 and Codeforces Round #537 Div. 2

CodeCraft-19 and Codeforces Round #537 Div. 2

sign dfs序 endif \n 就是 roo craft and ostream

  D:即有不超過52種物品,求容量為n/2的有序01背包方案數。容易想到設f[i][j]為前i種物品已用容量為j的方案數,有f[i][j]=f[i-1][j-a[i]]*C(n/2-j+a[i],a[i])+f[i-1][j]*C(n/2-s[i-1]+j,a[i])。顯然本質不同詢問只有O(k2)種,暴力就是O(n·k3)的。

  考慮優化,詢問相當於是把兩個物品從背包中拿出,合並兩物品後再放入背包。只要線性完成拿出物品的操作就可以優化到O(n·k2)。然而上面的式子並不能完成還原,因為後一部分的系數可能為0。但註意到進行背包的無序分配後,物品排列的方案數是固定的。所以之前的dp式子改為普通的01背包計數就可以還原了,最後再乘上排列方案數。

#include<iostream> 
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 100010
#define P 1000000007
#define M 55
char getc(){char c=getchar();while ((c<‘A‘||c>‘Z‘)&&(c<‘a‘||c>‘z‘)&&(c<‘0‘||c>‘9‘)) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
	int x=0,f=1;char c=getchar();
	while (c<‘0‘||c>‘9‘) {if (c==‘-‘) f=-1;c=getchar();}
	while (c>=‘0‘&&c<=‘9‘) x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x*f;
}
int n,m,q,f[N],g[N],a[M],sum[M],fac[N],inv[N],ans[M][M],tot;
char s[N];
int trans(char a){if (a<=‘Z‘) return a-‘A‘+1;else return a-‘a‘+27;}
int C(int n,int m){return 1ll*fac[n]*inv[m]%P*inv[n-m]%P;}
int INV_C(int n,int m){return 1ll*inv[n]*fac[m]%P*fac[n-m]%P;}
void inc(int &x,int y){x+=y;if (x>=P) x-=P;}
void ins(int u,int sum,int *f)
{
	if (!u) return;
	for (int j=min(sum,n);j>=0;j--)
	if (j>=u) inc(f[j],f[j-u]);
}
void del(int u,int sum)
{
	if (!u) return;
	for (int j=0;j<=min(sum,n);j++)
	if (j>=u) inc(g[j],P-g[j-u]);
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("d.in","r",stdin);
	freopen("d.out","w",stdout);
	const char LL[]="%I64d\n";
#endif
	scanf("%s",s+1);
	n=strlen(s+1);for (int i=1;i<=n;i++) a[trans(s[i])]++;
	fac[0]=1;for (int i=1;i<=n;i++) fac[i]=1ll*fac[i-1]*i%P;
	inv[0]=inv[1]=1;for (int i=2;i<=n;i++) inv[i]=P-1ll*(P/i)*inv[P%i]%P;
	for (int i=2;i<=n;i++) inv[i]=1ll*inv[i-1]*inv[i]%P;
	m=52;n>>=1;
	tot=1ll*fac[n]*fac[n]%P;
	for (int i=1;i<=m;i++) tot=1ll*tot*inv[a[i]]%P;
	for (int k=1;k<=m;k++)
	{
		memset(f,0,sizeof(f));f[0]=1;
		for (int i=1;i<=m;i++) sum[i]=sum[i-1]+(i!=k)*a[i];
		for (int i=1;i<=m;i++)
		if (i!=k) ins(a[i],sum[i],f);
		for (int j=k+1;j<=m;j++)
		{
			for (int i=0;i<=n;i++) g[i]=f[i];
			del(a[j],sum[m]);
			ins(a[j]+a[k],n*2,g);
			ans[k][j]=1ll*g[n]*C(a[j]+a[k],a[j])%P;
			ans[k][j]=1ll*fac[a[j]]*fac[a[k]]%P*inv[a[j]+a[k]]%P*ans[k][j]%P;
		}
		ins(a[k],n*2,f);
		ans[k][k]=f[n];
	}
	q=read();
	for (int i=1;i<=q;i++)
	{
		int x=trans(s[read()]),y=trans(s[read()]);
		if (x>y) swap(x,y);
		printf("%d\n",1ll*ans[x][y]*tot%P);
	}
	return 0;
}

  E:容易想到建出虛樹,換根只要換個點開始dfs就行了。問題在於dp,傳統樹形dp的思路似乎很難做到O(nm),因為無法避開合並兩個O(m)的狀態(當然應該可以只是我不會)。考慮按dfs序dp,這樣每一個點只要和他的祖先在不同集合即可,祖先兩兩之間顯然也在不同集合,所以能放的集合個數是確定的,就非常顯然了。

#include<iostream> 
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 100010
#define P 1000000007
#define M 310
#define ll long long
char getc(){char c=getchar();while ((c<‘A‘||c>‘Z‘)&&(c<‘a‘||c>‘z‘)&&(c<‘0‘||c>‘9‘)) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
	int x=0,f=1;char c=getchar();
	while (c<‘0‘||c>‘9‘) {if (c==‘-‘) f=-1;c=getchar();}
	while (c>=‘0‘&&c<=‘9‘) x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x*f;
}
int n,m,q,p[N],a[N],fac[N],inv[N],dfn[N],size[N],fa[N][20],cnt,deep[N],t;
struct data{int to,nxt;
}edge[N<<1];
void addedge(int x,int y){t++;edge[t].to=y,edge[t].nxt=p[x],p[x]=t;}
void inc(int &x,int y){x+=y;if (x>=P) x-=P;}
int ksm(int a,int k)
{
	int s=1;
	for (;k;k>>=1,a=1ll*a*a%P) if (k&1) s=1ll*s*a%P;
	return s;
}
int Inv(int a){return ksm(a,P-2);}
int C(int n,int m){return 1ll*fac[n]*inv[m]%P*inv[n-m]%P;}
int lca(int x,int y)
{
	if (deep[x]<deep[y]) swap(x,y);
	for (int j=19;~j;j--) if (deep[fa[x][j]]>=deep[y]) x=fa[x][j];
	if (x==y) return x;
	for (int j=19;~j;j--) if (fa[x][j]!=fa[y][j]) x=fa[x][j],y=fa[y][j];
	return fa[x][0];
}
int calc(int j,int x,int y){if (x+y<j) return 0;return 1ll*C(x,x+y-j)*C(y,x+y-j)%P*fac[x+y-j]%P;}
namespace virtual_tree
{
	int n,p[N],t,T,stk[N],top,f[2][M],g[M],x[N],y[N],DFN[N],DEEP[N],cnt;
	bool flag[N];
	struct data{int to,nxt;}edge[N<<1];
	void addedge(int u,int v){t++;x[t]=u,y[t]=v;}
	void Addedge(int x,int y){T++;edge[T].to=y,edge[T].nxt=p[x],p[x]=T;}
	bool cmp(const int&a,const int &b)
	{
		return dfn[a]<dfn[b];
	}
	bool cmp2(const int&a,const int &b)
	{
		return DFN[a]<DFN[b];
	}
	void build(int root,int m)
	{
		bool f=0;cnt=0;n=m;
		for (int i=1;i<=n;i++) if (a[i]==root) {f=1;break;} 
		if (!f) a[++n]=root;
		sort(a+1,a+n+1,cmp);
		stk[top=1]=1;t=0; 
		for (int i=1+(a[1]==1);i<=n;i++)
		{
			int l=lca(a[i],stk[top]);
			if (stk[top]!=l) 
			{
				while (top>1&&deep[stk[top-1]]>=deep[l]) addedge(stk[top-1],stk[top]),top--;
				if (stk[top]!=l) addedge(l,stk[top]);
				stk[top]=l;
			}
			stk[++top]=a[i];
		}
		while (top>1) addedge(stk[top-1],stk[top]),top--;
		for (int i=1;i<=t;i++) p[x[i]]=p[y[i]]=0;T=0;
		for (int i=1;i<=t;i++) Addedge(x[i],y[i]),Addedge(y[i],x[i]);
		for (int i=1;i<=t;i++) flag[x[i]]=flag[y[i]]=0;
		for (int i=1;i<=n;i++) if (a[i]!=root||f) flag[a[i]]=1;
		DEEP[root]=0;
	}
	void dfs(int k,int from)
	{
		DFN[k]=++cnt;
		for (int i=p[k];i;i=edge[i].nxt)
		if (edge[i].to!=from)
		{
			DEEP[edge[i].to]=DEEP[k]+flag[k];
			dfs(edge[i].to,k);
		}
	}
	void work(int root)
	{
		sort(a+1,a+n+1,cmp2);
		f[!flag[root]][0]=1;for (int j=1;j<=m;j++) f[!flag[root]][j]=0;
		for (int i=1+(!flag[root]);i<=n;i++)
		{
			f[i&1][0]=0;
			for (int j=1;j<=m;j++)
			{
				f[i&1][j]=f[i&1^1][j-1];
				if (j>=DEEP[a[i]]) inc(f[i&1][j],1ll*f[i&1^1][j]*(j-DEEP[a[i]])%P);
			}
		}
	}
}
void dfs(int k,int from)
{
	dfn[k]=++cnt;size[k]=1;
	for (int i=p[k];i;i=edge[i].nxt)
	if (edge[i].to!=from)
	{
		fa[edge[i].to][0]=k;
		deep[edge[i].to]=deep[k]+1;
		dfs(edge[i].to,k);
		size[k]+=size[edge[i].to];
	}
}
signed main()
{
#ifndef ONLINE_JUDGE
	freopen("aaa.in","r",stdin);
	freopen("aaa.out","w",stdout);
	const char LL[]="%I64d\n";
#endif
	n=read(),q=read();
	for (int i=1;i<n;i++)
	{
		int x=read(),y=read();
		addedge(x,y),addedge(y,x);
	}
	fac[0]=1;for (int i=1;i<=n;i++) fac[i]=1ll*fac[i-1]*i%P;
	inv[0]=inv[1]=1;for (int i=2;i<=n;i++) inv[i]=P-1ll*(P/i)*inv[P%i]%P;
	for (int i=2;i<=n;i++) inv[i]=1ll*inv[i-1]*inv[i]%P;
	fa[1][0]=1;dfs(1,1);
	for (int j=1;j<20;j++)
		for (int i=1;i<=n;i++)
		fa[i][j]=fa[fa[i][j-1]][j-1];
	for (int i=1;i<=q;i++)
	{
		int k=read();m=read();int root=read();
		for (int j=1;j<=k;j++) a[j]=read();
		virtual_tree::build(root,k);
		virtual_tree::dfs(root,root);
		virtual_tree::work(root);
		int ans=0;
		for (int j=0;j<=m;j++) inc(ans,virtual_tree::f[virtual_tree::n&1][j]);
		printf("%d\n",ans);
	}
	return 0;
	//NOTICE LONG LONG!!!!!
}

  

CodeCraft-19 and Codeforces Round #537 Div. 2