首页 技术 正文
技术 2022年11月9日
0 收藏 400 点赞 2,435 浏览 3610 个字

题解:

树上Hash

首先重心在边上就把边分裂

以重心为根建树,这样两个根一定对应

然后f[i][j]表示i匹配另一棵的j节点的最小代价

把他们的儿子摘出来做最小权匹配即可

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=1500;
typedef unsigned long long uLint;
const int oo=100000000;//int n;
int rx[maxn],ry[maxn];
int mi[maxn],ming[maxn];int cntedge=0;
int head[maxn]={0};
int to[maxn<<1],nex[maxn<<1];
void Addedge(int x,int y){
nex[++cntedge]=head[x];
to[cntedge]=y;
head[x]=cntedge;
}int root1=0,root2=0;
int siz[maxn]={0},g[maxn]={0};
void Dfs(int x,int fa){
siz[x]=1;g[x]=0;
for(int i=head[x];i;i=nex[i]){
if(to[i]==fa)continue;
Dfs(to[i],x);
siz[x]+=siz[to[i]];
g[x]=max(g[x],siz[to[i]]);
}
g[x]=max(g[x],n-siz[x]);if(g[x]<=g[root1]){
root2=root1;root1=x;
}else if(g[x]<=g[root2]){
root2=x;
}
}uLint hh[maxn];
int dep[maxn];
int father[maxn];
void Gethash(int x,int fa){
hh[x]=666;
father[x]=fa;
dep[x]=dep[fa]+1;
for(int i=head[x];i;i=nex[i]){
if(to[i]==fa)continue;
Gethash(to[i],x);
hh[x]+=hh[to[i]];
}
hh[x]=hh[x]*hh[x];
}int a[maxn];
bool cmp(const int &rhs1,const int &rhs2){
if(dep[rhs1]==dep[rhs2])return hh[rhs1]<hh[rhs2];
else return dep[rhs1]>dep[rhs2];
}struct NetworkFlow{
int totn,s,t;struct Edge{
int from,to,cap,flow,cost;
};
vector<int>G[maxn];
vector<Edge>edges;
void Addedge(int x,int y,int z,int w){
Edge e;
e.from=x;e.to=y;e.cap=z;e.flow=0;e.cost=w;
edges.push_back(e);
e.from=y;e.to=x;e.cap=0;e.flow=0;e.cost=-w;
edges.push_back(e);
int c=edges.size();
G[x].push_back(c-2);
G[y].push_back(c-1);
}int inq[maxn];
int d[maxn];
int p[maxn];
queue<int>q;
int Spfa(int &nowflow,int &nowcost){
for(int i=1;i<=totn;++i){
d[i]=oo;inq[i]=0;
}
d[s]=0;inq[s]=1;q.push(s);
while(!q.empty()){
int x=q.front();q.pop();inq[x]=0;
for(int i=0;i<G[x].size();++i){
Edge e=edges[G[x][i]];
if((e.cap>e.flow)&&(d[x]+e.cost<d[e.to])){
d[e.to]=d[x]+e.cost;
p[e.to]=G[x][i];
if(!inq[e.to]){
q.push(e.to);
inq[e.to]=1;
}
}
}
}if(d[t]==oo)return 0;int f=oo,x=t;
while(x!=s){
Edge e=edges[p[x]];
f=min(f,e.cap-e.flow);
x=e.from;
}
nowflow+=f;nowcost+=f*d[t];
x=t;
while(x!=s){
edges[p[x]].flow+=f;
edges[p[x]^1].flow-=f;
x=edges[p[x]].from;
}
return 1;
}int Mincost(){
int flow=0,cost=0;
while(Spfa(flow,cost)){
}
return cost;
}
void MCMFinit(){
totn=n+n+2;s=n+n+1;t=s+1;
edges.clear();
G[s].clear();G[t].clear();
}
}W;int f[maxn][maxn];
int main(){
scanf("%d",&n);
for(int i=1;i<=n-1;++i){
int x,y;
scanf("%d%d",&x,&y);
Addedge(x,y);
Addedge(y,x);
rx[i]=x;ry[i]=y;
}
for(int i=1;i<=n;++i)scanf("%d",&ming[i]);
for(int i=1;i<=n;++i)scanf("%d",&mi[i]);
mi[n+1]=ming[n+1]=0;g[0]=0x7fffffff;
Dfs(1,0);
if(g[root1]!=g[root2])root2=0;
if(root1&&root2){
cntedge=0;
memset(head,0,sizeof(head));
for(int i=0;i<n;++i){
if((rx[i]==root1&&ry[i]==root2)||(rx[i]==root2&&ry[i]==root1))continue;
Addedge(rx[i],ry[i]);
Addedge(ry[i],rx[i]);
}
Addedge(root1,n+1);
Addedge(n+1,root1);
Addedge(root2,n+1);
Addedge(n+1,root2);
root1=++n;
}
Gethash(root1,0);for(int i=1;i<=n;++i)a[i]=i;
sort(a+1,a+1+n,cmp);for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
int x=a[i];
int y=a[j];
if(hh[x]!=hh[y]){
f[x][y]=oo;
continue;
}
W.MCMFinit();
for(int ii=head[x];ii;ii=nex[ii]){
if(to[ii]==father[x])continue;
W.G[to[ii]].clear();
}
for(int ii=head[y];ii;ii=nex[ii]){
if(to[ii]==father[y])continue;
W.G[to[ii]+n].clear();
}
for(int ii=head[x];ii;ii=nex[ii]){
if(to[ii]==father[x])continue;
for(int jj=head[y];jj;jj=nex[jj]){
if(to[jj]==father[y])continue;
if(hh[to[ii]]!=hh[to[jj]])continue;
W.Addedge(to[ii],to[jj]+n,1,f[to[ii]][to[jj]]);
}
}
for(int ii=head[x];ii;ii=nex[ii]){
if(to[ii]==father[x])continue;
W.Addedge(W.s,to[ii],1,0);
}
for(int ii=head[y];ii;ii=nex[ii]){
if(to[ii]==father[y])continue;
W.Addedge(to[ii]+n,W.t,1,0);
}
f[x][y]=(ming[x]^mi[y])+W.Mincost();
}
}//for(int i=1;i<=n;++i){
//for(int j=1;j<=n;++j){
//cout<<f[i][j]<<' ';
//}
//cout<<endl;
//}
printf("%d\n",f[root1][root1]);
return 0;
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,484
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,899
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,732
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,485
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,125
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,286