poj3140(树形DP,删边)

来源:互联网 发布:用友软件u8教程 编辑:程序博客网 时间:2024/06/06 13:19

地址:http://poj.org/problem?id=3140

Contestants Division
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 8013 Accepted: 2285

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 integers N and M, (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 to N. The next line has N integers, the Kth integer is equal to the number of students in university numbered K. The number of students in any university does not exceed 100000000. Each of the following M lines has two integers st, and describes a communication line connecting university s and university t. 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

题意:删除某条边,是剩下两部分点权值差最小,输出权值差。

思路:以其中一点为根节点,求没点的子节点的权值和,然后根据每点子节点的权值和求删除该点与该点父亲节点的连线的两部分的点的权值差。

代码:

#include<stdio.h>#include<string.h>#include<iostream>#include<cmath>using namespace std;#define LL __int64#define Ma 100010#define Mod 1000000009struct node{    int to,next;}tree[Ma*2];int m,n,len,head[Ma];LL son[Ma],ans;void add(int l,int r){    tree[len].to=r;    tree[len].next=head[l];    head[l]=len++;}void dfs(int n,int pa){    for(int i=head[n];i!=-1;i=tree[i].next)        if(tree[i].to!=pa){            dfs(tree[i].to,n);            son[n]+=son[tree[i].to];        }}int main(){    int cas=1;    while(scanf("%d%d",&n,&m)>0,n|m){        for(int i=1;i<=n;i++)            scanf("%I64d",&son[i]);        memset(head,-1,sizeof(head));        len=0;        int l,r;        for(int i=1;i<n;i++){            scanf("%d%d",&l,&r);            add(l,r);            add(r,l);        }        printf("Case %d: ",cas++);        dfs(1,-1);        //for(int i=1;i<=n+1;i!=n+1?printf("%d ",son[i]):puts(""),i++);        ans=son[1];        for(int i=2;i<=n;i++){            LL k=son[1]-2*son[i];  //这里根据son数组直接求答案,注意用64位int            if(k<0) k=-k;            ans=min(ans,k);        }        printf("%I64d\n",ans);    }    return 0;}


0 0
原创粉丝点击