[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]\)表示当前 ...
随机推荐
- cv2.minAreaRect() 生成最小外接矩形
简介 使用python opencv返回点集cnt的最小外接矩形,所用函数为 cv2.minAreaRect(cnt) ,cnt是所要求最小外接矩形的点集数组或向量,这个点集不定个数. cv2 ...
- Problem T: 结构体--学生信息排序
Problem T: 结构体--学生信息排序 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2219 Solved: 1305[Submit][Sta ...
- Problem O: 国家排序
Problem O: 国家排序 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 405 Solved: 253[Submit][Status][Web ...
- python-判断alter是否存在
from selenium import webdriver import time from selenium.webdriver.support.ui import WebDriverWait f ...
- sql*plus常用指令介紹
sql*plus常用指令介紹 1.用set指令來設定SQL*Plus的環境參數值 格式: Set 環境參數名 環境參數值 ex:set feedback on set feedback 8.用show ...
- centos6启动故障排除
centos6中boot文件被全部删除的故障排除 /boot文件里关于启动的核心文件有三个,/vmlinuz-2.6.32-696.e16.x86_64,initramfs-2.6.32-696.el ...
- pandas关联mysql并读写数据库
1.代码读写mysql,必须安装关联mysql的工具 操作如下命令: sudo apt-get install mysql-server mysql-clientsudo apt-get instal ...
- Unity基础-编辑器
编辑器 Special Folders Hidden Folder(start with .) Standard Assets:第一批加载的文件 Editor:只在编辑下才能使用, Plugins R ...
- NodeJS基础入门-Event
大多数Node.js核心API都采用惯用的异步事件驱动架构,其中某些类型的对象(触发器)会周期性地触发命名事件来调用函数对象(监听器). 例如,net.Server对象会在每次有新连接时触发事件;fs ...
- Java中将字符串与unicode的相互转换工具类
unicode编码规则 unicode码对每一个字符用4位16进制数表示.具体规则是:将一个字符(char)的高8位与低8位分别取出,转化为16进制数,如果转化的16进制数的长度不足2位,则在其后补0 ...