POJ 3104 Contestants Division 删边类树形dp
来源:互联网 发布:淘宝信誉兼职 编辑:程序博客网 时间:2024/06/05 16:55
超级水题
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
Source
题意:给出一棵结点带权的树,删去一条边,问剩下两个连通块的权值之差最小为多少
/**========================================== * This is a solution for ACM/ICPC problem * * @source£º * @type: * @author: wust_ysk * @blog: http://blog.csdn.net/yskyskyer123 * @email: 2530094312@qq.com *===========================================*/#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<vector>using namespace std;typedef long long ll;const int INF =0x3f3f3f3f;const int inf =1e15;const int maxn=100000 ;//const int maxV=12 ;int n,m;int a[maxn+4];ll dp[maxn+4];vector<int>G[maxn+4];ll mini,sum;ll abs2(ll x){ return x>=0?x:-x;}void init(){ for(int i=1;i<=n;i++ ) G[i].clear();}void add_edge(int x,int y){ G[x].push_back(y); G[y].push_back(x);}void dfs(int x,int fa){ dp[x]=a[x]; for(int i=0;i<G[x].size();i++) { int y=G[x][i]; if(y==fa) continue; dfs(y,x); dp[x]+=dp[y]; } mini=min(abs2(2*dp[x]-sum),mini);}int main(){ int x,y,kase=0; while(~scanf("%d%d",&n,&m)&&(n||m)) { init(); sum=0; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); sum+=a[i]; } mini=sum; for(int i=1;i<=m;i++) { scanf("%d%d",&x,&y); add_edge(x,y); } dfs(1,-1); printf("Case %d: %lld\n",++kase,mini); } return 0;}
- POJ 3104 Contestants Division 删边类树形dp
- 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
- POJ3140 Contestants Division(树形DP)
- poj3140 Contestants Division(树形DP)
- Contestants Division (树形dp)
- poj 3140 Contestants Division(树形dp? dfs计数+枚举)
- POJ 3140 Contestants Division(简单的树形dp)
- POJ 3140 Contestants Division (删边,简单树形DP)
- POJ 3140 Contestants Division——树形dp
- poj3140--Contestants Division(树形dp-水题)
- [iOS]UICollectionView循环滚动功能的实现思路
- PHP Session简单应用例程
- Xcode7 添加PCH文件(2)
- 利用hsdis和JITWatch查看分析HotSpot JIT compiler生成的汇编代码
- ubuntu下jdk的javac和java
- POJ 3104 Contestants Division 删边类树形dp
- C语言之实用函数
- 嵌入式Linux之主机平台环境的搭建
- 做程序员的一些励志故事----持续之道(程序员修炼之道)
- sqlcipher开源项目的ubuntu编译和交叉编译
- Java下appium 里的一方法的使用心得
- Live555在Android下改造
- Mac下Android studio 之NDK配置教程
- spark核心术语和运行流程