Divide Tree(0785)

问题描述

As we all know that we can consider a tree as a graph. Now give you a tree with nodes having its weight. We define the weight of a tree is the sum of the weight of all nodes on it. You know we can divide the tree into two subtrees by any edge of the tree. And your task is to tell me the minimum difference between the two subtrees’ weight.

输入

The first line, an integer T (T <= 30), representing T test cases blew.

For each test case, the first line contains one integer N (2 <= N <= 10000), indicating the number of tree’s nodes. Then follow N integers in one line, indicating the weight of nodes from 1 to N.

For next N-1 lines, each line contains two integers Vi and Vj (1 <= Vi, Vj <= N), indicating one edge of the tree.

输出

For each test case, output the minimum weight difference. We assume that the result will not exceed 2^20.

样例输入

1
5
6 3 9 3 1
2 3
3 1
4 1
1 5

样例输出

20

简单题

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
using namespace std;
#define min(a,b) ((a)<(b)?(a):(b))
#define INF 0x3f3f3f3f
#define pb push_back
#define N 10010 int n;
int ans;
int val[N];
int size[N];
vector<int> edge[N]; void init()
{
ans=INF;
for(int i=;i<=n;i++){
edge[i].clear();
size[i]=;
}
}
void dfs1(int u,int pre)
{
size[u]=val[u];
for(int i=;i<edge[u].size();i++){
int v=edge[u][i];
if(v!=pre){
dfs1(v,u);
size[u]+=size[v];
}
}
}
void dfs2(int u,int pre)
{
for(int i=;i<edge[u].size();i++){
int v=edge[u][i];
if(v!=pre){
ans=min(ans,abs((size[]-size[v])-size[v]));
dfs2(v,u);
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
init();
for(int i=;i<=n;i++){
scanf("%d",&val[i]);
}
for(int i=;i<n;i++){
int a,b;
scanf("%d%d",&a,&b);
edge[a].pb(b);
edge[b].pb(a);
}
dfs1(,);
dfs2(,);
printf("%d\n",ans);
}
return ;
}

[swustoj 785] Divide Tree的更多相关文章

  1. [swustoj 856] Huge Tree

    Huge Tree(0856) 问题描述 There are N trees in a forest. At first, each tree contains only one node as it ...

  2. ACM-Divide Tree

    题目描述:Divide Tree   As we all know that we can consider a tree as a graph. Now give you a tree with n ...

  3. 九章算法系列(#3 Binary Tree & Divide Conquer)-课堂笔记

    前言 第一天的算法都还没有缓过来,直接就进入了第二天的算法学习.前一天一直在整理Binary Search的笔记,也没有提前预习一下,好在Binary Tree算是自己最熟的地方了吧(LeetCode ...

  4. [LeetCode] 系统刷题4_Binary Tree & Divide and Conquer

    参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历. 此处说明Divi ...

  5. [LeetCode] 124. Binary Tree Maximum Path Sum_ Hard tag: DFS recursive, Divide and conquer

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

  6. leetcode Ch4-Binary Tree & BFS & Divide/Conquer

    一. 1. Lowest Common Ancestor class Solution { public: TreeNode *lowestCommonAncestor(TreeNode *root, ...

  7. [P3806] Divide and Conquer on Tree

    Link: P3806 传送门 Solution: 询问树上是否存在两点间的距离为$k$,共有$m$次询问($m\le 100,k\le 1e7$) 预处理出所有距离的可能性再$O(1)$出解的复杂度 ...

  8. 【Codeforces715C&716E】Digit Tree 数学 + 点分治

    C. Digit Tree time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ...

  9. leetcode 236. Lowest Common Ancestor of a Binary Tree

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

随机推荐

  1. C编程实现2的1000次方(使程序中的n=1000即可)

    #include<stdio.h> #include<malloc.h> void double_(int n) { ,j,s,jw=; p=(int *)malloc(siz ...

  2. Kafka的消息格式

    Commit Log Kafka储存消息的文件被它叫做log,按照Kafka文档的说法是: Each partition is an ordered, immutable sequence of me ...

  3. solr4.5部署

    一.服务器部署 1.solr自带jetty服务器上部署 cd到solr-4.5.0\example目录下,运行java -jar start.jar即可运行jetty服务器.访问http://loca ...

  4. Download Manager

    从Android 2.3(API level 9)开始Android用系统服务(Service)的方式提供了Download Manager来优化处理长时间的下载操作.Download Manager ...

  5. Android 父类super.onDestroy();的有关问题

    super.onDestroy(); 的问题. 注意:没有显式地在自己的方法中调用父类Activity的onDestroy是会报错的.我的问题很简单,在我覆盖的onDestroy(),方法中需要调用父 ...

  6. [杂题]HDOJ5515 Game of Flying Circus

    嗯...这是一道水题... 鉴于还没人写这题的题解, 那我就来写一发. 题意:有个边长为300米的正方形 嗯  这样标号 有两个人A和S,开始的时候A.S都在1(左下角)那个位置. 两个人都要按照2. ...

  7. 修改linux文件/文件夹权限

    事情缘起:在VirtualBox虚拟机Ubuntu 12.04里通过共享文件夹从物理机拷贝jdk,拷贝过来之后不能正常使用.用javac -version命令不能查看java版本信息,sudo可以.原 ...

  8. 【mongoDB运维篇①】用户管理

    3.0版本以前 在mongodb3.0版本以前中,有一个admin数据库, 牵涉到服务器配置层面的操作,需要先切换到admin数据库.即 use admin , 相当于进入超级用户管理模式,mongo ...

  9. PCB走线角度选择 — PCB Layout 跳坑指南

    现在但凡打开SoC原厂的PCB Layout Guide,都会提及到高速信号的走线的拐角角度问题,都会说高速信号不要以直角走线,要以45度角走线,并且会说走圆弧会比45度拐角更好.狮屎是不是这样?PC ...

  10. java:接口实例

    接口:打印机接口 interface Printer { public void read(); } 函数一:佳能打印机 class CanPrinter implements Printer { p ...