HDU5905(树dp)

来源:互联网 发布:吃饭 叫号软件 编辑:程序博客网 时间:2024/06/15 03:14

Black White Tree

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 80 Accepted Submission(s): 40

Problem Description
Alex has a tree T with n vertices conveniently labeled with 1,2,…,n. Each vertex of the tree is colored black or white. For every integer pair (a,b), Alex wants to know whether there is a subtree of T with exact a white vertices and b black vertices.

A subtree of T is a subgraph of T that is also a tree.

Input
There are multiple test cases. The first line of input contains an integer T (1≤T≤100) indicating the number of test cases. For each test case:

The first line contains an integer n (1≤n≤2000). The next line contains a binary string s length n where si denoting the color of the i-th vertex (‘0’ means white, while ‘1’ means black). Each of the following n−1 lines contains two integers ai,bi, denoting an edge between vertices ai and bi (1≤ai,bi≤n).

Output
For each test case, output an integer W=∑na=0∑nb=0(a+1)(b+1)S(a,b). S(a,b)=1 if there is a subtree with exact a white vertices and b black vertices, or 0 otherwise.

Sample Input
3
4
1010
1 4
2 4
3 4
3
101
1 2
2 3
10
1010111001
1 2
1 3
1 8
2 4
2 6
2 7
3 9
4 5
7 10

Sample Output
33
15
365

题意:这是bc题,有中文题,自己去找。

解题思路:有一个结论,就是大小为i的子树中,肯定有一个里面的黑点数最大,我们设为Max,肯定有一个里面的黑点数最小,我们设为Min,那么这个结论就是这个树一定存在大小为i,黑点数介于Max与Min之间的子树,具体怎么证明,本弱鸡也不会,跪请大佬解释下怎么证明,那么知道了这个结论后就好办了,我们只需要求出大小为i的子图中Max与Min就行。具体做发看代码注释。

#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 2e3 + 10;const ll inf = 1e9;ll Max[maxn];//Max[i]表示大小为i的连通图黑点数的最大值ll Min[maxn];//Min[i]表示大小为i的连通图黑点数的最小值ll dpMax[maxn][maxn];//dpMax[i][j]表示以i为根的子树大小为j的最大黑点数ll dpMin[maxn][maxn];//dpMin[i][j]表示以i为根的子树大小为j的最小黑点数ll dp[maxn];//dp[i]表示以i为根的子树的大小bool visit[maxn];int n;char s[maxn];vector<int> g[maxn];void init(){    memset(visit,false,sizeof(visit));    memset(dp,0,sizeof(dp));    for(int i = 1; i <= n; i++)    {        g[i].clear();        Max[i] = -inf;        Min[i] = inf;    }    for(int i = 1; i <= n; i++)    {        for(int j = 1; j <= n; j++)        {            dpMax[i][j] = -inf;            dpMin[i][j] = inf;        }    }}void dfs(int root){    if(visit[root]) return;    visit[root] = true;    dp[root] = 1;    for(int i = 0; i < g[root].size(); i++)    {        int v = g[root][i];        if(!visit[v])        {            dfs(v);            dp[root] += dp[v];        }    }}void solve(int root){    if(visit[root]) return;    dpMax[root][0] = dpMin[root][0] = 0;    if(s[root] == '1')    {        dpMax[root][1] = dpMin[root][1] = 1;    }    else    {        dpMax[root][1] = dpMin[root][1] = 0;    }    visit[root] = true;    int ans = 1;    for(int i = 0; i < g[root].size(); i++)    {        int v = g[root][i];        if(!visit[v])        {            solve(v);            for(int j = ans; j > 0; j--)            {                for(int k = 1; k <= dp[v]; k++)                {                    dpMax[root][j + k] = max(dpMax[root][j + k],dpMax[root][j] + dpMax[v][k]);                    dpMin[root][j + k] = min(dpMin[root][j + k],dpMin[root][j] + dpMin[v][k]);                }            }            ans += dp[v];        }    }}int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        init();        scanf("%s",s + 1);        int u,v;        for(int i = 1; i < n; i++)        {            scanf("%d%d",&u,&v);            g[u].push_back(v);            g[v].push_back(u);        }        dfs(1);//假设以一为根        memset(visit,false,sizeof(visit));        solve(1);        for(int i = 1; i <= n; i++)        {            for(int j = 1; j <= n; j++)            {                Max[j] = max(Max[j],dpMax[i][j]);                Min[j] = min(Min[j],dpMin[i][j]);            }        }        ll sum = 1;//a = 0,b = 0的特殊情况        for(int i = 1; i <= n; i++)        {            for(int j = Max[i]; j >= Min[i]; j--)            {                sum += (j + 1)*(i - j + 1);            }        }        printf("%lld\n",sum);    }    return 0;}
0 0
原创粉丝点击