题意:

  有n个城市,构成一棵树,每个城市有v个人,要求断开树上的一条边,使得两个连通分量中的人数之差最小。问差的绝对值。(注意本题的M是没有用的,因为所给的必定是一棵树,边数M必定是n-1)

思路:

  考虑当前节点t,当断开t与父亲的边时,“子树t中的人数”与“剩下的人数”之差的绝对值若最小,则为答案。

 //#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <deque>
#include <algorithm>
#include <vector>
#include <iostream>
#define pii pair<int,int>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define abs(x) ((x)<0?-(x):(x))
#define INF 2147483647
#define LL long long
using namespace std;
const double PI = acos(-1.0);
const int N=;
int edge_cnt, head[N], w[N];
LL ans, sum;
struct node
{
int from, to, next;
node(){};
node(int from,int to,int next):from(from),to(to),next(next){};
}edge[N*]; void add_node(int from,int to)
{
edge[edge_cnt]=node(from,to,head[from]);
head[from]=edge_cnt++;
} LL DFS(int t,int far) //枚举删除t头上的边
{
node e;
LL cnt=;
for(int i=head[t]; i!=-; i=e.next)
{
e=edge[i];
if(e.to==far) continue;
cnt+=DFS(e.to, t);
}
cnt+=w[t]; //本子树的人数
ans=min(ans, abs( *cnt-sum ));
return cnt;
} int main()
{
//freopen("input.txt", "r", stdin);
int a, b, n, m, Case=;
while(scanf("%d%d",&n,&m), n+m)
{
sum=edge_cnt=;
memset(head,-,sizeof(head)); for(int i=; i<=n; i++)
{
scanf("%d",&w[i]);
sum+=w[i];
}
ans=sum;
for(int i=; i<m; i++)
{
scanf("%d%d",&a,&b);
add_node(a,b);
add_node(b,a);
}
DFS(,-);
printf("Case %d: %lld\n", ++Case, ans);
}
return ;
}

AC代码

POJ 3140 Contestants Division (树形DP,简单)的更多相关文章

  1. POJ 3140 Contestants Division 树形DP

    Contestants Division   Description In the new ACM-ICPC Regional Contest, a special monitoring and su ...

  2. POJ 3140 Contestants Division (树dp)

    题目链接:http://poj.org/problem?id=3140 题意: 给你一棵树,问你删去一条边,形成的两棵子树的节点权值之差最小是多少. 思路: dfs #include <iost ...

  3. POJ 3140.Contestants Division 基础树形dp

    Contestants Division Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10704   Accepted:  ...

  4. poj 3140 Contestants Division(树形dp? dfs计数+枚举)

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

  5. POJ 3140 Contestants Division 【树形DP】

    <题目链接> 题目大意:给你一棵树,让你找一条边,使得该边的两个端点所对应的两颗子树权值和相差最小,求最小的权值差. 解题分析: 比较基础的树形DP. #include <cstdi ...

  6. POJ 3140 Contestants Division

    题目链接 题意很扯,就是给一棵树,每个结点有个值,然后把图劈成两半,差值最小,反正各种扯. 2B错误,导致WA了多次,无向图,建图搞成了有向了.... #include <cstdio> ...

  7. poj 3140 Contestants Division [DFS]

    题意:一棵树每个结点上都有值,现删掉一条边,使得到的两棵树上的数值和差值最小. 思路:这个题我直接dfs做的,不知道树状dp是什么思路..一开始看到数据规模有些后怕,后来想到long long 可以达 ...

  8. [poj3140]Contestants Division树形dp

    题意:切掉树上的某条边,使分开的两棵树上各点的权值和差值最小. 与hdu2196不同的是,此题是点权,其他无太大差别,注意数据范围. 先求出每个节点的子树权值和,然后自底向上dp即可.取$\min ( ...

  9. POJ 2378 Tree Cutting 3140 Contestants Division (简单树形dp)

    POJ 2378 Tree Cutting:题意 求删除哪些单点后产生的森林中的每一棵树的大小都小于等于原树大小的一半 #include<cstdio> #include<cstri ...

随机推荐

  1. dubbo 使用 filter 报错解决

    dubbo可以用filter实现类似tomcat filter过滤器. 实现1.接口请求时间监控. 2.打印输入输出日志(输出日志有应用自己决定) 配置时出现报错. No such extension ...

  2. Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力

    Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...

  3. 理解复杂的const和typedef和指针的关系

    // container.cpp : 定义控制台应用程序的入口点. //   #include "stdafx.h" #include<iostream> #inclu ...

  4. 15.oauth2 + oidc 实现 server部分

    OAuth主要做授权. OpenIdConnect简历在OAuth2.0基础之上的,相结合 客户端.授权中心.Resource Owner用户本身(资源的拥有者).Resource Server 通过 ...

  5. html和xml的区别

    一.HTML HTML(HyperTextMark-upLanguage)即超文本标记语言,是WWW的描述语言. 二.XML XML即ExtentsibleMarkup Language(可扩展标记语 ...

  6. bugfree-解决方案的意思

    BugFree的7种解决方案各自的含义: By Design - 就是这么设计的,无效的Bug Duplicate - 这个问题别人已经发现了,重复的Bug External - 是个外部因素(比如浏 ...

  7. (水题)HDU - 1077 - Catching Fish - 计算几何

    http://acm.hdu.edu.cn/showproblem.php?pid=1077 很明显这样的圆,必定有两个点在边界上.n平方枚举圆,再n立方暴力判断.由于没有给T,所以不知道行不行.

  8. C【C#公共帮助类】分页逻辑处理类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Comm ...

  9. [題解](單調隊列/水)luogu_P3088擠奶牛

    d長度內區間最大值,單調隊列維護即可 由於需要滿足左右同時有2倍高度的牛才能更新答案,所以正反跑兩次 #include<bits/stdc++.h> using namespace std ...

  10. tracert查网络问题

    tracert命令的格式为:tracert [-d] [-h maximum_hops] [-j host-list] [-w timeout] [-R] [-S srcaddr] [-4] [-6] ...