Contestants Division - POJ 3140 树形dp
来源:互联网 发布:弱电网络进度计划表 编辑:程序博客网 时间:2024/05/29 13:00
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 s, t, 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
题意:给你一棵树,让你将这棵树分成两组,问权值的差最小是多少。
思路:dfs,比较简单就不解释了。另外我有一点困惑的是边的数量比较多,应该存在形成环的情况,可是我去搜题解的时候发现都没有说这一点,好像都是按照一棵树来做的,然后我就试了下,过了……欢迎大家批评指教。
莫名其妙的AC代码如下:
#include<cstdio>#include<cstring>#include<vector>using namespace std;vector<int> vc[100010];long long value[100010],sum,ans;int n,m,vis[100010];void dfs(int u){ int i,j,k,v; vis[u]=1; for(i=0;i<vc[u].size();i++) { v=vc[u][i]; if(vis[v]==0) { dfs(v); value[u]+=value[v]; if(value[v]*2-sum<0) ans=min(ans,sum-value[v]*2); else ans=min(ans,value[v]*2-sum); } }}int main(){ int i,j,k,u,v,t=0; while(~scanf("%d%d",&n,&m) && n+m) { sum=0; ans=1e18; for(i=1;i<=n;i++) { vc[i].clear(); vis[i]=0; } for(i=1;i<=n;i++) { scanf("%I64d",&value[i]); sum+=value[i]; } for(i=1;i<=m;i++) { scanf("%d%d",&u,&v); vc[u].push_back(v); vc[v].push_back(u); } dfs(1); printf("Case %d: %I64d\n",++t,ans); }}
- Contestants Division - POJ 3140 树形dp
- poj 3140 Contestants Division(树形dp)
- POJ 3140 Contestants Division(树形DP)
- poj 3140 Contestants Division (树形dp)
- POJ 3140 Contestants Division(树形DP)
- [poj 3140] Contestants Division 树形DP
- POJ 3140 Contestants Division 简单树形DP
- POJ 3140 Contestants Division 树形dp
- 【POJ】3140 Contestants Division 树形dp
- poj 3140 Contestants Division(树形dp? dfs计数+枚举)
- POJ 3140 Contestants Division(简单的树形dp)
- POJ 3140 Contestants Division (删边,简单树形DP)
- POJ 3140 Contestants Division——树形dp
- POJ 3104 Contestants Division 删边类树形dp
- POJ3140 Contestants Division(树形DP)
- poj3140 Contestants Division(树形DP)
- Contestants Division (树形dp)
- poj 3140 Contestants Division(树dp)
- 常用正则表达式收集
- Erlang inline编译
- C语言的本质(19)——预处理之一:宏定义
- java RSA加密解密
- 二叉平衡树AVL插入删除操作的实现
- Contestants Division - POJ 3140 树形dp
- Commando War
- A - Sum(1.5.1)
- MRC下安全的Block
- Eclipse中从SVN检出项目时遇见的一些问题记录
- 并查集(Union-Find)算法介绍
- Linux中设置服务自启动的三种方式
- scala
- spring tool suite 新建maven项目报错问题