poj3728 The merchant

题链http://poj.org/problem?id=3728

题述:一个树有N个节点,每个节点都有同一件物品,但不同的节点价格不相同,给出每个节点的价钱,Q次询问x,y对于每次询问求从x到y进行一次买卖最大可以获取的利润。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;
const int inf=20000000, N=50010;
struct edge
{
  int v,id,nx;
}e[3][N*2];
int i,n,m,size,u,v,head[3][N];
int uu[N],vv[N],w[N],ans[N],up[N],down[N],maxw[N],minw[N],f[N];
bool b[N];

void add(int u,int v,int id,int p)
{
  size++; e[p][size].v=v; e[p][size].id=id;
  e[p][size].nx=head[p][u]; head[p][u]=size;
}

void init()
{
  size=0;
  for(i=0;i<=n;i++)
  {
    head[0][i]=head[1][i]=head[2][i]=-1;
    b[i]=false;
  }
  for(i=1;i<=n;i++)
  {
    scanf("%d",&w[i]);
    up[i]=down[i]=0;
    maxw[i]=minw[i]=w[i];
  }
  for(i=1;i<n;i++)
  {
    scanf("%d%d",&u,&v);
    add(u,v,i,0);
    add(v,u,i,0);
  }
  size=0; scanf("%d",&m);
  for(i=1;i<=m;i++)
  {
    scanf("%d%d",&uu[i],&vv[i]);
    add(uu[i],vv[i],i,1);
    add(vv[i],uu[i],i,1);
  }
}

int Max(int a,int b)
{
  return (a>b)?a:b;
}
int Min(int a,int b)
{
  return (a<b)?a:b;
}

int find(int v)
{

  if (v==f[v]) return f[v];
  int fa=f[v];
  f[v]=find(f[v]);
  up[v]=Max(Max(up[v],up[fa]),maxw[fa]-minw[v]);
  down[v]=Max(Max(down[v],down[fa]),maxw[v]-minw[fa]);
  maxw[v]=Max(maxw[v],maxw[fa]);
  minw[v]=Min(minw[v],minw[fa]);
  return f[v];
}

void lca(int u)
{
  int i,v,id,fa;
  b[u]=true;
  f[u]=u;
  i=head[1][u];
  while (i!=-1)
  {
    v=e[1][i].v; id=e[1][i].id;
    if (b[v])
    {
      fa=find(v);
      add(fa,v,id,2);
    }
    i=e[1][i].nx;
  }
  i=head[0][u];
  while (i!=-1)
  {
    v=e[0][i].v;
    if (!b[v])
    {
      lca(v);
      f[v]=u;
    }
    i=e[0][i].nx;
  }
  i=head[2][u];
  while (i!=-1)
  {
    id=e[2][i].id;
    find(uu[id]); find(vv[id]);
    ans[id]=Max(Max(up[uu[id]],down[vv[id]]),maxw[vv[id]]-minw[uu[id]]);
    i=e[2][i].nx;
  }
}

int main()
{
  while (scanf("%d",&n)==1)
  {
    init();
    size=0; lca(1);
    for(i=1;i<=m;i++) printf("%d\n",ans[i]);
  }
  return 0;
}

时间: 2024-11-10 01:15:49

poj3728 The merchant的相关文章

POJ3728 The merchant解题报告

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 on

题解 【POJ3728】The merchant(LCA)

题意:一棵树有N个城市,每个城市商品价格不一样,Q个询问,问从u出发到达v点,每个城市只能经过一次的最大利润 max min数组存u城到u的第2^i个祖先路径上的最值 答案就是u-v路径上的最大值-最小值 真的是这样吗? 仔细想想,买入点可能在卖出点之后吗?当然不行 于是把路径分成两段 问题就变成下列三种情况 购买和卖出都在A段中完成 购买和卖出都在B段中完成 在A段中购买,B段中卖出 然后将A,B段分别进行处理(感觉像是dp的思想) 这还不够,还要分方向进行处理 shun[u]表示从u往LCA

ThoughtWorks笔试题之Merchant&#39;s Guide To The Galaxy解析

一.背景 在某网站上看到ThoughtWorks在武汉招人,待遇在本地还算不错,就投递了简历.第二天HR就打开电话,基本了解了一下情况(工作环境不错,男人妹子比例:1:1,双休,六险一金,满一年年假15天,病假8天,月薪1W--2W).然后立马收到一封:Coding Assignment的笔试题目.网上搜索了一下,发现这个公司还是挺大的,公司面试流程是出了名的繁杂和苛刻.据说有8轮:电话面试=>笔试=>Homework=>结对编程(中午管饭)=>技术面试=>PM面试=>

[最近公共祖先] POJ 3728 The merchant

The merchant Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 4556   Accepted: 1576 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 w

Apple 开发者账号没有Merchant IDs

今天和杭创的联调碰到一个问题:他的Apple 开发者账号无法添加Merchant IDs .国内百度了很久貌似都没有一个详细的说明.然后Google.搜索结果如下: http://stackoverflow.com/questions/30119629/applepay-how-to-r Apple 企业账号:无法使用Apple Pay功能. 如只有已经有了企业账号,可尝试如下第三方的: https://github.com/stripe/ApplePayStubs

poj——3728 The merchant

The merchant Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5055   Accepted: 1740 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 w

Paypal获取Transaciton历史交易API初步研究(二)——Merchant API调用

关于调用Classic API查询Transaction记录,是使用Paypal的Merchant API,需要在项目的工程导入SDK包.下面是在Maven项目的pom.xml加入代码: 1 <dependency> 2 <groupId>com.paypal.sdk</groupId> 3 <artifactId>merchantsdk</artifactId> 4 <version>2.2.98</version> 5

poj 3728 The merchant(LCA)

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 on

如何查看卖家ID (Merchant ID) 亚马逊哪里找?

如何查看卖家ID (Merchant ID) 亚马逊哪里找? 如何查看卖家ID (Merchant ID) 亚马逊哪里找? 1. 找到想要获取ID的卖家,点击店铺名(跟卖的卖家会收在”Other Sellers on Amazon”底下) (没有跟卖可跳过下面这步) 跟卖请看下图:页面跳转至跟卖卖家,找到想要查找ID的卖家,点击它的“卖家信息(Seller Information) 2. 页面跳转,地址栏“seller=”后面的字符串即卖家ID,双击可以选中它: Vipon    最大的纯亚马逊