zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Could note find result map com.xxxx.entity.UserAccountDO
    浏览器通过file://访问文件和通过http://访问文件有什么区别
    FreeMarker template error: The following has evaluated to null or missing: ==> blogger.md [in template "admin/about.ftl" at line 44, column 84]
    2018-01-03 --活动观赏鱼的2017总结及2018年展望
    eclipse open call hierarchy无效
    Mysql 查询重复的记录
    oracle用户、权限操作
    Oracle 修改表操作
    Python 魔法方法详解
    Python __dict__和vars()
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3711911.html
Copyright © 2011-2022 走看看