[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 ...
随机推荐
- 编写一个小程序,从标准输入读入一系列string对象,寻找连续重复出现的单词。程序应该找出满足一下条件的单词:该单词的后面紧接着再次出现自己本身。跟踪重复次数最多的单词及其重复次数,输出.
// test13.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- SQL Server 之 解锁
下图,制作了一个可以维持1分钟的表锁: 下图,可以查询出被锁的表,其中 spid 是锁定表的进程ID(也是 session_id): 可以通过 select connect_time from sys ...
- plsql 使用技巧
问题1: 每次打开plsql 布局都被恢复还原了. 你可以调整好一个布局,然后window ->Save Lay out 即可
- 从String类看写C++ class需要注意的地方
#include <iostream> #include <string.h> using namespace std; class String { char* m_data ...
- struts2之请求参数接收
struts2之请求参数接收 1. 采用基本类型接受请求参数(get/post)在Action类中定义与请求参数同名的属性,struts2便能自动接收请求参数并赋予给同名的属性.请求路径:http:/ ...
- uva 10131
DP 先对大象体重排序 然后寻找智力的最长升序子列 输出路径.... #include <iostream> #include <cstring> #include &l ...
- Unity3dBug - OnEnable
最近 项目 因为 使用 active 代替 instantiate机制,很多时候 OnEnable 代理 OnStart. 然后发现一个 奇怪的 问题 void Awake() { Debug.Log ...
- 【设计模式六大原则3】依赖倒置原则(Dependence Inversion Principle)
定义:高层模块不应该依赖低层模块,二者都应该依赖其抽象:抽象不应该依赖细节:细节应该依赖抽象. 问题由来:类A直接依赖类B,假如要将类A改为依赖类C,则必须通过修改类A的代码来达成.这种场景下,类 ...
- spark在eclipse上配置
环境:spark1.4.0,hadoop2.6.0 1.安装好jdk 2.在spark的conf目录下找到spark-env.sh.template,打开,在后面加上 export SCALA_HOM ...
- ***CI分页:为CodeIgniter写的分页类
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...