poj3728The merchant
The merchant
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 4800 | Accepted: 1666 |
Description
There are N cities in a country, and there is one and only one simple path between each pair of cities. A merchant has chosen some paths and wants to earn as much money as possible in each path. When he move along a path, he can choose one city to buy some goods and sell them in a city after it. The goods in all cities are the same but the prices are different. Now your task is to calculate the maximum possible profit on each path.
Input
The first line contains N, the number of cities. Each of the next N lines contains wi the goods' price in each city. Each of the next N-1 lines contains labels of two cities, describing a road between the two cities. The next line contains Q, the number of paths. Each of the next Q lines contains labels of two cities, describing a path. The cities are numbered from 1 to N.
1 ≤ N, wi, Q ≤ 50000
Output
The output contains Q lines, each contains the maximum profit of the corresponding path. If no positive profit can be earned, output 0 instead.
Sample Input
- 4
- 1
- 5
- 3
- 2
- 1 3
- 3 2
- 3 4
- 9
- 1 2
- 1 3
- 1 4
- 2 3
- 2 1
- 2 4
- 3 1
- 3 2
- 3 4
Sample Output
- 4
- 2
- 2
- 0
- 0
- 0
- 0
- 2
- 0
Source
- #include <cstdio>
- #include <cstring>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- int n,a[],head[],to[],nextt[],tot,q,d[];
- int fa[][], minn[][], maxx[][], lmax[][], rmin[][];
- void add(int x, int y)
- {
- to[tot] = y;
- nextt[tot] = head[x];
- head[x] = tot++;
- }
- void dfs(int u, int from,int dist)
- {
- d[u] = dist;
- fa[u][] = from;
- for (int i = head[u]; i != -; i = nextt[i])
- {
- int v = to[i];
- if (from != v)
- dfs(v, u,dist + );
- }
- }
- void init()
- {
- dfs(, , );
- minn[][] = maxx[][] = a[];
- lmax[][] = - << ;
- rmin[][] = << ;
- for (int i = ; i <= n; i++)
- {
- minn[i][] = min(a[i], a[fa[i][]]);
- maxx[i][] = max(a[i], a[fa[i][]]);
- lmax[i][] = max(a[fa[i][]] - a[i], );
- rmin[i][] = min(a[fa[i][]] - a[i], );
- }
- for (int j = ; ( << j) < n; j++)
- for (int i = ; i <= n; i++)
- {
- fa[i][j] = fa[fa[i][j - ]][j - ];
- maxx[i][j] = max(maxx[i][j - ], maxx[fa[i][j - ]][j - ]);
- minn[i][j] = min(minn[i][j - ], minn[fa[i][j - ]][j - ]);
- lmax[i][j] = max(max(lmax[i][j - ], lmax[fa[i][j - ]][j - ]), maxx[fa[i][j-]][j - ] - minn[i][j - ]);
- rmin[i][j] = min(min(rmin[i][j - ], rmin[fa[i][j - ]][j - ]), minn[fa[i][j - ]][j - ] - maxx[i][j - ]);
- }
- }
- int LCA(int x, int y)
- {
- int maxxx = , miny = , minx = a[x], maxy = a[y];
- for (int i = ; i >= && d[x] != d[y]; i--)
- {
- if (abs(d[x] - d[y]) >= << i)
- {
- if (d[y] < d[x])
- {
- maxxx = max(max(maxxx, lmax[x][i]), maxx[x][i] - minx);
- minx = min(minx, minn[x][i]);
- x = fa[x][i];
- }
- else
- {
- miny = min(min(miny, rmin[y][i]), minn[y][i] - maxy);
- maxy = max(maxy, maxx[y][i]);
- y = fa[y][i];
- }
- }
- }
- if (x == y)
- return max(max(maxxx, -miny), maxy - minx);
- for (int i = ; i >= ;i--)
- if (fa[x][i] != fa[y][i] && fa[x][i] && fa[y][i])
- {
- maxxx = max(max(maxxx, lmax[x][i]), maxx[x][i] - minx);
- minx = min(minx, minn[x][i]);
- x = fa[x][i];
- miny = min(min(miny, rmin[y][i]), minn[y][i] - maxy);
- maxy = max(maxy, maxx[y][i]);
- y = fa[y][i];
- }
- maxxx = max(max(maxxx, lmax[x][]), maxx[x][] - minx);
- minx = min(minx, minn[x][]);
- miny = min(min(miny, rmin[y][]), minn[y][] - maxy);
- maxy = max(maxy, maxx[y][]);
- return max(max(maxxx, -miny), maxy - minx);
- }
- int main()
- {
- memset(head, -, sizeof(head));
- scanf("%d", &n);
- for (int i = ; i <= n; i++)
- scanf("%d", &a[i]);
- for (int i = ; i <= n - ; i++)
- {
- int u, v;
- scanf("%d%d", &u, &v);
- add(u, v);
- add(v, u);
- }
- init();
- scanf("%d", &q);
- while (q--)
- {
- int x, y;
- scanf("%d%d", &x, &y);
- printf("%d\n", LCA(x, y));
- }
- return ;
- }
poj3728The merchant的更多相关文章
- poj3728The merchant 【倍增】【LCA】
There are N cities in a country, and there is one and only one simple path between each pair of citi ...
- POJ3728The merchant (倍增)(LCA)(DP)(经典)(||并查集压缩路径?)
There are N cities in a country, and there is one and only one simple path between each pair of citi ...
- poj3728The merchant树剖+线段树
如果直接在一条直线上,那么就建线段树 考虑每一个区间维护最小值和最大值和答案,就符合了合并的条件,一个log轻松做 那么在树上只要套一个树剖就搞定了,多一个log也不是问题 注意考虑在树上的话每一条链 ...
- [最近公共祖先] POJ 3728 The merchant
The merchant Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 4556 Accepted: 1576 Desc ...
- POJ 3278 The merchant
传送门 Time Limit: 3000MS Memory Limit: 65536K Description There are N cities in a country, and there i ...
- poj 3728 The merchant(LCA)
Description There are N cities in a country, and there is one and only one simple path between each ...
- ThoughtWorks Merchant's Guide To The Galaxy
ThoughtWorks笔试题之Merchant's Guide To The Galaxy解析 一.背景 在某网站上看到ThoughtWorks在武汉招人,待遇在本地还算不错,就投递了简历.第二天H ...
- [POJ 3728]The merchant
Description There are N cities in a country, and there is one and only one simple path between each ...
- opencart3图片Google Merchant Center验证通过不了的解决方法
最近在做一个opencart项目,有对接Google Merchant Center,但是一直提示产品图片验证无法通过,ytkah看了一下图片路径,/image/cache/catalog/demo/ ...
随机推荐
- PWD简介与妙用(一个免费、随时可用的Docker实验室)
转载自 https://baiyue.one/archives/472.html 本文介绍下 PWD 的历史,并依据本站最近学习心得,经过多次尝试,终于打通了 Docker 与常规宝塔面板搭建,因此, ...
- 二、antd pro 删除eslint检测
删除package.json 里 " pre-commit": "npm run lint-staged" 这个对象就可以.
- 2018.3.11 Java DEBUG 调试的方法
F5:Step Into:跳进当前执行的方法中 F6:Step Over:跳过该行继续执行 F7:Step Return:从方法中跳出继续执行 F8:Resume:全部运行通过,从调试状态恢复 Ctr ...
- 使用lua做序列化和反序列化
-- lua对象序列化 function serialize(obj) local lua = "" local t = type(obj) if t == "numbe ...
- Unity3d 中键值监听方法
unity3d的api中没有负责监听键值的方法,不过unity的input类是通过c#类获取各类监听事件,所以我们可以通过c#类监听,方法如下: void OnGUI() { Event e = Ev ...
- 【kmp】bzoj3620: 似乎在梦中见过的样子
考察kmp理解题 Description “Madoka,不要相信 QB!”伴随着 Homura 的失望地喊叫,Madoka 与 QB 签订了契约. 这是 Modoka 的一个噩梦,也同时是上个轮回中 ...
- JWT的使用流程
JWT的实现原理 一篇文章告诉你JWT的实现原理 发布于 3 个月前 作者 axetroy 3097 次浏览 来自 分享 在使用 JWT 的时候,有没有想过,为什么我们需要 JWT?以及它的工作原理是 ...
- linux常用命令(配置查看,定时任务)
1.查看所有待挂载设备信息 fdisk -l # fdisk -l Disk /dev/sda: bytes heads, sectors/track, cylinders, total sector ...
- Python3 简单封装 sqlite3 - SimpleToolSql
#coding: utf-8 #Author:boxker #Mail:icjb@foxmail.com import sqlite3 import os class simpleToolSql(): ...
- angular5自适应窗口大小
import {AfterViewInit, Directive, ElementRef, HostBinding, HostListener, Inject, Input, Renderer2} f ...