传送门

比着题解写还错。。。

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

以后再查

——代码(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. Python XML 解析

    什么是 XML? XML 指可扩展标记语言(eXtensible Markup Language). XML 被设计用来传输和存储数据. XML 是一套定义语义标记的规则,这些标记将文档分成许多部件并 ...

  2. keycode对应表

    keycode对应表 keycode    8 = BackSpace 回格keycode    9 = Tab keycode   12 = Clearkeycode   13 = Enter 回车 ...

  3. python_87_shelve模块

    'shelve模块是一个简单的key,value将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式(只支持pickle)' #序列化,将数据写入文件 import ...

  4. jQuery JavaScript Library v3.2.1

    /*! * jQuery JavaScript Library v3.2.1 * https://jquery.com/ * * Includes Sizzle.js * https://sizzle ...

  5. Golang glog使用详解

    golang/glog 是 C++ 版本 google/glog 的 Go 版本实现,基本实现了原生 glog 的日志格式.在 Kuberntes 中,glog 是默认日志库. glog 的使用与特性 ...

  6. FZOJβ #31.字符串

    http://1572m36l09.iask.in:30808/problem/31 首先转化为保留尽量少的段使得字典序最大.考虑逐字符确定,显然我们可以将相同的连续字符缩在一起.注意到字典序最大的字 ...

  7. java中插入myslq的datetime类型的

    java.util.Date d = new java.util.Date(); Timestamp tt=new Timestamp(d.getTime()); ps.setTimestamp(4, ...

  8. presenting view controller

    Present ViewController详解 Present ViewController Modally 一.主要用途 弹出模态ViewController是IOS变成中很有用的一个技术,UIK ...

  9. Bzoj 1131[POI2008]STA-Station (树形DP)

    Bzoj 1131[POI2008]STA-Station (树形DP) 状态: 设\(f[i]\)为以\(i\)为根的深度之和,然后考虑从他父亲转移. 发现儿子的深度及其自己的深度\(-1\) 其余 ...

  10. CentOS7.2 虚拟机网卡无法启动

    在开机之后,发现网卡没有启动起来,进行了如下操作1.ifup ens33Bringing up interface ens33: Error: Connection activation failed ...