• POJ 3728


    The merchant
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 2887   Accepted: 958

    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 ≤ NwiQ ≤ 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

     
    lca (trajan) 
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 #include <vector>
      6 #include <stack>
      7 
      8 using namespace std;
      9 
     10 const int MAX_N = 50005;
     11 
     12 int N;
     13 int pri[MAX_N],first[MAX_N],v[MAX_N * 2],Next[MAX_N * 2];
     14 int qu[MAX_N],qv[MAX_N];
     15 int Min[MAX_N],Max[MAX_N],down[MAX_N],up[MAX_N];
     16 int f[MAX_N],ans[MAX_N];
     17 bool vis[MAX_N];
     18 vector<int> s[MAX_N];
     19 vector<int> Q[MAX_N];
     20 
     21 void add_edge(int id,int u) {
     22         int e = first[u];
     23         Next[id] = e;
     24         first[u] = id;
     25 }
     26 
     27 int _find(int x) {
     28         if(f[x] == x) return x;
     29 
     30         int t = _find(f[x]);
     31         up[x] = max(up[x],Max[ f[x] ] - Min[ x ]);
     32         up[x] = max(up[x],up[ f[x] ]);
     33         down[x] = max(down[x],down[ f[x] ]);
     34         down[x] = max(down[x],Max[ x ] - Min[ f[x] ]);
     35         Max[x] = max(Max[x],Max[ f[x] ]);
     36         Min[x] = min(Min[x],Min[ f[x] ]);
     37 
     38         return f[x] = t;
     39 }
     40 
     41 void dfs(int u,int fa) {
     42         for(int e = first[u]; e != -1; e = Next[e]) {
     43                 if(v[e] == fa) continue;
     44                 dfs(v[e],u);
     45                 f[_find( v[e] )] = u;
     46         }
     47 
     48         vis[u] = 1;
     49         for(int i = 0; i < Q[u].size(); ++i) {
     50                 int V = qu[ Q[u][i] ] != u ? qu[ Q[u][i] ] : qv[ Q[u][i] ];
     51                 if(vis[V]) {
     52                         int p = _find(V);
     53                         s[p].push_back(Q[u][i]);
     54                 }
     55         }
     56 
     57         for(int i = 0; i < s[u].size(); ++i) {
     58                 int a,b;
     59                 int e = s[u][i];
     60                 a = qu[e],b = qv[e];
     61                 _find(a);
     62                 _find(b);
     63                 ans[e] = max(up[a],down[b]);
     64                 ans[e] = max(ans[e],Max[b] - Min[a]);
     65         }
     66 
     67 
     68 
     69 }
     70 
     71 int main()
     72 {
     73    // freopen("sw.in","r",stdin);
     74     scanf("%d",&N);
     75     for(int i = 1; i <= N; ++i) first[i] = -1;
     76     for(int i = 1; i <= N; ++i) f[i] = i;
     77     for(int i = 1; i <= N; ++i) {
     78             scanf("%d",&pri[i]);
     79             Max[i] = Min[i] = pri[i];
     80     }
     81 
     82     for(int i = 1; i <= 2 * (N - 1); i += 2) {
     83             int u;
     84             scanf("%d%d",&u,&v[i]);
     85             v[i + 1] = u;
     86             add_edge(i,u);
     87             add_edge(i + 1,v[i]);
     88     }
     89 
     90     int q;
     91     scanf("%d",&q);
     92     for(int i = 1; i <= q; ++i) {
     93             scanf("%d%d",&qu[i],&qv[i]);
     94             Q[ qu[i] ].push_back(i);
     95             Q[ qv[i] ].push_back(i);
     96     }
     97 
     98     dfs(1,-1);
     99 
    100     for(int i = 1; i <= q; ++i) {
    101             printf("%d
    ",ans[i]);
    102     }
    103 
    104     return 0;
    105 }
    View Code
  • 相关阅读:
    AIO5岗位桌面主页【我的收藏夹】只显示8行,怎样增加显示行?
    已设置了参考编号的编码规则定义,但是新增单据的时候,没有出来自动编号是什么原因?
    10个TWaver 网页3D可视化精彩案例
    HTML5大数据可视化效果(二)可交互地铁线路图
    无插件纯web 3D机房 (第四季:大型园区、地球仪效果和其他扩展应用)
    一款基于HTML5的Web 3D开发工具
    Legolas工业自动化平台入门(三)交互事件响应动作
    Legolas工业自动化平台入门(二)数据响应动作
    Legolas工业自动化平台入门(一)搭建应用
    Legolas工业自动化平台案例 —— 水源地自动化监控系统
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3711911.html
Copyright © 2020-2023  润新知