[POJ3728]The merchant(tanrjan_lca + DP)
比着题解写还错。。。
查了两个小时没查出来,心态爆炸啊
以后再查
——代码(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)的更多相关文章
- POJ 3728 The merchant(LCA+DP)
The merchant Time Limit : 6000/3000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total ...
- POJ 3278:The merchant(LCA&DP)
The merchant Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6864 Accepted: 2375 Desc ...
- ACdreamOJ 1154 Lowbit Sum (数字dp)
ACdreamOJ 1154 Lowbit Sum (数位dp) ACM 题目地址:pid=1154" target="_blank" style="color ...
- 「SDOI2016」储能表(数位dp)
「SDOI2016」储能表(数位dp) 神仙数位 \(dp\) 系列 可能我做题做得少 \(QAQ\) \(f[i][0/1][0/1][0/1]\) 表示第 \(i\) 位 \(n\) 是否到达上界 ...
- 【HDU1693】Eat the Trees(插头dp)
[HDU1693]Eat the Trees(插头dp) 题面 HDU Vjudge 大概就是网格图上有些点不能走,现在要找到若干条不相交的哈密顿回路使得所有格子都恰好被走过一遍. 题解 这题的弱化版 ...
- 【BZOJ1814】Ural 1519 Formula 1 (插头dp)
[BZOJ1814]Ural 1519 Formula 1 (插头dp) 题面 BZOJ Vjudge 题解 戳这里 上面那个链接里面写的非常好啦. 然后说几个点吧. 首先是关于为什么只需要考虑三进制 ...
- 【BZOJ4712】洪水(动态dp)
[BZOJ4712]洪水(动态dp) 题面 BZOJ 然而是权限题QwQ,所以粘过来算了. Description 小A走到一个山脚下,准备给自己造一个小屋.这时候,小A的朋友(op,又叫管理员)打开 ...
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- 【POJ2411】Mondriaan's Dream(轮廓线DP)
[POJ2411]Mondriaan's Dream(轮廓线DP) 题面 Vjudge 题解 这题我会大力状压!!! 时间复杂度大概是\(O(2^{2n}n^2)\),设\(f[i][S]\)表示当前 ...
随机推荐
- UVA 810 A Dicey Promblem 筛子难题 (暴力BFS+状态处理)
读懂题意以后还很容易做的, 和AbbottsRevenge类似加一个维度,筛子的形态,可以用上方的点数u和前面的点数f来表示,相对的面点数之和为7,可以预先存储u和f的对应右边的点数,点数转化就很容易 ...
- C#之winform实现文件拖拽功能【转】
将一个文件拖拽到窗体的某个控件时,将该控件的路径显示在该控件上,只要拿到了路径自然可以读取文件中的内容了 将一个控件的属性AllowDrop设置为true,然后添加DragDrop.DragEnter ...
- Android颜色选择器介绍
使用Android的颜色选择器可以让我们的view在不同状态下显示不同的颜色. 1.Android中ListView 选择某项改变该行字体颜色 2.文件位置 res/color/filename.xm ...
- TextView中使用Linkify添加超链接
首先,在TextView所属xml配置文件中,直接添加android:autoLink特性即可,它支持一个或多个(用分割线)自定义的值:none.web.email.phone或all. 另外, ...
- CPP-网络/通信:经典HTTP协议详解
2008-11-03 09:11 by Hundre, 266688 阅读, 23 评论, 收藏, 编辑 转自:http://blog.csdn.net/gueter/archive/2007/03/ ...
- Idea01 Idea2018中集成Tomcat9导致OutPut乱码
版本和平台 idea2018.3 tomcat9 jdk1.8 windows7 64位 output乱码 经过测试,项目编码格式设置为utf-8,在main方法中输出中文正常. 而iedea集成to ...
- PAT (Basic Level) Practise (中文)- 1014. 福尔摩斯的约会 (20)
http://www.patest.cn/contests/pat-b-practise/1014 1014. 福尔摩斯的约会 (20) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 ...
- MVCPager学习小记
1.PageIndexParameterName怎么关联? 答:其实就是Action里面的pageindex参数 例子: @Html.Pager(Model, new PagerOptions { P ...
- 不安装oracle客户端如何使用plsql连接数据库
不安装oracle客户端如何使用plsql连接数据库 1. 准备工作 1.1下载plsqldev破解版软件 我这里使用plsqldev715版本 1.2下载instantclient-basic-wi ...
- tensorflow目标检测API之建立自己的数据集
1 收集数据 为了方便,我找了11张月儿的照片做数据集,如图1,当然这在实际应用过程中是远远不够的 2 labelImg软件的安装 使用labelImg软件(下载地址:https://github.c ...