UVA10253 - Series-Parallel Networks(樹形DP+(組合) _ 建模很好)(好題)

題目鏈接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1194

大致題意: 給你n條邊,問你恰好用n條邊,能構成幾種串並聯網絡。(串聯的各個部分可以任意調換,並聯在一起的各個部分也可以任意調換,若通過調換可得,則二者視爲等效)

思路:一個n條邊的整體總能拆成一些子整體的串聯或一些子整體的並聯。 這些子整體仍然可以這樣拆

每個不同的整體必然有且只有一種唯一的拆開方式。

這樣就可以轉化成在樹上操作,最終拆成只有1條邊的整體,就是葉子,然後問題就是一顆n個葉子的樹有多少種構樹方式,每個孩子節點位置等價,由於位置等價,所以可以按子樹的葉子節點從小到大活從大到小DP,這樣構樹就不會重複,現在問題就是一些子樹的包含的葉節點個數一樣,這些子樹在DP的時候要避免重複計數,只能枚舉包含相同葉子的子樹有m顆,然後轉化成這樣的可重組合問題:在每次在n個元素中選m個元素,元素可以重複選,答案就是C(n+m-1, m)

所以DP方程是(dp[x][y]表示含有x個葉子的樹,它的每棵子樹的葉子個數<=y 有多少種構成方式) 

dp[sum][maxn] = sigma_m  ( C(DP(maxn,maxn-1) - 1 + m, m) * DP(sum - m * maxn, maxn - 1)  )

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <ctime>
#include <bitset>
#include <algorithm>
#define SZ(x) ((int)(x).size())
#define ALL(v) (v).begin(), (v).end()
#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)
#define reveach(i, v) for (__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++ i)
#define REP(i,n) for ( int i=1; i<=int(n); i++ )
#define rep(i,n) for ( int i=0; i< int(n); i++ )
using namespace std;
typedef long long ll;
#define X first
#define Y second
#define PB push_back
#define MP make_pair
typedef pair<int,int> pii;

template <class T>
inline bool RD(T &ret) {
        char c; int sgn;
        if (c = getchar(), c == EOF) return 0;
        while (c != '-' && (c<'0' || c>'9')) c = getchar();
        sgn = (c == '-') ? -1 : 1 , ret = (c == '-') ? 0 : (c - '0');
        while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
        ret *= sgn;
        return 1;
}
template <class T>
inline void PT(T x) {
        if (x < 0) putchar('-') ,x = -x;
        if (x > 9) PT(x / 10);
        putchar(x % 10 + '0');
}

const int N = 33;
ll dp[N][N]; // x is leaf , y is maxn
ll C(ll n,ll m) {
	ll ans = 1;
	for(int i = 1; i <= m; i ++ ) ans = ans * (n - m + i) / i ;
	return ans ;
}
ll DP(int n, int maxn) {
	ll &ans = dp[n][maxn];
	if( maxn == 1 ) return ans = 1;
   	if( ans != -1 ) return ans ;
	ans = 0;	
	for(int c = 0; c * maxn <= n; c ++ ) {
		ans += C(DP(maxn,maxn-1) - 1 + c, c) * DP(n - c * maxn, maxn - 1) ; 
	}
	return ans ;
}
int main() {
	int n ;
	while( RD(n) ) {
		if( n == 0 ) break;
		memset( dp, -1, sizeof(dp) );
		dp[1][1] = 1;
		if( n == 1 ) {
			puts("1");
		   	continue;
		}
		PT(2 * DP( n, n - 1 ));
		puts("");
	}
}



發佈了308 篇原創文章 · 獲贊 11 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章