[POJ3728]The merchant(tanrjan_lca + DP)

传送门

 

比着题解写还错。。。

查了两个小时没查出来,心态爆炸啊

以后再查

 

——代码(WA)

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <iostream>
  4 #define N 2000001
  5 #define Min(x, y) ((x) < (y) ? (x) : (y))
  6 #define Max(x, y) ((x) > (y) ? (x) : (y))
  7 #define swap(x, y) ((x) ^= (y) ^= (x) ^= (y))
  8 
  9 int n, m, cnt, qcnt, acnt;
 10 int f[N], fa[N], max[N], min[N], up[N], down[N], ans[N];
 11 int head[N], to[N << 1], next[N << 1], qhead[N], qu[N << 1], qv[N << 1], qid[N << 1], qnext[N << 1], ahead[N], ato[N << 1], anext[N << 1];
 12 
 13 inline int read()
 14 {
 15     int x = 0, f = 1;
 16     char ch = getchar();
 17     for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
 18     for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
 19     return x * f;
 20 }
 21 
 22 inline void add(int x, int y)
 23 {
 24     to[cnt] = y;
 25     next[cnt] = head[x];
 26     head[x] = cnt++;
 27 }
 28 
 29 inline void add_ask(int x, int y, int z)
 30 {
 31     qu[qcnt] = x;
 32     qv[qcnt] = y;
 33     qid[qcnt] = z;
 34     qnext[qcnt] = qhead[x];
 35     qhead[x] = qcnt++;
 36 }
 37 
 38 inline void add_ans(int x, int y)
 39 {
 40     ato[acnt] = y;
 41     anext[acnt] = ahead[x];
 42     ahead[x] = acnt++;
 43 }
 44 
 45 inline int find(int x)
 46 {
 47     if(x ^ f[x])
 48     {
 49         int fx = f[x];
 50         f[x] = find(f[x]);
 51         up[x] = Max(up[x], Max(up[fx], max[fx] - min[x]));
 52         down[x] = Max(down[x], Max(down[fx], max[x] - min[fx]));
 53         max[x] = Max(max[fx], max[x]);
 54         min[x] = Min(min[fx], max[x]);
 55     }
 56     return f[x];
 57 }
 58 
 59 inline void dfs(int u)
 60 {
 61     int i, v, x, y;
 62     f[u] = u;
 63     for(i = qhead[u]; i ^ -1; i = qnext[i])
 64         if(f[qv[i]])
 65         {
 66             x = find(qv[i]);
 67             add_ans(x, i);
 68         }
 69     for(i = head[u]; i ^ -1; i = next[i])
 70     {
 71         v = to[i];
 72         if(v ^ fa[u]) fa[v] = u, dfs(v);
 73     }
 74     for(i = ahead[u]; i ^ -1; i = anext[i])
 75     {
 76         x = qu[ato[i]];
 77         y = qv[ato[i]];
 78         find(x), find(y);
 79         if(ato[i] & 1) swap(x, y);
 80         ans[qid[ato[i]]] = Max(max[y] - min[x], Max(up[x], down[y]));
 81     }
 82     f[u] = fa[u];
 83 }
 84 
 85 int main()
 86 {
 87     int i, j, x, y;
 88     while(~scanf("%d", &n))
 89     {
 90         cnt = qcnt = acnt = 0;
 91         memset(head, -1, sizeof(head));
 92         memset(qhead, -1, sizeof(qhead));
 93         memset(ahead, -1, sizeof(ahead));
 94         memset(f, 0, sizeof(f));
 95         memset(fa, 0, sizeof(fa));
 96         memset(ans, 0, sizeof(ans));
 97         for(i = 1; i <= n; i++) min[i] = max[i] = read(), up[i] = down[i] = 0;
 98         for(i = 1; i < n; i++)
 99         {
100             x = read();
101             y = read();
102             add(x, y);
103             add(y, x);
104         }
105         m = read();
106         for(i = 1; i <= m; i++)
107         {
108             x = read();
109             y = read();
110             add_ask(x, y, i);
111             add_ask(y, x, i);
112         }
113         dfs(1);
114         for(i = 1; i <= m; i++) printf("%d\n", ans[i]);
115     }
116     return 0;
117 }
View Code

 

posted @ 2017-06-16 10:06  zht467  阅读(176)  评论(0编辑  收藏  举报