1. 程式人生 > >Bzoj3197/洛谷3296 [SDOI2013]刺客信條assassin(樹的重心+樹Hash+樹形DP+KM)

Bzoj3197/洛谷3296 [SDOI2013]刺客信條assassin(樹的重心+樹Hash+樹形DP+KM)

題面

Bzoj

洛谷

題解

首先固定一棵樹,列舉另一棵樹,顯然另一棵樹只有與這棵樹同構才有可能產生貢獻 如果固定的樹以重心為根,那麼另一棵樹最多就只有重心為根才有可能同構了(可能有兩個) 然後就是求改動次數最小值,設$f[x][y]$表示以第一棵樹$x$為根的子樹內和第二棵樹內$y$為根的子樹內,達到目標最少需要改動的次數 我們發現只有同構的子樹需要決策,我們把同構的子樹分別拿出來,我們要做的就是做一個匹配,跑一遍$KM$或者費用流就好了。因為要最小化$f[x][y]$,所以是跑最小完美匹配。 $f[x][y]$要記憶化一下,判斷同構用樹雜湊即可

#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using std::sort; using std::vector;
typedef long long ll;

template<typename T>
void read(T &x) {
    int flag = 1; x = 0; char ch = getchar();
    while(ch < '0' || ch > '9') { if(ch == '-') flag = -flag; ch = getchar(); }
    while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x *= flag;
}

const int N = 1.4e3 + 10, Inf = 1e9 + 7;
void upt0(int &x, int y) { if(x < y) x = y; }
void upt1(int &x, int y) { if(x > y) x = y; }

namespace KM {
	int n, w[N][N], match[N], ret, lx[N], ly[N];
	bool visx[N], visy[N];
	bool Hungary(int x) {
		visx[x] = 1;
		for(int y = 1; y <= n; ++y)
			if(!visy[y] && lx[x] + ly[y] == w[x][y]) {
				visy[y] = true;
				if(!match[y] || Hungary(match[y])) { match[y] = x; return 1; }
			} 
		return 0;
	}
	int main(int opt) {
	    for(int i = 1; i <= n; ++i)
	        for(int j = 1; j <= n; ++j)
	            w[i][j] = opt * w[i][j];
		for(int i = 1; i <= n; ++i) {
			lx[i] = -Inf, ly[i] = 0;
			for(int j = 1; j <= n; ++j) upt0(lx[i], w[i][j]);
		}
		memset(match, 0, sizeof match);
		for(int x = 1; x <= n; ++x)
			while(1) {
				memset(visx, 0, sizeof visx), memset(visy, 0, sizeof visy);
				if(Hungary(x)) break;
				int inc = Inf;
				for(int i = 1; i <= n; ++i)
					if(visx[i])
						for(int j = 1; j <= n; ++j)
							if(!visy[j]) upt1(inc, lx[i] + ly[j] - w[i][j]);
				for(int i = 1; i <= n; ++i) {
					if(visx[i]) lx[i] -= inc;
					if(visy[i]) ly[i] += inc;
				}
			}
		for(int i = 1; i <= n; ++i)
			if(match[i]) ret += w[match[i]][i];
		return opt * ret;
	}
}//KM模板

int n, rt, fir[N], sec[N], f[N][N], c[N][N]; ll hash[N];
int from[N], cnt, to[N << 1], nxt[N << 1];
inline void addEdge(int u, int v) {
	to[++cnt] = v, nxt[cnt] = from[u], from[u] = cnt;
}
int tmp, siz[N];
vector<int> v1[N], v2[N];
inline bool cmp(const int &i, const int &j) { return hash[i] < hash[j]; }

void getrt(int u, int fa) {
	int max_part = 0; siz[u] = 1;
	for(int i = from[u]; i; i = nxt[i]) {
		int v = to[i]; if(v == fa) continue;
		getrt(v, u), siz[u] += siz[v], upt0(max_part, siz[v]);
	} upt0(max_part, n - siz[u]);
	if(max_part < tmp) tmp = max_part, rt = u;
}//尋找樹的重心

void dfs(int u, int fa, vector<int> *V) {
	siz[u] = 1, hash[u] = 0, V[u].clear();
	for(int i = from[u]; i; i = nxt[i]) {
		int v = to[i]; if(v == fa) continue;
		dfs(v, u, V), siz[u] += siz[v], V[u].push_back(v);
	} sort(V[u].begin(), V[u].end(), cmp);
	for(int i = V[u].size() - 1; ~i; --i)
		hash[u] = hash[u] * N + hash[V[u][i]];
	hash[u] = hash[u] * N + siz[u];
}//處理各子樹hash值以及兒子(將兒子放進一個vector裡面)

int dp(int x, int y) {
	if(f[x][y] != -1) return f[x][y];
	f[x][y] = fir[x] ^ sec[y]; int lim = v1[x].size() - 1;
	for(int i = 0; i <= lim; ++i) {
		int j = i;
		while(j < lim && hash[v1[x][j + 1]] == hash[v1[x][i]]) ++j;
		for(int k = i; k <= j; ++k)
			for(int l = i; l <= j; ++l)
				dp(v1[x][k], v2[y][l]);
		for(int k = i; k <= j; ++k)
			for(int l = i; l <= j; ++l)
				KM::w[k - i + 1][l - i + 1] = dp(v1[x][k], v2[y][l]);
        //初始化邊權
		KM::ret = 0, KM::n = j - i + 1, f[x][y] += KM::main(-1), i = j;
        //最小化f[x][y]
	} return f[x][y];
}

int main () {
	read(n);
	for(int i = 1, u, v; i < n; ++i)
		read(u), read(v), addEdge(u, v), addEdge(v, u);
	for(int i = 1; i <= n; ++i) read(fir[i]);
	for(int i = 1; i <= n; ++i) read(sec[i]);
	tmp = Inf, getrt(1, 0), dfs(rt, 0, v2); ll tmp = hash[rt];
	int ans = Inf;
	for(int i = 1; i <= n; ++i) {//暴力列舉重心
		dfs(i, 0, v1);
		if(hash[i] == tmp) memset(f, -1, sizeof f), upt1(ans, dp(i, rt));
	} printf("%d\n", ans);
	return 0;
}