POJ 3140 Contestants Division 树型DP删边
来源:互联网 发布:开源电商源码 编辑:程序博客网 时间:2024/06/05 18:31
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
很水的树型DP。
树上删一条边,使两部分节点的权值和的差的绝对值最小。
删每条边后的差,就是总权值和减去子树权值和与子树权值和的差。
dfs一遍过
#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <bitset>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;const int maxn=100005,inf=0x3f3f3f3f;const ll llinf=0x3f3f3f3f3f3f3f3f; const ld pi=acos(-1.0L);int num;ll a[maxn],tot,ans;int head[maxn];bool visit[maxn];struct Edge {int from,to,pre;};Edge edge[maxn*5];void addedge(int from,int to) {edge[num]=(Edge){from,to,head[from]};head[from]=num++;edge[num]=(Edge){to,from,head[to]};head[to]=num++;}ll fuck(ll x) {if (x>0) return x; else return -x;}ll dfs(int now) {ll sum=a[now];visit[now]=1;for (int i=head[now];i!=-1;i=edge[i].pre) {int to=edge[i].to;if (!visit[to]) sum+=dfs(to);}ans=min(ans,fuck(tot-2*sum));return sum;}int main() {int n,m;scanf("%d%d",&n,&m);int cnt=0;while (n!=0||m!=0) {cnt++;int i,j,x,y;num=0;memset(head,-1,sizeof(head));tot=0;for (i=1;i<=n;i++) {scanf("%I64d",&a[i]);tot+=a[i];}for (i=1;i<=m;i++) { scanf("%d%d",&x,&y);addedge(x,y);}mem0(visit);ans=tot;dfs(1);printf("Case %d: %I64d\n",cnt,ans);scanf("%d%d",&n,&m);}return 0;}
- 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)
- 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)
- poj 3140 Contestants Division
- POJ--3140--Contestants Division
- Spring4从入门到精通---工厂方法创建bean
- 四、java运行环境之centos 如何开启端口
- 第二章 环境搭建
- IDEA GsonFormat插件 Json字符串自动生成实体类
- ldrc 报表文件
- POJ 3140 Contestants Division 树型DP删边
- spring事务管理
- dhcp 配置服务
- sql自动发邮件功能
- SQL中内连接和外连接
- 1105. Spiral Matrix (25)
- TCP
- Java Exception
- 监控-zabbix