[swustoj 785] Divide Tree
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的更多相关文章
- [swustoj 856] Huge Tree
Huge Tree(0856) 问题描述 There are N trees in a forest. At first, each tree contains only one node as it ...
- 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 Binary Tree & Divide Conquer)-课堂笔记
前言 第一天的算法都还没有缓过来,直接就进入了第二天的算法学习.前一天一直在整理Binary Search的笔记,也没有提前预习一下,好在Binary Tree算是自己最熟的地方了吧(LeetCode ...
- [LeetCode] 系统刷题4_Binary Tree & Divide and Conquer
参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历. 此处说明Divi ...
- [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 ...
- leetcode Ch4-Binary Tree & BFS & Divide/Conquer
一. 1. Lowest Common Ancestor class Solution { public: TreeNode *lowestCommonAncestor(TreeNode *root, ...
- [P3806] Divide and Conquer on Tree
Link: P3806 传送门 Solution: 询问树上是否存在两点间的距离为$k$,共有$m$次询问($m\le 100,k\le 1e7$) 预处理出所有距离的可能性再$O(1)$出解的复杂度 ...
- 【Codeforces715C&716E】Digit Tree 数学 + 点分治
C. Digit Tree time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ...
- 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 ...
随机推荐
- 原生JS实现苹果菜单
今天分享下用原生JS实现苹果菜单效果,这个效果的重点有以下几点 图标中心点到鼠标的距离的算法 利用比例计算图标的宽度 代码地址:https://github.com/peng666/blogs/blo ...
- PAT-乙级-1048. 数字加密(20)
1048. 数字加密(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求实现一种数字加密方法.首先固 ...
- 【C# 反射泛型】
C# 反射泛型 摘自:http://www.itwis.com/html/net/c/20110411/10175.html C#泛型反射和普通反射的区别,泛型反射和普通反射的区别就是泛型参数的处理上 ...
- c# 在windows服务中 使用定时器
由于最近做自动执行的程序,开始做windows服务程序, 在windows服务中如何使用定时器的时候一直失效, 以前是直接拖入timer控件,但是不能直接运行,后来在网上找了一段程序,好使了. //开 ...
- hdu2013
http://acm.hdu.edu.cn/showproblem.php?pid=2013 #include<iostream> #include<stdio.h> #inc ...
- excel公式应用大全
excel公式应用大全 1.ABS函数 函数名称:ABS 主要功能:求出相应数字的绝对值. 使用格式:ABS(number) 参数说明:number代表需要求绝对值的数值或引用的单元格. 应用举例:如 ...
- lintcode:strStr 字符串查找
题目: 字符串查找 字符串查找(又称查找子字符串),是字符串操作中一个很有用的函数.你的任务是实现这个函数. 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source ...
- SaaS系列介绍之十五: SaaS知识重用
1 建立并积累自己的开发体系 遵行业界的规定又有自己的特色是我们所追求的目标.成功的软件公司都有丰富而可复用的代码组件,几行代码在单个系统里可能无足轻重,但一旦可在大量的系统中可重复使用那就是价值不菲 ...
- 【Linux常识篇(3)】文件及文件夹的ctime atime mtime的含义详解
首先可以使用stat 命令来查询文件的inode信息,其中包括ctime atime mtime [root@localhost ~]# stat sort2.txt File: 'sort2.txt ...
- 一个简单的将GUI程序的log信息输出到关联的Console窗口中(AllocConsole SetConsoleTitle WriteConsole 最后用ShowWindow(GetConsoleWindow)进行显示)
// .h 文件 #pragma once class CConsoleDump { public: explicit CConsoleDump(LPCTSTR lpszWindowTitle = N ...