[KM 树同构Hash DP] BZOJ 3197 [Sdoi2013]assassin

丢下题解跑:http://blog.csdn.net/PoPoQQQ/article/details/43206463


#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define cl(x) memset(x,0,sizeof(x))
using namespace std;
typedef unsigned long long ull;
 
inline char nc()
{
	static char buf[100000],*p1=buf,*p2=buf;
	if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
	return *p1++;
}
 
inline void read(int &x)
{
	char c=nc(),b=1;
	for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
	for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
 
namespace KMA{
	const int N=15;
	int n,w[N][N];
	int boy[N],lx[N],ly[N],sla[N];
	int S[N],T[N];
	inline bool match(int u){
		S[u]=1;
		for (int v=1;v<=n;v++){
			if (T[v]) continue;
			if (lx[u]+ly[v]==w[u][v]){
				T[v]=1;
				if (!boy[v] || match(boy[v]))
					return boy[v]=u,1;
			}else
				sla[v]=min(sla[v],lx[u]+ly[v]-w[u][v]);
		}return 0;
	}
	inline int KM(){
		cl(boy); cl(ly);
		for (int i=1;i<=n;i++){
			lx[i]=-1<<30;
			for (int j=1;j<=n;j++) lx[i]=max(lx[i],w[i][j]);
		}
		for (int i=1;i<=n;i++){
			for (int j=1;j<=n;j++) sla[j]=1<<30;
			for (;;){
				cl(S); cl(T); if (match(i)) break;
				int a=1<<30;
				for (int j=1;j<=n;j++) if (!T[j]) a=min(a,sla[j]);
				for (int j=1;j<=n;j++) if (S[j]) lx[j]-=a;
				for (int j=1;j<=n;j++) if (T[j]) ly[j]+=a; else sla[j]-=a;
			}
		}
		int ret=0;
		for (int i=1;i<=n;i++) if (boy[i]) ret+=w[boy[i]][i];
		return ret;
	}
}
 
const int N=705;
 
struct edge{
	int u,v; int next;
}G[N<<1];
int head[N],inum;
inline void add(int u,int v,int p){
	G[p].u=u; G[p].v=v; G[p].next=head[u]; head[u]=p;
}
 
int n,ru[N],rv[N];
int size[N];
int minimum,rt[2],rot;
 
#define U G[p].u
#define V G[p].v
void Root(int u,int fa){
	int maximum=0; size[u]=1;
	for (int p=head[u];p;p=G[p].next)
		if (V!=fa)
			Root(V,u),maximum=max(maximum,size[V]),size[u]+=size[V];
	maximum=max(maximum,n-size[u]);
	if (maximum==minimum) rt[1]=u;
	if (maximum<minimum) minimum=maximum,rt[0]=u,rt[1]=0;
}
 
const int pri[]={0,2,23,29,37,53,67,73,79,83,89,97};
 
ull H[N],tmp[N];
int fat[N],depth[N];
void Hash(int u,int fa){
	fat[u]=fa; depth[u]=depth[fa]+1;
	for (int p=head[u];p;p=G[p].next)
		if (V!=fa)
			Hash(V,u);
	int pnt=0;
	for (int p=head[u];p;p=G[p].next)
		if (V!=fa)
			tmp[++pnt]=H[V];
	sort(tmp+1,tmp+pnt+1);
	H[u]=1;
	for (int i=1;i<=pnt;i++)
		H[u]+=tmp[i]*pri[i];
}
 
int a[N];
bool cmp(int x,int y){
	return depth[x]==depth[y]?H[x]<H[y]:depth[x]>depth[y];
}
bool jud(int x,int y){
	return depth[x]==depth[x] && H[x]==H[y];
}
 
int ans,f[N][N];
int ch1[15],ch2[15],tot;
int sm[N],tm[N];
 
int main()
{
	freopen("t.in","r",stdin);
	freopen("t.out","w",stdout);
	read(n);
	for (int i=1;i<n;i++)
		read(ru[i]),read(rv[i]),add(ru[i],rv[i],++inum),add(rv[i],ru[i],++inum);
	for (int i=1;i<=n;i++) read(sm[i]);
	for (int i=1;i<=n;i++) read(tm[i]);
	minimum=1<<30; Root(1,0);
	if (rt[0] && rt[1])
	{
		rot=++n;
		cl(head); inum=0;
		for (int i=1;i<=n-2;i++)
			if ((ru[i]==rt[0] && rv[i]==rt[1]) || (ru[i]==rt[1] && rv[i]==rt[0]))
				add(ru[i],n,++inum),add(n,ru[i],++inum),add(rv[i],n,++inum),add(n,rv[i],++inum);
			else
				add(ru[i],rv[i],++inum),add(rv[i],ru[i],++inum);
	}
	else
		rot=rt[0];
	Hash(rot,0);
	for (int i=1;i<=n;i++) a[i]=i;
	sort(a+1,a+n+1,cmp);
	for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) f[i][j]=1<<25;
	int l,r;
	for (l=1;l<=n;l=r+1)
	{
		r=l;
		while (r+1<=n && jud(a[r],a[r+1])) r++;
		for (int i=l;i<=r;i++)
			for (int j=l;j<=r;j++)
			{
				tot=0;
				for (int p=head[a[i]];p;p=G[p].next)
					if (V!=fat[a[i]])
						ch1[++tot]=V;
				tot=0;
				for (int p=head[a[j]];p;p=G[p].next)
					if (V!=fat[a[j]])
						ch2[++tot]=V;
				KMA::n=tot;
				for (int x=1;x<=tot;x++)
					for (int y=1;y<=tot;y++)
						KMA::w[x][y]=-f[ch1[x]][ch2[y]];
				f[a[i]][a[j]]=-KMA::KM()+(sm[a[i]]^tm[a[j]]);
			}
	}
	ans=f[rot][rot];
	printf("%d\n",ans);
	return 0;
}


已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页