POJ3140_Contestants Division_深搜

Contestants Division
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 9874 Accepted: 2809

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 6
1 1 1 1 1 1 1
1 2
2 7
3 7
4 6
6 2
5 7
0 0

Sample Output

Case 1: 1

Source


大致题意:

从输入数据入手解释题意如下:

第一行:N,M; 参加竞赛的共N个学校,在这些学校之间建立M条联系的纽带,保证所有学校都被连接在一起,且连接通路不会重复经过一个学校两次。由此可以推出两点: 1)M=N-1。在N个学校之间两两建立联系,联系数显然是N-1。但是这点实际做题时没啥卵用。。。 2)最后实际上形成了一个树形图。因为学校不会被重复经过,即没有回路,整个图就可以看做以任意点为根的树。画一下样例的示意图就很清楚了。整体思路就是建立在这一点的基础上。

第二行:含有N个数字,表示N个学校中各自的参赛学生数。所有学校1到N编号。

从第三行开始的N行:每行两个数字,表示在这两个学校之间建立联系。

实现要求:把所有的学校分成两个区,要求两个区的人数尽可能相等。输出最佳情况下的两区人数之差的绝对值。(当然分到一个区的学校要联通成一个整体)。


总体思路:

网上的聚聚们说这个题是树的dp,弱不是很懂,但所幸深搜也能做。

首先选择任意一个学校作为根节点,向下搜索到叶,然后沿着树枝逐级向上返回,比较更新最小差值。


解题过程:

有了思路解起来还是蛮容易的。


#include<cstdio>
#include<vector>
#define _max 100010

int sch [_max];//保存每个学校的学生数目
int n,m,cas;
long long tot,ans;//tot:所有学校的总人数	ans:最终答案即最小差值
std::vector<int>con[_max];//记录每个学校所连接的学校

long long dfs (int now,int pre)//now:当前学校	pre:上一级学校
{
	long long sum=sch[now];	//以此学校为根的子树共有多少学生
	for(int i=0;i<con[now].size();i++){
		if(con[now][i]==pre) continue;//防止无意义的循环
		sum+=dfs(con[now][i],now);//首先向下搜索子树,子树搜索结束后,把子树上的学生都加到本级根上
	}

	long long dif=tot-2*sum;//子树搜索结束,此节点实际代表以它为根的子树。
	dif=dif>0?dif:-dif;//取绝对值
	ans=ans<dif?ans:dif;//更新ans
	return sum;//将本级以下的学生数返回到上一级根
}

int main ()
{
	while(scanf("%d%d",&n,&m)&&n){

		for(int i=1;i<=n;i++)//将连接关系初始化
			con[i].clear();

		tot=0;//输入学校人数,计算总人数
		for(int i=1;i<=n;i++){
			scanf("%d",sch+i);
			tot+=sch[i];
		}
		
		int x,y;//建立学校间的联系
		for(int i=1;i<=m;i++){
			scanf("%d%d",&x,&y);
			con[x].push_back(y);//双向联系
			con[y].push_back(x);
		}

		ans=tot;//将最小差值初始化为总人数
		dfs(1,0);

		printf("Case %d: ",++cas);
		printf("%I64d\n",ans);

	}

	return 0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值