这道题细节真的非常多

首先能够想到a和b的最优策略一定是沿着a和b在树上的链走,走到某个点停止,然后再依次占据和这个点邻接的边

所以,解决这道题的过程例如以下:

预处理阶段:

step 1:取随意一个点为根节点。找出父子关系而且对这个树进行dp。求出从某个节点出发往下所包括的全部边的权值总和  复杂度O(n)

step 2:从tree dp 的结果中计算对于某个节点。从某条边出发所包括的边的综合。而且对其从大到小进行排序 复杂度O(n*logn)

step 3:dfs求出这颗树的欧拉回路,以及每一个点的dfn,而且按欧拉回路的顺序计算每一个节点的深度 复杂度O(2*n)

step 4:利用sparse table算法初始化step 3中的深度序列 复杂度 O(n*logn)

step 5:计算出从某个节点往上走2的n次方步所到达的节点  复杂度O(n*logn)

查询阶段:

关键是找到两点的 LCA 以及相遇点,而且找到一条或两条所经过且和相遇点邻接的边

分几种情况讨论

1. 两个点在一起

2.两个点之间的距离为1

3.dep[a] == dep[b]

4.dep[a] > dep[b] + 1

5.dep[a] < dep[b]

6.dep[a] == dep[b]+1

ps:少考虑第六种情况wa了一个下午

#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
#include<ctime>
using namespace std;
#ifdef _WIN32
typedef __int64 i64;
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
typedef long long i64;
#define out64 "%lld\n"
#define in64 "%lld"
#endif
/************ for topcoder by zz1215 *******************/
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define FOR(i,a,b) for( int i = (a) ; i <= (b) ; i ++)
#define FF(i,a) for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a,b) for( int i = (a) ; i >= (b) ; i --)
#define S64(a) scanf(in64,&a)
#define SS(a) scanf("%d",&a)
#define LL(a) ((a)<<1)
#define RR(a) (((a)<<1)+1)
#define pb push_back
#define pf push_front
#define X first
#define Y second
#define CL(Q) while(!Q.empty())Q.pop()
#define MM(name,what) memset(name,what,sizeof(name))
#define MC(a,b) memcpy(a,b,sizeof(b))
#define MAX(a,b) ((a)>(b)? (a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout) const int inf = 0x3f3f3f3f;
const long long inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-9;
const double pi = acos(-1.0);
const int maxn = 100111;
const int maxlevel = 21; struct Node
{
int now;
int to;
int c;
int tot;
int ss;
const bool operator < (const Node& cmp) const {
return tot > cmp.tot;
}
}; int all;
int n, m;
vector<Node>g[maxn];
int t[maxn];
int dep[maxn]; int df;
int dfn[maxn];
int dfv[maxn * 2];
int st[maxn * 2][maxlevel];
int up[maxn][maxlevel]; int dp[maxn]; //down sum
int xtof[maxn];
int ftox[maxn]; int vis[maxn]; int lg2[maxn*2]; void dfs(int now)
{
vis[now] = true;
int to;
for (int i = 0; i < (int)g[now].size(); i++) {
to = g[now][i].to;
if (!vis[to]) {
t[to] = now;
dfs(to);
}
}
} int treedp(int now)
{
int to,id;
dp[now] = 0;
for (int i = 0; i < (int)g[now].size(); i++) {
to = g[now][i].to;
if (to != t[now]) {
int temp = treedp(to) + g[now][i].c;
g[now][i].tot = temp;
dp[now] += temp;
}
else {
id = i;
}
}
if (t[now] != -1) {
g[now][id].tot = all - dp[now];
}
return dp[now];
} void euler_circuit(int now ,int step)
{
dep[now] = step;
dfn[now] = df;
dfv[df++] = now;
int to;
for (int i = 0; i < (int)g[now].size(); i++) {
to = g[now][i].to;
if (to != t[now]) {
euler_circuit(to,step+1);
dfv[df++] = now;
}
}
} void get_up_node()
{
for (int i = 1; i <= n; i++) {
up[i][0] = t[i];
}
int to;
for (int step = 1; step < maxlevel; step++) {
for (int now = 1; now <= n; now++) {
to = up[now][step - 1];
if (to == -1) {
up[now][step] = -1;
}
else {
up[now][step] = up[to][step - 1];
}
}
}
} void sparse_table()
{
for (int i = 1; i < df; i++){
st[i][0] = dep[dfv[i]];
} int to;
for (int step = 1; step <= lg2[n] + 1; step++){
for (int now = 1; now < df; now++) {
to = now + (1 << (step - 1));
if (to < df){
st[now][step] = min(st[now][step - 1], st[to][step - 1]);
}
else{
st[now][step] = st[now][step - 1];
}
}
}
} void relation()
{
int to;
for (int now = 1; now <= n; now++){
for (int i = 0; i < (int)g[now].size(); i++){
to = g[now][i].to;
if (to == t[now]){
xtof[now] = i;
}
else{
ftox[to] = i;
}
}
}
} int rmq(int l,int r)
{
return min(st[l][lg2[r - l]], st[r - (1 << lg2[r - l])][lg2[r - l]] );
} int calculate(int x,bool first,int id1,int id2=-1)
{
if (id2 != -1){
if (id1 > id2){
swap(id1, id2);
}
}
int sum = g[x][0].ss;
sum -= g[x][id1].tot;
if (id2 != -1){
sum -= g[x][id2].tot;
}
int size = (int)g[x].size() - 1;
if (size >= 1){
sum += g[x][1].ss;
}
int ans = g[x][0].ss;
if (id1 % 2 ){
if (id1 + 1 <= size){
ans -= g[x][id1 + 1].ss;
if (id1 + 2 <= size){
ans += g[x][id1 + 2].ss;
}
}
if (id2 != -1){
if (id2 % 2){
ans -= g[x][id2].ss;
if (id2 + 1 <= size){
ans += g[x][id2 + 1].ss;
}
}
else{
if (id2 + 1 <= size){
ans -= g[x][id2 + 1].ss;
if (id2 + 2 <= size){
ans += g[x][id2 + 2].ss;
}
}
}
}
}
else{
ans -= g[x][id1].ss;
if (id1 + 1 <= size){
ans += g[x][id1 + 1].ss;
}
if (id2 != -1){
if (id2 % 2){
ans -= g[x][id2].ss;
if (id2 + 1 <= size){
ans += g[x][id2 + 1].ss;
}
}
else{
if (id2 + 1 <= size){
ans -= g[x][id2 + 1].ss;
if (id2 + 2 <= size){
ans += g[x][id2 + 2].ss;
}
}
}
}
}
if (first) return ans;
else return sum - ans;
} int go_up(int now, int x)
{
int step = 0;
while (x) {
if (x & 1) {
now = up[now][step];
}
step++;
x >>= 1;
}
return now;
} int find(int a,int b)
{
int l = dfn[a];
int r = dfn[b];
if (l == r){
return g[a][0].ss;
}
if (l > r){
swap(l, r);
}
int lca = rmq(l, r + 1); //dep
if (dep[a] - lca + dep[b] - lca == 1){
if (dep[a] == lca){
return g[b][xtof[b]].tot + calculate(b, false, xtof[b]);
}
else if (dep[b] == lca){
return g[b][ftox[a]].tot + calculate(b, false, ftox[a]);
}
}
else if (dep[a] > dep[b]+1){
int temp = dep[a] - dep[b];
int mid = lca + temp / 2;
int child = go_up(a, dep[a] - mid - 1);
if (temp % 2){
return g[t[child]][ftox[child]].tot + calculate(t[child], false, ftox[child], xtof[t[child]]);
}
else{
return g[t[child]][ftox[child]].tot + calculate(t[child], true, ftox[child], xtof[t[child]]);
}
}
else if (dep[a] == dep[b] + 1) {
int ca = go_up(a, dep[a] - lca - 1);
int cb = go_up(b, dep[b] - lca - 1);
int meet = t[ca];
return g[meet][ftox[ca]].tot + calculate(meet, false, ftox[ca], ftox[cb]);
}
else if (dep[a] < dep[b]){
int temp = dep[b] - dep[a];
int mid = lca + (temp + 1)/ 2;
int child = go_up(b, dep[b] - mid - 1);
if (temp % 2){
return g[t[child]][xtof[t[child]]].tot + calculate(t[child], false, xtof[t[child]], ftox[child]);
}
else{
return g[t[child]][xtof[t[child]]].tot + calculate(t[child], true, xtof[t[child]], ftox[child]);
}
}
else if(dep[a] == dep[b]) {
int ca = go_up(a, dep[a] - lca - 1);
int cb = go_up(b, dep[b] - lca - 1);
int meet = t[ca];
return g[meet][ftox[ca]].tot + calculate(meet, true, ftox[ca], ftox[cb]);
}
assert(false);
} void start()
{
for (int i = 1; i <= n; i++) {
vis[i] = false;
}
t[0] = t[1] = -1;
dfs(1);
treedp(1); for (int now = 1; now <= n; now++) {
sort(g[now].begin(), g[now].end());
for (int i =(int) g[now].size() - 1; i >= 0; i--) {
g[now][i].ss = g[now][i].tot;
if (i + 3 <= (int)g[now].size()) {
g[now][i].ss += g[now][i + 2].ss;
}
}
}
df = 1;
euler_circuit(1, 0);
get_up_node();
sparse_table();
relation();
} int main()
{
for (int i = 0; i < maxlevel; i++){
if ( (1<<i) < maxn*2)
lg2[1 << i] = i;
}
for (int i = 3; i < maxn*2; i++) {
if (!lg2[i]){
lg2[i] = lg2[i - 1];
}
} int T;
cin >> T;
while (T--) {
all = 0;
cin >> n >> m;
for (int i = 0; i <= n; i++){
g[i].clear();
}
Node node;
for (int i = 1; i <= n - 1; i++) {
//cin >> node.now >> node.to >> node.c;
SS(node.now);
SS(node.to);
SS(node.c);
g[node.now].push_back(node);
swap(node.now, node.to);
g[node.now].push_back(node);
all += node.c;
}
start();
int a, b;
for (int i = 1; i <= m; i++){
//cin >> a >> b;
SS(a); SS(b);
//cout << find(a, b) << endl;
printf("%d\n", find(a, b));
}
}
return 0;
}

hdu 4603 Color the Tree的更多相关文章

  1. hdu 6241 Color a Tree 2017 CCPC 哈理工站 L

    Bob intends to color the nodes of a tree with a pen. The tree consists of NN nodes. These nodes are ...

  2. HDU 1055 - Color a Tree

    一棵树,结点树为n,根结点为r.每个结点都有一个权值ci,开始时间为0,每染色一个结点需要耗时1,每个结点的染色代价为ci*ti(ti为当前的时间),每个结点只有在父结点已经被染色的条件下才能被染色. ...

  3. Color a Tree HDU - 6241

    /* 十分巧妙的二分 题意选最少的点涂色 使得满足输入信息: 1 x的子树涂色数不少于y 2 x的子树外面涂色数不少于y 我们若是把2转化到子树内最多涂色多少 就可以维护这个最小和最大 如果我们二分出 ...

  4. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  5. POJ 2054 Color a Tree

    贪心....                    Color a Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions:  ...

  6. hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. Color a Tree[HDU1055]

    Color a Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  9. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

随机推荐

  1. Excel数据迁移到SQL Server遇到的若干问题

    系统环境为:Windows Server 2008 r2 SQL Server 2012 1.建表过程中,如果用图形化的方式修改表结构会遇到问题: '不允许保存更改.您所做的更改要求删除并重新创建以下 ...

  2. MFC知识点总结

    一:消息1.什么是消息?消息是驱动windows系统运行的基础.从计算机的角度来看,消息就是一个整数.    (1)一个无符号整数,是消息值:    (2)消息附带的WPARAM和LPARAM类型的参 ...

  3. MIME类型记录

    Content-Disposition: attachment; filename="filename.xls" 提供下载

  4. Boost1.67编译+CMake Generate时遇到的一个错误

    下载的一个库编译时依赖boost,记录一下boost的编译: 下载源码 vs命令行里cd到根目录,运行bootstrap.bat,发现多了几个文件{b2.exe.bjam.exe.project-co ...

  5. Python基础:使用list & tuple

    list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. tuple 小结 list和tuple是Python内置的有序集合,一个可变,一个不 ...

  6. day26 hashlib, logging

    目录 hashlib hmac uuid logging v1 v2 v3 看了这个,上面的当作没看过 hashlib 为了防止密码在传输过程被抓取 对字符进行加密,相当于是一个自定义的字符编码表 原 ...

  7. Linux分布式测试

    在使用Jmeter进行性能测试时,如果并发数比较大(比如最近项目需要支持1000并发),单台电脑的配置(CPU和内存)可能无法支持,这时可以使用Jmeter提供的分布式测试的功能. 执行机和调度机做好 ...

  8. Python笔记12-----画图Matplotlib

    1.matplotlib:pyplot和pylab 如: import pylab as pl pl.figure(figsize=(8,6),dpi=100)[建立的图像大小和图的精度] pl.pl ...

  9. leetCode笔记--(1)

    陪朋友刷题,记录下. 1.Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operato ...

  10. Win32 编程消息常量(C#)

    public class WinMessages { #region 基本消息 public const int WM_NULL = 0x0000; public const int WM_CREAT ...