poj 3140 Contestants Division DFS

来源:互联网 发布:搜索排名优化策划 编辑:程序博客网 时间:2024/05/21 11:19

原题:

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 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 toN. The next line has N 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 integers s, t, and describes a communication line connecting universitys 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

数据量较大,乍一看上去不像搜索题。题中有一句话为重点:there will be only one way to transfer information from one university to another without passing the same university twice.意为在两所大学间传输信息的路只有一条。明显,这几所大学之间组成的不是一张图,而是一棵树。题意就是,将这棵树分成两棵树,使这两棵树中学生人数的差的绝对值最小。

方法:

用深搜遍历这棵树,并将每个结点及其儿子的总权重记录下来。


代码:

//By Sean Chen#include <iostream>#include <cstdio>#include <cstring>#define Max 100005using namespace std;int n,m,cnt;int head[Max];long long sum,ans,stunum[Max];long long abs(long long a){    if (a>=0)        return a;    return -a;}struct data{int v,next;}edge[Max*10*2];long long dfs(int u,int root)           //深搜{long long dp=0;int pos=head[u];while (pos!=-1)    {int v=edge[pos].v;if(v!=root)            dp+=dfs(v,u);        pos=edge[pos].next;}dp+=stunum[u];if(abs(sum-2*dp)<ans)             //dp记录当前结点及其孩子的权重。        ans=abs(sum-2*dp);return dp;}int main(){int u,v,num=0;scanf("%d%d",&n,&m);while(n && m)    {memset(head,-1,sizeof(head));cnt=0;sum=0;for(int i=1;i<=n;++i)        {            scanf("%lld",&stunum[i]);            sum+=stunum[i];        }for(int i=0;i<m;++i)        {                              //记录路径scanf("%d%d",&u,&v);edge[cnt].v=v;edge[cnt].next=head[u];head[u]=cnt++;edge[cnt].v=u;edge[cnt].next=head[v];head[v]=cnt++;}ans=sum;dfs(1,-1);printf("Case %d: %lld\n",++num,ans);scanf("%d%d",&n,&m);}return 0;}


0 0
原创粉丝点击