传送门

比着题解写还错。。。

查了两个小时没查出来,心态爆炸啊

以后再查

——代码(WA)

 #include <cstdio>
#include <cstring>
#include <iostream>
#define N 2000001
#define Min(x, y) ((x) < (y) ? (x) : (y))
#define Max(x, y) ((x) > (y) ? (x) : (y))
#define swap(x, y) ((x) ^= (y) ^= (x) ^= (y)) int n, m, cnt, qcnt, acnt;
int f[N], fa[N], max[N], min[N], up[N], down[N], ans[N];
int head[N], to[N << ], next[N << ], qhead[N], qu[N << ], qv[N << ], qid[N << ], qnext[N << ], ahead[N], ato[N << ], anext[N << ]; inline int read()
{
int x = , f = ;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -;
for(; isdigit(ch); ch = getchar()) x = (x << ) + (x << ) + ch - '';
return x * f;
} inline void add(int x, int y)
{
to[cnt] = y;
next[cnt] = head[x];
head[x] = cnt++;
} inline void add_ask(int x, int y, int z)
{
qu[qcnt] = x;
qv[qcnt] = y;
qid[qcnt] = z;
qnext[qcnt] = qhead[x];
qhead[x] = qcnt++;
} inline void add_ans(int x, int y)
{
ato[acnt] = y;
anext[acnt] = ahead[x];
ahead[x] = acnt++;
} inline int find(int x)
{
if(x ^ f[x])
{
int fx = f[x];
f[x] = find(f[x]);
up[x] = Max(up[x], Max(up[fx], max[fx] - min[x]));
down[x] = Max(down[x], Max(down[fx], max[x] - min[fx]));
max[x] = Max(max[fx], max[x]);
min[x] = Min(min[fx], max[x]);
}
return f[x];
} inline void dfs(int u)
{
int i, v, x, y;
f[u] = u;
for(i = qhead[u]; i ^ -; i = qnext[i])
if(f[qv[i]])
{
x = find(qv[i]);
add_ans(x, i);
}
for(i = head[u]; i ^ -; i = next[i])
{
v = to[i];
if(v ^ fa[u]) fa[v] = u, dfs(v);
}
for(i = ahead[u]; i ^ -; i = anext[i])
{
x = qu[ato[i]];
y = qv[ato[i]];
find(x), find(y);
if(ato[i] & ) swap(x, y);
ans[qid[ato[i]]] = Max(max[y] - min[x], Max(up[x], down[y]));
}
f[u] = fa[u];
} int main()
{
int i, j, x, y;
while(~scanf("%d", &n))
{
cnt = qcnt = acnt = ;
memset(head, -, sizeof(head));
memset(qhead, -, sizeof(qhead));
memset(ahead, -, sizeof(ahead));
memset(f, , sizeof(f));
memset(fa, , sizeof(fa));
memset(ans, , sizeof(ans));
for(i = ; i <= n; i++) min[i] = max[i] = read(), up[i] = down[i] = ;
for(i = ; i < n; i++)
{
x = read();
y = read();
add(x, y);
add(y, x);
}
m = read();
for(i = ; i <= m; i++)
{
x = read();
y = read();
add_ask(x, y, i);
add_ask(y, x, i);
}
dfs();
for(i = ; i <= m; i++) printf("%d\n", ans[i]);
}
return ;
}

[POJ3728]The merchant(tanrjan_lca + DP)的更多相关文章

  1. POJ 3728 The merchant(LCA+DP)

    The merchant Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total ...

  2. POJ 3278:The merchant(LCA&DP)

    The merchant Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6864   Accepted: 2375 Desc ...

  3. ACdreamOJ 1154 Lowbit Sum (数字dp)

    ACdreamOJ 1154 Lowbit Sum (数位dp) ACM 题目地址:pid=1154" target="_blank" style="color ...

  4. 「SDOI2016」储能表(数位dp)

    「SDOI2016」储能表(数位dp) 神仙数位 \(dp\) 系列 可能我做题做得少 \(QAQ\) \(f[i][0/1][0/1][0/1]\) 表示第 \(i\) 位 \(n\) 是否到达上界 ...

  5. 【HDU1693】Eat the Trees(插头dp)

    [HDU1693]Eat the Trees(插头dp) 题面 HDU Vjudge 大概就是网格图上有些点不能走,现在要找到若干条不相交的哈密顿回路使得所有格子都恰好被走过一遍. 题解 这题的弱化版 ...

  6. 【BZOJ1814】Ural 1519 Formula 1 (插头dp)

    [BZOJ1814]Ural 1519 Formula 1 (插头dp) 题面 BZOJ Vjudge 题解 戳这里 上面那个链接里面写的非常好啦. 然后说几个点吧. 首先是关于为什么只需要考虑三进制 ...

  7. 【BZOJ4712】洪水(动态dp)

    [BZOJ4712]洪水(动态dp) 题面 BZOJ 然而是权限题QwQ,所以粘过来算了. Description 小A走到一个山脚下,准备给自己造一个小屋.这时候,小A的朋友(op,又叫管理员)打开 ...

  8. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  9. 【POJ2411】Mondriaan's Dream(轮廓线DP)

    [POJ2411]Mondriaan's Dream(轮廓线DP) 题面 Vjudge 题解 这题我会大力状压!!! 时间复杂度大概是\(O(2^{2n}n^2)\),设\(f[i][S]\)表示当前 ...

随机推荐

  1. 《大规模 web服务开发》笔记

    大规模服务:     可扩展,负载均衡,保证冗余,低运维成本,开发人数和开发方法的变化 数据处理:     磁盘—>内存—>缓存—>CPU 障碍:     持续增长的服务,”无法在内 ...

  2. 2018.4.10 Ubuntu cat命令解答、用法

    cat命令是linux下的一个文本输出命令,通常是用于观看某个文件的内容的: cat主要有三大功能: 1.一次显示整个文件. $ cat filename 2.从键盘创建一个文件. $ cat > ...

  3. Python 继承实现的原理(继承顺序)

    继承顺序 Python3 : 新式类的查找顺序:广度优先 新式类的继承: class A(object): Python2 3 都是了 MRO算法--生成一个列表保存继承顺序表 不找到底部 Pytho ...

  4. apropos linux

    Apropos adj. 恰当的,关于,就...而言 adv. 顺便地,恰当地 All my suggestions apropos the script were accepted. 我所有有关该剧 ...

  5. js类型判别大合集

    1.typeof number,string,boolean,undefined,symbol,object,function 对象中除了函数为function,其他对象都判别为object, 缺陷: ...

  6. c#自定义类型之间的转换(强制类型转换)

    public class ResultModel { public string PlateNumber { get; set; } public int PlateColor { get; set; ...

  7. 通过脚本批量添加AD用户

    1.新建一个csv文件(逗号分隔的一种值文件) 内容为:放在C:\盘根目录下 test300 test300 .com test300 test301 test301 .com test301 tes ...

  8. LLDB详解

    LLDB的Xcode默认的调试器,它与LLVM编译器一起,带给我们更丰富的流程控制和数据检测的调试功能.平时用Xcode运行程序,实际走的都是LLDB.熟练使用LLDB,可以让你debug事半功倍 L ...

  9. Neural Style论文笔记+源码解析

    引言 前面在Ubuntu16.04+GTX1080配置TensorFlow并实现图像风格转换中介绍了TensorFlow的配置过程,以及运用TensorFlow实现图像风格转换,主要是使用了文章A N ...

  10. Linux安装OpenCV

    sudo apt-get update sudo apt-get install git git clone https://github.com/jayrambhia/Install-OpenCV ...