poj 3140(树形dp)
来源:互联网 发布:淘宝1001夜视频 编辑:程序博客网 时间:2024/06/07 20:40
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 记录以i点为根的子树的所有节点的权值
#include <vector>#include <cstring>#include <cmath>#include <iostream>#include <cstdio>using namespace std;#define ll long longconst int N=1e5+5;vector<int>G[N];ll d[N],a[N],n,m,ans,sum;void dfs(int u,int p){ ll tol=a[u]; for(int i=0;i<G[u].size();i++) { int v=G[u][i]; if(v==p)continue; dfs(v,u); tol+=d[v]; } d[u]=tol; ll b=fabs(2*d[u]-sum); ans=min(ans,b);}int main(){ int cnt=1; while(~scanf("%d%d",&n,&m)&&(n+m)) { sum=0; memset(d,0,sizeof(d)); for(int i=1;i<=n;i++) { scanf("%d",a+i); sum+=a[i]; G[i].clear(); } for(int i=1;i<=m;i++) { int u,v; scanf("%d%d",&u,&v); G[u].push_back(v); G[v].push_back(u); } ans=sum; //bitch dfs(1,-1); // for(int i=1;i<=n;i++) // printf("%d ",d[i]); // puts(""); printf("Case %d: %lld\n",cnt++,ans); //必须为long long } return 0;}
- poj 3140 树形dp
- poj-3140-树形dp
- POJ 3140 树形dp
- poj 3140(树形dp)
- POJ 3140 树形DP
- POJ 3140 树形dp
- poj 3140(树形dp)
- POJ 3140(简单树形DP)
- poj 1655树形dp && poj 3107 &&poj 2378 && poj 3140
- 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
- NOIP 1969 积木大赛
- Ajax超时处理
- Java基础系列之日期时间处理
- 一些常见svn图标的含义
- Ruby语言
- poj 3140(树形dp)
- Shell学习笔记
- js如何实现点击一个按钮显示一个div,然后该div上有一个按钮,点击此按钮后隐藏,隐藏的同时显示另外一个div,而在次弹出的div也有一个按钮,点击后隐藏.
- 几张图看懂列式存储
- 一个搜索和展示热词和历史内容等筛选条件的类
- Spring学习笔记-2:JSP标准标签库
- [leetcode] 60. Permutation Sequence
- 采用Tagged Pointer的字符串
- 系统或进程监控工具monit