Poj 3140 Contestants Division (DP_树形DP)

题目链接:http://poj.org/problem?id=3140

题目大意:给定一棵n棵节点的树,求删去某条边后两个分支的最小差异值。

解题思路:树形DP.深搜两次,第一次深搜记录从当前节点的子孙节点总数(包括自己),第一次算预处理,复杂度为O(N),第二次利用第一次的结果找去掉某条边后的最小差异值,答案需用__int64。其实一切皆模拟,这题只是模拟地比较有规律而已。


测试数据:

7 6
2 2 2 2 2 2 2
1 2
2 7
3 7
4 6
6 2
5 7
0 0


代码:
#include <stdio.h>
#include <string.h>
#define MAX 110000
#define min(a,b) (a)<(b)?(a):(b)
#define Rabs(a)  (a<0?-a:a)
#define int64 __int64
#define INF 21474836470000000


struct node {

	int v;
	node *next;
}*head[MAX],tree[MAX*2];
int n,m,ptr,val[MAX],vis[MAX];
int64 dp[MAX],ans,cnt,sum;


void Initial() {

	ans = INF;
	sum = ptr = 0;
	memset(vis,0,sizeof(vis));
	memset(head,NULL,sizeof(head));
}
void AddEdge(int x,int y) {

	tree[ptr].v = y;
	tree[ptr].next = head[x],head[x] = &tree[ptr++];
	tree[ptr].v = x;
	tree[ptr].next = head[y],head[y] = &tree[ptr++];
}
void Dfs_Ini(int s,int pa) {
	//自底向上计算每个节点的子孙个数
	dp[s] = val[s];
	vis[s] = 1;
	node *p = head[s];


	while (p != NULL) {

		if (vis[p->v] == 0) {

			Dfs_Ini(p->v,s);
			dp[s] += dp[p->v];
		}
		p = p->next;
	}
}
void Dfs_Solve() {

	for (int i = 1; i <= n; ++i) {
		//枚举每一条边
		node *p = head[i];
		while (p != NULL) {
			
			int64 tp = dp[p->v];
			ans = min(ans,Rabs((2 * tp - sum)));
			p = p->next;
		}
	}
}


int main()
{
	int i,j,k,a,b,cas = 0;



	while (scanf("%d%d",&n,&m),n+m) {
	
		Initial();
		for (i = 1; i <= n; ++i)
			scanf("%d",&val[i]),sum += val[i];
		for (i = 1; i <= m; ++i) {

			scanf("%d%d",&a,&b);
			AddEdge(a,b);
		}


		Dfs_Ini(1,0);			//第一次深搜,记录当前节点的子孙总数
		memset(vis,0,sizeof(vis));
		Dfs_Solve();			//更新答案
		printf("Case %d: %I64d\n",++cas,ans);
	}
}


本文ZeroClock原创,但可以转载,因为我们是兄弟。

展开阅读全文

Contestants Division

01-18

DescriptionnnIn 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?nnInputnnThere 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 s, t, and describes a communication line connecting university s and university t. All communication lines of this new system are bidirectional.nnN = 0, M = 0 indicates the end of input and should not be processed by your program.nnOutputnnFor every test case, output one integer, the minimum absolute difference of students between two regions in the format as indicated in the sample output.nnSample Inputnn7 6n1 1 1 1 1 1 1n1 2n2 7n3 7n4 6n6 2n5 7n0 0nSample OutputnnCase 1: 1 问答

没有更多推荐了,返回首页