题目描述:Divide Tree
 

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

样例输出

2

DFS水题

备注:另一个结点的权值=父节点权值-当前结点的权值。

//// Divide Tree.cpp : 定义控制台应用程序的入口点。
////
//
//#include "stdafx.h"
//
//#include <stdio.h>
//#include <string.h>
//#include <cmath>
//#include <iostream>
//using namespace std;
//
//const int maxn = 10005;
//const int INF = 0x3f3f3f3f;
//
//int t, n, graph[maxn][maxn],weight[maxn],vis[maxn];
//int ans,sum[maxn];
//
//
////计算子树权值
////思路:沿着DFS路线就可以确定树的权值
//void sum_weight(int i)
//{
// vis[i] = 1;
//
// sum[i] = weight[i];
//
// for (int j = 1; j <= n; j++)
// {
// if (!vis[j] && graph[i][j])//沿着边搜索没有经过的顶点
// {
// sum_weight(j);
// sum[i] += sum[j];
// }
// }
//}
//
//
//
//void DFS(int i)
//{
// vis[i] = 1;
//
// for (int j = 1; j <= n; j++)
// {
// if (graph[i][j] && !vis[j])//也是沿着边搜索
// {
// //思路:
// //1.一个顶点的权值:s[j]
// //2.另一个顶点的权值:s[i] - s[j]
// int sub_diff = (sum[i] - sum[j]) - sum[j];
// ans = ans < abs(sub_diff) ? ans : abs(sub_diff);
// DFS(j);
// }
// }
//}
//
//int main()
//{
// scanf("%d",&t);
// while (t--)
// {
// memset(graph, 0, sizeof(graph));
// memset(vis, 0, sizeof(vis));
// scanf("%d", &n);
// for (int i = 1; i <= n; i++)
// {
// scanf("%d", &weight[i]);
// }
// for (int i = 1; i <= n-1; i++)
// {
// int v1, v2;
// scanf("%d %d", &v1, &v2);
// graph[v1][v2] = 1;
// graph[v2][v1] = 1;//无向图!!!!
// }
//
// sum_weight(1);
//
// ans = INF;
// memset(vis, 0, sizeof(vis));
// DFS(1);
//
// printf("%d\n",ans);
//
// }
// return 0;
//}
// #include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <cmath>
#include <vector>
#include <string.h>
using namespace std; const int M = ;
const int INF = 0x3f3f3f3f;//c、c++的最大数是十六进制的0x int t, n,ans,weight[M],vis[M],sum[M];
vector<int> G[M]; void sum_weight(int i)
{
vis[i] = ; sum[i] = weight[i]; for (int j = ; j < G[i].size(); j++)
{
int next = G[i][j]; if (!vis[next])//沿着边搜索没有经过的顶点
{
sum_weight(next);
sum[i] += sum[next];
}
}
} void DFS(int i)
{
vis[i] = ; for (int j = ; j < G[i].size(); j++)//直接遍历边比遍历顶点循环次数少,可以达到减枝的目的。
{
int next = G[i][j];
if (!vis[next])//也是沿着边搜索
{
//思路:
//1.一个顶点的权值:s[j]
//2.另一个顶点的权值:s[i] - s[j]
int sub_diff = (sum[] - sum[next]) - sum[next];//为什么这里是sum[1]-sum[j],不是sum[i]-sum[j]????
ans = ans < abs(sub_diff) ? ans : abs(sub_diff);
DFS(next);
}
}
} int main()
{ scanf("%d",&t);
while (t--)
{
memset(vis, , sizeof(vis));
scanf("%d", &n);
for (int i = ; i <= n; i++)
{
scanf("%d", &weight[i]);
G[i].clear();
}
for (int i = ; i <= n - ; i++)
{
int v1, v2;
scanf("%d %d", &v1, &v2);
G[v1].push_back(v2);
G[v2].push_back(v1);
} sum_weight(); ans = INF;
memset(vis, , sizeof(vis));
DFS(); printf("%d\n", ans); } return ;
}

ACM-Divide Tree的更多相关文章

  1. [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 wi ...

  2. [ACM]Link-Cut Tree实现动态树初探

    动态树问题是指的一类问题,而不是具体指的某一种数据结构.它主要维护一个包含若干有根树的森林,实现对森林的修改和查询等. 实现动态树的数据结构据说主要有4种,Link-Cut Tree是其中的一种.Li ...

  3. HDOJ-3065(AC自动机+每个模板串的出现次数)

    病毒侵袭持续中 HDOJ-3065 第一个需要注意的是树节点的个数也就是tree的第一维需要的空间是多少:模板串的个数*最长模板串的长度 一开始我的答案总时WA,原因是我的方法一开始不是这样做的,我是 ...

  4. HDOJ-2896(AC自动机+文本串中出现了哪几个模板串)

    病毒侵袭 HDOJ-2896 主要使用AC自动机解决,其次在query函数中改变一下,用来记录每个模板串出现的次数,还有insert函数中记录模板串的编号 需要注意最好使用结构体,而且不能一次性使用m ...

  5. HDU ACM 1325 / POJ 1308 Is It A Tree?

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

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

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

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

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

  8. [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 ...

  9. Minimal Steiner Tree ACM

    上图论课的时候无意之间看到了这个,然后花了几天的时间学习了下,接下来做一个总结. 一般斯坦纳树问题是指(来自百度百科): 斯坦纳树问题是组合优化问题,与最小生成树相似,是最短网络的一种.最小生成树是在 ...

随机推荐

  1. Metasploit学习笔记——社会工程学

    1.社会工程学攻击案例——伪装木马 Linux命令终端输入命令msfvenom -l payloads用来列出攻击载荷,grep命令用来查询所需要的攻击载荷,条件是windows系统.要有回连至监听主 ...

  2. Java中的输入"scanf",Scanner用法

    1.输入程序 1)Scnner的初始用法: 1 import java.util.Scanner; 2 public class Textone { 3 public static void main ...

  3. IOS pin约束问题 存在间隙

    今天在为自己的view添加约束 对比以前添加的约束时,发现有有两层淡红色线框一条实线和一条虚线,而以前一个demo中添加的则只有一个蓝色实线框. 今天添加的约束如图1所示: 图1 而以前添加约束如图2 ...

  4. python中单下划线的变量

    1._xxx 不能用于’from module import *’ 以单下划线开头的表示的是protected类型的变量.即保护类型只能允许其本身与子类进行访问.2.__xxx 双下划线的表示的是私有 ...

  5. c# Thread、ThreadPool、Task的区别

    Thread与ThreadPoll 前台线程:主程序必须等待线程执行完毕后才可退出程序.Thread默认为前台线程,也可以设置为后台线程 后台线程:主程序执行完毕后就退出,不管线程是否执行完毕.Thr ...

  6. 对S7通信的连接的理解以及对比CAN通信协议来理解PLC通讯

    对S7通信的连接的理解以及对比CAN通信协议来理解PLC通讯. 对功能块 SFB12 和 SFB13 的R_ID参数的理解 ? 对于同一个数据包.发送方与接收方的R_ID应该相同. 用下图解释 双向连 ...

  7. Nginx配置的一些说明(添加https证书)

    server { listen 443 ssl; #监听https 443端口 server_name www.XXXX.com; client_max_body_size 260M; #这下面的就是 ...

  8. 弱点扫描-openvas初始化

    OPENVAS: NESSUS项目分支:商业版的扫描器 管理目标系统的漏洞 免费开源 Kali 默认安装但是未配置个启动 安装 创建证书 同步弱点数据库 创建客户端证书 重建数据库 备份数据库 启动服 ...

  9. Adobe Illustrator CC 2017安装方法

    1.下载软件地址 2017破解版32位64位中文版下载[百度网盘资源地址]:https://pan.baidu.com/s/13BsU8CfsLB6OXr7SkRFzCg 注意:使用之前请关闭杀毒软件 ...

  10. 使用nginx做反向代理来访问tomcat服务器

    本次记录的是使用nginx来做一个反向代理来访问tomcat服务器.简单的来说就是使用nginx做为一个中间件,来分发客户端的请求,将这些请求分发到对应的合适的服务器上来完成请求及响应. 第一步:安装 ...