【BZOJ4013】【HNOI2015】實驗比較(樹形DP,組合)

Description

https://www.lydsy.com/JudgeOnline/problem.php?id=4013


Solution

先將相等的都丟進一個並查集,又因爲有條件:“小 D 都最多隻記住了某一張質量不比 i 差的另一張圖片 Ki”,我們對大小關係進行建圖後是一棵樹(如果是森林,我們新建一個點連接所有根節點即可)。
dp[u][i]dp[u][i]表示uu的子樹內分成了ii段的方案數(將相同的元素看做一段)。
對於uu的每個子節點,我們有:
dp[u][i]=j,kdp[u][j]×dp[v][k]×(i1j1)(j1j+ki)dp[u][i] = \sum_{j,k} dp'[u][j]\times dp[v][k] \times \binom{i-1}{j-1}\binom{j-1}{j+k-i}
(dpdp'爲上一個子節點轉移後的結果)
答案爲dp[root][i]\sum dp[root][i]


Code

/************************************************
 * Au: Hany01
 * Date: Sep 15th, 2018
 * Prob: BZOJ4013 HNOI2015 實驗比較
 * Email: [email protected] & [email protected]
 * Inst: Yali High School
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define SZ(a) ((int)(a).size())
#define ALL(a) a.begin(), a.end()
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read() {
	static int _, __; static char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 105, MOD = 1e9 + 7;

int C[maxn][maxn], n, m, sz[maxn], ans, dp[maxn][maxn], deg[maxn], tmp[maxn], beg[maxn], nex[maxn], v[maxn], e, tot;
PII E[maxn];

struct DSU {
	int fa[maxn];
	inline void init(int n) { For(i, 1, n) fa[i] = i; }
	int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
	inline void merge(int x, int y) { if ((x = find(x)) ^ (y = find(y))) fa[x] = y; }
}dsu;

inline void add(int uu, int vv) { v[++ e] = vv, nex[e] = beg[uu], beg[uu] = e, ++ deg[vv]; }
inline int ad(int x, int y) { if ((x += y) >= MOD) return x - MOD; return x; }

void DFS(int u) {
	sz[u] = dp[u][1] = 1;
	for (register int p = beg[u]; p; p = nex[p]) {
		DFS(v[p]);
		For(i, 1, sz[u] + sz[v[p]]) tmp[i] = 0;
		For(j, 1, sz[u]) For(k, 1, sz[v[p]]) For(i, max(j, k), j + k)
			tmp[i] = ad(tmp[i], (LL)dp[u][j] * dp[v[p]][k] % MOD * C[i - 1][j - 1] % MOD * C[j - 1][j + k - i] % MOD);
		sz[u] += sz[v[p]];
		For(i, 1, sz[u]) dp[u][i] = tmp[i];
	}
}

int main()
{
#ifdef hany01
	freopen("bzoj4013.in", "r", stdin);
	freopen("bzoj4013.out", "w", stdout);
#endif

	static char ty;
	static int x, y;

	dsu.init(n = read()), m = read();
	For(i, 0, n) {
		C[i][0] = C[i][i] = 1;
		For(j, 1, i - 1) C[i][j] = ad(C[i - 1][j], C[i - 1][j - 1]);
	}
	For(i, 1, m) {
		x = read(), scanf("%c", &ty), y = read();
		if (ty == '=') dsu.merge(x, y);
		else E[++ tot] = mp(x, y);
	}
	int cnt = 0; For(i, 1, n) if (dsu.find(i) == i) ++ cnt;
	if (tot >= cnt) { puts("0"); return 0; }
	For(i, 1, tot) add(dsu.find(E[i].x), dsu.find(E[i].y));
	For(i, 1, n) if (!deg[i] && dsu.fa[i] == i) add(0, i);
	DFS(0);
	For(i, 1, n + 1) ans = ad(ans, dp[0][i]);
	printf("%d\n", ans);

	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章