poj3140(树形DP,删边)
来源:互联网 发布:用友软件u8教程 编辑:程序博客网 时间:2024/06/06 13:19
地址:http://poj.org/problem?id=3140
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
题意:删除某条边,是剩下两部分点权值差最小,输出权值差。
思路:以其中一点为根节点,求没点的子节点的权值和,然后根据每点子节点的权值和求删除该点与该点父亲节点的连线的两部分的点的权值差。
代码:
#include<stdio.h>#include<string.h>#include<iostream>#include<cmath>using namespace std;#define LL __int64#define Ma 100010#define Mod 1000000009struct node{ int to,next;}tree[Ma*2];int m,n,len,head[Ma];LL son[Ma],ans;void add(int l,int r){ tree[len].to=r; tree[len].next=head[l]; head[l]=len++;}void dfs(int n,int pa){ for(int i=head[n];i!=-1;i=tree[i].next) if(tree[i].to!=pa){ dfs(tree[i].to,n); son[n]+=son[tree[i].to]; }}int main(){ int cas=1; while(scanf("%d%d",&n,&m)>0,n|m){ for(int i=1;i<=n;i++) scanf("%I64d",&son[i]); memset(head,-1,sizeof(head)); len=0; int l,r; for(int i=1;i<n;i++){ scanf("%d%d",&l,&r); add(l,r); add(r,l); } printf("Case %d: ",cas++); dfs(1,-1); //for(int i=1;i<=n+1;i!=n+1?printf("%d ",son[i]):puts(""),i++); ans=son[1]; for(int i=2;i<=n;i++){ LL k=son[1]-2*son[i]; //这里根据son数组直接求答案,注意用64位int if(k<0) k=-k; ans=min(ans,k); } printf("%I64d\n",ans); } return 0;}
- poj3140(树形DP,删边)
- poj3140 Contestants Division(树形dp)
- POJ3140 简单树形DP
- POJ3140【树形DP】
- POJ3140 Contestants Division(树形DP)
- poj3140 Contestants Division(树形DP)
- poj3140--Contestants Division(树形dp-水题)
- poj3140(经典-树的dp)
- poj3140 树状dp
- POJ3140:Contestants Division(DFS,树形DP)
- poj3140
- poj3140
- poj3140
- poj3140-树型dp&搜索-Contestants Division
- Contestants Division(树形dp+删边)
- poj1155 (树形dp)
- poj1947(树形dp)
- hdu2196Computer(树形dp)
- RTEMS在S3C2440上的移植-(5)
- 利用ajax进行异步请求验证
- 现代高低温环境试验箱物联网速度的发展要求
- TestNG annotation
- 如何将多张jpg转换成pdf
- poj3140(树形DP,删边)
- 服务端统一时间戳 boost::date_time UTC
- Android API之Typeface代码演示
- 套接字编程非阻塞
- Html5 中获取镜像图像 - 解决 WebGL 中纹理倒置问题
- 搭建RHadoop环境
- ACdream 1095 EOF女神的相反数(数学:二进制处理)
- My first blog
- mysql错误处理之ERROR 1665 (HY000)