题目给一棵树,边带有权值,求每一点到其他点路径上的最大权和. 树上任意两点的路径都可以看成是经过某棵子树根的路径,即路径权=两个点到根路径权的和,于是果断树分治. 对于每次分治的子树,计算其所有结点到根的距离:对于每个结点,找到另一个离根最远的且与该结点路径过根的结点,二者的距离和就是这个点在过这棵子树的根能到的最远距离. 现在问题就是怎么比较快地找到这另一个最远距离的点..两点路径过根,说明两点间不存在一点是另一点的祖先..我一开始还想用DFS序+线段树来着..想了想,想出了线性的算法: 记录…
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1257 跟hdu2196一样,两次dfs //#pragma comment(linker, "/STACK:102400000, 102400000") #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #inc…
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. The edges of the tree are weighted and undire…
1094 - Farthest Nodes in a Tree problem=1094" style="color:rgb(79,107,114)"> problem=1094&language=english&type=pdf" style="color:rgb(79,107,114)">PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Li…
http://acm.hust.edu.cn/vjudge/contest/121398#problem/H 不是特别理解,今天第一次碰到这种问题.给个链接看大神的解释吧 http://www.cnblogs.com/qq2424260747/p/4740347.html #include <cstdio> #include <cstring> #include <iostream> #include <cmath> #include<vector&g…
题目链接:http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. The edges of the tree are weighted and undirected. That means you have to find two nodes in…
http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. The edges of the tree are weighted and undirected. That means you have to find two nodes in the t…
题目链接,密码:hpu Description Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. The edges of the tree are weighted and undirected. That means you have to find two nodes in the tree whose distance is maximum a…
1094 - Farthest Nodes in a Tree   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. The edges of the tree are weighted and undi…
Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. The edges of the tree are weighted and undirected. That means you have to find two nodes in the tree whose distance is maximum amongst all nodes. Input…
poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出来.接着对于每一个子树再次运算.如果不用点分治的技巧,时间复杂度可能退化成\(O(n^2)\)(链).如果对于子树重新选根,找到树的重心,就一定可以保证时间复杂度在\(O(nlogn)\)内. 具体技巧是:首先选出树的重心,将重心视为根.接着计算出每个结点的深度,以此统计答案.由于子树中可能出现重复…
题目大概说给一棵有点权的树,输出字典序最小的点对,使这两点间路径上点权的乘积模1000003的结果为k. 树的点分治搞了.因为是点权过根的两条路径的LCA会被重复统计,而注意到1000003是质数,所以这个用乘法逆元搞一下就OK了.还有要注意“治”的各个实现,把时间复杂度“控制”在O(nlogn). WA了几次,WA在漏了点到子树根的路径,还有每次分治忘了清空数组. #include<cstdio> #include<cstring> #include<algorithm&g…
http://lightoj.com/volume_showproblem.php?problem=1094 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点,再从终点进行BFS,则第二次BFS找到的最长路即为树的直径: 原理: 设起点为u,第一次BFS找到的终点v一定是树的直径的一个端点 证明: 1) 如果u 是直径上的点,则v显然是直径的终点(因为如果v不是的话,则必定存在另一个点w使得u到w的距离更长,则于BFS找到了v矛盾)2) 如果u不是直径上的…
树上最远点对(树的直径) 做法1:树形dp 最长路一定是经过树上的某一个节点的. 因此: an1[i],an2[i]分别表示一个点向下的最长链和次长链,次长链不存在就设为0:这两者很容易求 an3[i]表示i为根的子树中的答案:an3[u]=max(max{an3[v]}(v是u的子节点),an1[u]+an2[u]) #include<cstdio> #include<cstring> #include<queue> #include<algorithm>…
Give a tree with n vertices,each edge has a length(positive integer less than 1001).Define dist(u,v)=The min distance between node u and v. Give an integer k,for every pair (u,v) of vertices is called valid if and only if dist(u,v) not exceed k. Writ…
  Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16172   Accepted: 5272 Description    Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist(u,v)=The min distance between node u and v.    …
题目大概是,给一棵树,统计距离为k的点对数. 不会DP啊..点分治的思路比较直观,啪啪啪敲完然后AC了.具体来说是这样的: 树上任何两点的路径都可以看成是一条过某棵子树根的路径,即任何一条路径都可以由一个子树到达根的一条或两条路径组成 就可以分治累加各个结点为根的子树的统计数目 对于各个子树可以这样统计:假设这个子树的根有a.b.c...若干个孩子,开一个数组cnt[i]记录有几个结点到根结点为i,依次处理a.b.c...结点及各自以下的结点,处理的时候根据当前的cnt数组统计数目,处理完后把新…
题目给一棵边带权的树,统计路径长度<=k的点对数. 楼教主男人八题之一,分治算法在树上的应用. 一开始看论文看不懂,以为重心和距离那些是一遍预处理得来的..感觉上不敢想每棵子树都求一遍重心和距离——那样时间复杂度怎么会只有O(nlogn)? 后来想通了,真的是对于每颗子树都把其所有结点单独提取出来,而且这么做就是O(nlogn)! 首先每次都选择重心进行分治,这样最多大概处理logn层,每一层都包含若干棵子树: 考虑每一层的每棵子树要提取的结点个数的和:第一层:n,第二层:n-1(第一层子树个数…
http://poj.org/problem?id=2255 #include<cstdio> #include <cstring> using namespace std; const int maxn = 27; char pre[maxn],in[maxn]; char past[maxn]; void tre(int ps,int pe,int is,int ie,int& ind) { int lnum = strchr(in,pre[ps]) - in - is…
Farthest Nodes in a Tree Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu Submit Status Description Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. The edges of the tree are weigh…
COT2 - Count on a tree II #tree You are given a tree with N nodes. The tree nodes are numbered from 1 to N. Each node has an integer weight. We will ask you to perform the following operation: u v : ask for how many different integers that represent…
链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Roman planted a tree consisting of n vertices. Each vertex contains a low…
Problem Description For a sequence S1, S2, ... , SN, and a pair of integers (i, j), if 1 <= i <= j <= N and Si < Si+1 < Si+2 < ... < Sj-1 < Sj , then the sequence Si, Si+1, ... , Sj is a CIS(Continuous Increasing Subsequence). The…
3637: Query on a tree VI Time Limit: 8 Sec  Memory Limit: 1024 MBSubmit: 206  Solved: 38[Submit][Status][Discuss] Description You are given a tree (an acyclic undirected connected graph) with n nodes. The tree nodes are numbered from 1 to n. Each nod…
P1501 [国家集训队]Tree II 看着维护吧2333333 操作和维护区间加.乘线段树挺像的 进行修改操作时不要忘记吧每个点的点权$v[i]$也处理掉 还有就是$51061^2=2607225721>2147483647$ 所以要开unsigned int #include<iostream> #include<cstdio> #include<cstring> #define rint register int #define di unsigned i…
BZOJ2589 Spoj 10707 Count on a tree II Solution 吐槽:这道题目简直...丧心病狂 如果没有强制在线不就是树上莫队入门题? 如果加了强制在线怎么做? 考虑分块(莫队与分块真是基友) 我们按照深度为\(\sqrt{n}\)的子树分块,那么这一棵树最多不超过\(\sqrt{n}\)个块. 维护每一个块的根节点到树上每一个节点的答案,暴力即可.然后用可持久化块状数组维护一下遍历时出现的最深的颜色的深度. 查询答案的做法: 在一个块内,直接暴力查. 不在一个…
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com/problems/symmetric-tree/description/ 101. Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). Fo…
[BZOJ2589][SPOJ10707]Count on a tree II 题面 bzoj 题解 这题如果不强制在线就是一个很\(sb\)的莫队了,但是它强制在线啊\(qaq\) 所以我们就用到了另一个东西:树分块 具体是怎么分块的呢:根据深度,从最深的叶子节点往上分,同一子树内的节点在一个块 比如说上面那张图, 有\(7\)个点,那么我们每隔\(2\)的深度就分一块 但是我们又要保证同一子树内的在一块,且要从最深的叶子节点一直往下 所以最后分块的结果:\((1,2)(7,6,3)(4,5)…
P1501 [国家集训队]Tree II 题目描述 一棵\(n\)个点的树,每个点的初始权值为\(1\).对于这棵树有\(q\)个操作,每个操作为以下四种操作之一: + u v c:将\(u\)到\(v\)的路径上的点的权值都加上自然数\(c\): - u1 v1 u2 v2:将树中原有的边\((u_1,v_1)\)删除,加入一条新边\((u_2,v_2)\),保证操作完之后仍然是一棵树: * u v c:将\(u\)到\(v\)的路径上的点的权值都乘上自然数\(c\): / u v:询问\(u…
COT2 - Count on a tree II http://www.spoj.com/problems/COT2/ #tree You are given a tree with N nodes. The tree nodes are numbered from 1 to N. Each node has an integer weight. We will ask you to perform the following operation: u v : ask for how many…