• POJ 2763 Housewife Wind 纯粹LCA写法(简单无脑)


    Description

    After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy life. People in XX Village lived in beautiful huts. There are some pairs of huts connected by bidirectional roads. We say that huts in the same pair directly connected. XX Village is so special that we can reach any other huts starting from an arbitrary hut. If each road cannot be walked along twice, then the route between every pair is unique. 

    Since Jiajia earned enough money, Wind became a housewife. Their children loved to go to other kids, then make a simple call to Wind: 'Mummy, take me home!' 

    At different times, the time needed to walk along a road may be different. For example, Wind takes 5 minutes on a road normally, but may take 10 minutes if there is a lovely little dog to play with, or take 3 minutes if there is some unknown strange smell surrounding the road. 

    Wind loves her children, so she would like to tell her children the exact time she will spend on the roads. Can you help her? 

    Input

    The first line contains three integers n, q, s. There are n huts in XX Village, q messages to process, and Wind is currently in hut s. n < 100001 , q < 100001. 

    The following n-1 lines each contains three integers a, b and w. That means there is a road directly connecting hut a and b, time required is w. 1<=w<= 10000. 

    The following q lines each is one of the following two types: 

    Message A: 0 u 
    A kid in hut u calls Wind. She should go to hut u from her current position. 
    Message B: 1 i w 
    The time required for i-th road is changed to w. Note that the time change will not happen when Wind is on her way. The changed can only happen when Wind is staying somewhere, waiting to take the next kid. 

    Output

    For each message A, print an integer X, the time required to take the next child.

    Sample Input

    3 3 1
    1 2 1
    2 3 2
    0 2
    1 2 3
    0 3
    

    Sample Output

    1
    3

    修改边权的LCA 先把无根树dfs转化为有根树
    然后根据深度进行 dis的更新

      1 #include <cstdio>
      2 #include <cstring>
      3 #include <queue>
      4 #include <cmath>
      5 #include <algorithm>
      6 #include <set>
      7 #include <iostream>
      8 #include <map>
      9 #include <stack>
     10 #include <string>
     11 #include <vector>
     12 #define  pi acos(-1.0)
     13 #define  eps 1e-6
     14 #define  fi first
     15 #define  se second
     16 #define  lson l,m,rt<<1
     17 #define  rson m+1,r,rt<<1|1
     18 #define  bug         printf("******
    ")
     19 #define  mem(a,b)    memset(a,b,sizeof(a))
     20 #define  fuck(x)     cout<<"["<<x<<"]"<<endl
     21 #define  f(a)        a*a
     22 #define  sf(n)       scanf("%d", &n)
     23 #define  sff(a,b)    scanf("%d %d", &a, &b)
     24 #define  sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
     25 #define  sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
     26 #define  pf          printf
     27 #define  FRE(i,a,b)  for(i = a; i <= b; i++)
     28 #define  FREE(i,a,b) for(i = a; i >= b; i--)
     29 #define  FRL(i,a,b)  for(i = a; i < b; i++)
     30 #define  FRLL(i,a,b) for(i = a; i > b; i--)
     31 #define  FIN         freopen("DATA.txt","r",stdin)
     32 #define  gcd(a,b)    __gcd(a,b)
     33 #define  lowbit(x)   x&-x
     34 #pragma  comment (linker,"/STACK:102400000,102400000")
     35 using namespace std;
     36 typedef long long LL;
     37 typedef unsigned long long ULL;
     38 const int maxn = 2e5 + 10;
     39 int _pow[maxn], R[maxn], dis[maxn], vis[maxn], ver[maxn], dep[maxn];
     40 int tot, head[maxn], dp[maxn * 2][30], k, first[maxn], val[maxn];
     41 struct node {
     42     int u, v, w, nxt;
     43 } edge[maxn << 2];
     44 void init() {
     45     tot = 0;
     46     mem(head, -1);
     47 }
     48 void add(int u, int v, int w) {
     49     edge[tot].v = v, edge[tot].u = u;
     50     edge[tot].w = w, edge[tot].nxt = head[u];
     51     head[u] = tot++;
     52 }
     53 void dfs(int u, int d) {
     54     vis[u] = 1;
     55     ver[++k] = u;
     56     first[u] = k;
     57     R[k] = d;
     58     dep[u] = d;
     59     for (int i = head[u]; ~i; i = edge[i].nxt) {
     60         if (vis[edge[i].v]) continue;
     61         int v = edge[i].v, w = edge[i].w;
     62         dis[v] = dis[u] + w;
     63         dfs(v, d + 1);
     64         ver[++k] = u;
     65         R[k] = d;
     66     }
     67 }
     68 void ST(int len) {
     69     int K = (int)(log((double)len) / log(2.0));
     70     for (int i = 1 ; i <= len ; i++) dp[i][0] = i;
     71     for (int j = 1 ; j <= K ; j++) {
     72         for (int i = 1 ; i + _pow[j] - 1 <= len ; i++) {
     73             int a = dp[i][j - 1], b = dp[i + _pow[j - 1]][j - 1];
     74             if (R[a] < R[b]) dp[i][j] = a;
     75             else dp[i][j] = b;
     76         }
     77     }
     78 }
     79 int RMQ(int x, int y) {
     80     int K = (int)(log((double)(y - x + 1)) / log(2.0));
     81     int a = dp[x][K], b = dp[y - _pow[K] + 1][K];
     82     if (R[a] < R[b]) return a;
     83     else return b;
     84 }
     85 int LCA(int u, int v) {
     86     int x = first[u], y = first[v];
     87     if (x > y) swap(x, y);
     88     int ret = RMQ(x, y);
     89     return ver[ret];
     90 }
     91 void update(int u, int fa, int ret) {
     92     dis[u] += ret;
     93     for (int i = head[u]; ~i ; i = edge[i].nxt) {
     94         int v = edge[i].v;
     95         if (v == fa) continue;
     96         update(v, u, ret);
     97     }
     98 }
     99 int main() {
    100     for (int i = 0 ; i < 40 ; i++) _pow[i] = (1 << i);
    101     int n, q, s;;
    102     while(~sfff(n, q, s)) {
    103         init();
    104         mem(vis, 0);
    105         mem(dep, 0);
    106         for (int i = 0 ; i < n - 1 ; i++) {
    107             int u, v, w;
    108             sfff(u, v, w);
    109             add(u, v, w);
    110             add(v, u, w);
    111         }
    112         k = 0, dis[1] = 0;
    113         dfs(1, 1);
    114         ST(2 * n - 1);
    115         while(q--) {
    116             int op, i, w, t;
    117             sf(op);
    118             if (op) {
    119                 sff(i, w);
    120                 i = (i - 1) << 1;
    121                 int u = edge[i].u, v = edge[i].v;
    122                 int ret = w - edge[i].w;
    123                 edge[i].w = edge[i ^ 1].w = w;
    124                 int x = dep[u] > dep[v] ? u : v;
    125                 int y = dep[u] < dep[v] ? u : v;
    126                 update(x, y, ret);
    127             } else {
    128                 sf(t);
    129                 int lca = LCA(s, t);
    130                 printf("%d
    ", dis[s] + dis[t] - 2 * dis[lca]);
    131                 s = t;
    132             }
    133         }
    134     }
    135     return  0;
    136 }
  • 相关阅读:
    宣化上人:大佛顶首楞严经四种清净明诲浅释(4-5)(转自学佛网:http://www.xuefo.net/nr/article23/230699.html)
    宣化上人:大佛顶首楞严经四种清净明诲浅释(6-7)(转自学佛网:http://www.xuefo.net/nr/article23/230700.html)
    宣化上人: 大佛顶首楞严经四种清净明诲浅释(8-9)(转自学佛网:http://www.xuefo.net/nr/article23/230825.html)
    [我的CVE][CVE-2017-15708]Apache Synapse Remote Code Execution Vulnerability
    Ubuntu bash不记录history方法
    [我的CVE][CVE-2017-15709]Apache ActiveMQ Information Leak
    java.lang.Runtime.exec() Payload Workarounds
    CVE-2017-12149 JBOOS AS 6.X 反序列化漏洞利用
    Apache Continuum 远程命令执行漏洞
    docker 端口映射iptables: No chain/target/match by that name错误解决方法
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9449479.html
Copyright © 2020-2023  润新知