【hdu6035】 Colorful Tree dfs序】的更多相关文章

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6035 题目大意:给你一棵树,树上每个节点都有一个颜色. 现在定义两点间的距离为两点最短路径上颜色集合大小,求该树上所有点对的距离之和.其中树上的节点个数$≤2*10^5$ 如果直接处理每一条路径上颜色集合大小,显然比较麻烦.我们不妨换一种思路. 我们用S_i表示经过颜色i的路径的数量,显然答案$=\sum S_i$. 考虑如何求S_i.我们先将所有颜色为i的节点全部找出来,按照dfs序排序. 显然…
/** 题目:hdu6035 Colorful Tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6035 题意:给定一棵树,每个节点有一个颜色值.定义每条路径的值为经过的节点的不同颜色数.求所有路径的值和. 思路:看题解后,才想出来的.树形dp. 求所有路径的值和 = 路径条数*总颜色数(n*(n-1)*colors/2)-sigma(每种颜色没有经过的路径条数) 主要是求每种颜色没有经过的路径条数. 画一棵树,我直接用颜色值表示节点编号. 2…
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根的编号永远是1.每个节点上最多结一个苹果.卡卡想要了解某一个子树上一共结了多少苹果. 现在的问题是不断会有新的苹果长出来,卡卡也随时可能摘掉一个苹果吃掉.你能帮助卡卡吗? 前缀技能 边表存储树 DFS时间戳 线段树 首先利用边表将树存储下来,然后DFS打上时间戳.打上时间戳之后,我们就知道书上节点对…
Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple t…
Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26762   Accepted: 7947 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been…
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/problem/C Description Iahub likes trees very much. Recently he discovered an interesting tree named propagating tree. The tree consists of n nodes numb…
D. Water Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343/problem/D Description Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a reservoir which can be either empty or…
出自——博客园-zhouzhendong ~去博客园看该题解~ 题目 POJ3321 Apple Tree 题意概括 有一颗01树,以结点1为树根,一开始所有的结点权值都是1,有两种操作: 1.改变其中一个结点的权值(0变1,1变0) 2.询问子树X的节点权值和. 输入描述 一组数据. 先是一个数n,表示有n个节点. 接下去n-1行,每行表示一条边. 然后一个数m,表示有m个操作. 然后m行,每行一个字母一个数x,如果字母是Q,则是询问:否则是修改. 输出描述 每一个询问一行,表示答案. 题解…
Split The Tree 时间限制: 1 Sec  内存限制: 128 MB提交: 46  解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 You are given a tree with n vertices, numbered from 1 to n. ith vertex has a value wiWe define the weight of a tree as the number of different vertex value in the…
D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Alyona has a tree with n vertices. The root of the tree is the vertex 1. In each vertex Alyona wrote an positive intege…