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. openmeetings 视频会议系统介绍

    在功能上,视频会议具有如下特点(这一部分转自:http://www.kissthink.com/archive/5150.html): 1.该方基于P2P技术,服务器压力小.流畅.用户之间可以互相获取 ...

  2. clion 帮助文档 EN

    下载时间 2015年10月 下载地址:http://pan.baidu.com/s/1E4fgE 备用地址:链接:http://pan.baidu.com/s/1bn6u5Wj 密码:icn4

  3. 2391: Cirno的忧郁 - BZOJ

    Description Cirno闲着无事的时候喜欢冰冻青蛙.Cirno每次从雾之湖中固定的n个结点中选出一些点构成一个简单多边形,Cirno运用自己的能力能将此多边形内所有青蛙冰冻.雾之湖生活着m只 ...

  4. 定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。

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

  5. 首次push本地代码到github上出现的问题及解决方案

    刚创建的github版本库,在push代码时出错: $ git push -u origin masterTo git@github.com:******/Demo.git ! [rejected] ...

  6. codeforces 397B

    #include <cstdio> #include <cstdlib> #include <cmath> #include <map> #includ ...

  7. struts2+hibernate+spring+jquery返回json List列表

    1.引入包:struts2-json-plugin-2.1.8.1.jar json-lib-2.1.jar commons-collections-3.2.1.jar commons-beanuti ...

  8. unity3d android互调

    unityPlayer = new AndroidJavaClass("com.xxx.xxx.MainActivity"); curActivity = unityPlayer. ...

  9. Android 问题流水总结

    先来一篇环境搭建的博客 这些都是大同小异. http://blog.csdn.net/yzhj2005/article/details/6980676 http://blog.csdn.net/wan ...

  10. unity调用MMBilling_2.4.2 Android SDK.

    原地址:http://www.cnblogs.com/ayanmw/p/3736284.html 项目要使用android 的移动支付SDK 应用内付费[http://dev.10086.cn/wik ...