poj_3140 Contestants Division(树形dp)

来源:互联网 发布:淘宝怎么改价 编辑:程序博客网 时间:2024/06/14 20:17
Contestants Division
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 9870 Accepted: 2807

Description

In the new ACM-ICPC Regional Contest, a special monitoring and submitting system will be set up, and students will be able to compete at their own universities. However there’s one problem. Due to the high cost of the new judging system, the organizing committee can only afford to set the system up such that there will be only one way to transfer information from one university to another without passing the same university twice. The contestants will be divided into two connected regions, and the difference between the total numbers of students from two regions should be minimized. Can you help the juries to find the minimum difference?

Input

There are multiple test cases in the input file. Each test case starts with two integersN andM, (1 ≤ N ≤ 100000, 1 ≤ M ≤ 1000000), the number of universities and the number of direct communication line set up by the committee, respectively. Universities are numbered from 1 toN. The next line hasN integers, the Kth integer is equal to the number of students in university numberedK. The number of students in any university does not exceed 100000000. Each of the followingM lines has two integerss, t, and describes a communication line connecting universitys and universityt. All communication lines of this new system are bidirectional.

N = 0, M = 0 indicates the end of input and should not be processed by your program.

Output

For every test case, output one integer, the minimum absolute difference of students between two regions in the format as indicated in the sample output.

Sample Input

7 61 1 1 1 1 1 11 22 73 74 66 25 70 0

Sample Output

Case 1: 1
比较暴力的树形dp做法:
设sum[u]为以u为根结点的树中结点值总和。
设dp[u]为在u为根结点的树中将整颗树分成两棵子树,使两子树的sum的差最小的方案,值为子树之差。
整颗树以边 u->v 分成两子树的sum的差即 树总和 - sum[v] - sum[v]
则状态方程:dp[u] = min(dp[u], dp[v], 树总和 - 2*sum[v])
-----------------------------------
注意答案可能是大于int范围的数,所以dp[u]必须初始化为一个更大的数,可以直接初始化为树总和,
因为答案显然不会大于树总和。
 
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define FOP2 freopen("data1.txt","w",stdout)#define inf 0x3f3f3f3f#define maxn 100010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;LL Abs(LL a){    return  a<0 ? -a : a;}int n, m;LL val[maxn], sum[maxn], dp[maxn];vector<int> G[maxn];void dfs(int u, int fa){    dp[u] = sum[0];    for(int i = 0; i < G[u].size(); i++)    {        int v = G[u][i];        if(v == fa) continue;        dfs(v, u);        sum[u] += sum[v];        dp[u] = min(dp[u], Abs(sum[0]-2*sum[v]));        dp[u] = min(dp[u], dp[v]);    }}int main(){    int casen = 0;    while(~scanf("%d%d", &n, &m) && n)    {        memset(sum, 0, sizeof(sum));        for(int i = 1; i <= n; i++) G[i].clear();        for(int i = 1; i <= n; i++)        {            scanf("%lld", &val[i]);            sum[i] = val[i];            sum[0] += val[i];        }        int a, b;        for(int i = 1; i <= m; i++)        {            scanf("%d%d", &a, &b);            G[a].push_back(b);            G[b].push_back(a);        }        dfs(1, 0);        printf("Case %d: ", ++casen);        printf("%lld\n", dp[1]);    }    return 0;}

0 0