zoukankan      html  css  js  c++  java
  • 【POJ】2763 Housewife Wind

      1 #include<cstdio>
      2 #include<cstring>
      3 #include<queue>
      4 #include<iostream>
      5 #define MAXN 100010
      6 #define MAXM 200010
      7 using namespace std;
      8 struct LCT {
      9     int bef[MAXN], belong[MAXN];
     10     int next[MAXN][2], pre[MAXN], key[MAXN], sum[MAXN];
     11     void Init() {
     12         memset(next, 0, sizeof(next));
     13         memset(pre, 0, sizeof(pre));
     14         memset(sum, 0, sizeof(sum));
     15     }
     16     inline void PushUp(int x) {
     17         sum[x] = key[x] + sum[next[x][0]] + sum[next[x][1]];
     18     }
     19     void Rotate(int x, int kind) {
     20         int y, z;
     21         y = pre[x];
     22         z = pre[y];
     23         next[y][!kind] = next[x][kind];
     24         pre[next[x][kind]] = y;
     25         next[z][next[z][1] == y] = x;
     26         pre[x] = z;
     27         next[x][kind] = y;
     28         pre[y] = x;
     29         PushUp(y);
     30     }
     31     void Splay(int x) {
     32         int rt;
     33         for (rt = x; pre[rt]; rt = pre[rt])
     34             ;
     35         if (rt != x) {
     36             bef[x] = bef[rt];
     37             bef[rt] = 0;
     38             while (pre[x]) {
     39                 if (next[pre[x]][0] == x)
     40                     Rotate(x, 1);
     41                 else
     42                     Rotate(x, 0);
     43             }
     44             PushUp(x);
     45         }
     46     }
     47     void Access(int x) {
     48         int father;
     49         for (father = 0; x; x = bef[x]) {
     50             Splay(x);
     51             pre[next[x][1]] = 0;
     52             bef[next[x][1]] = x;
     53             next[x][1] = father;
     54             pre[father] = x;
     55             bef[father] = 0;
     56             father = x;
     57             PushUp(x);
     58         }
     59     }
     60     void Change(int x, int val) {
     61         key[x] = val;
     62         Splay(x);
     63     }
     64     int Query(int x, int y) {
     65         Access(y);
     66         for (y = 0; x; x = bef[x]) {
     67             Splay(x);
     68             if (!bef[x])
     69                 return sum[y] + sum[next[x][1]];
     70             pre[next[x][1]] = 0;
     71             bef[next[x][1]] = x;
     72             next[x][1] = y;
     73             pre[y] = x;
     74             bef[y] = 0;
     75             y = x;
     76             PushUp(x);
     77         }
     78         return 0;
     79     }
     80 } lct;
     81 bool vis[MAXN];
     82 int first[MAXN], next[MAXM], v[MAXM], cost[MAXM], e;
     83 int INT() {
     84     char ch;
     85     int res;
     86     while (ch = getchar(), !isdigit(ch))
     87         ;
     88     for (res = ch - '0'; ch = getchar(), isdigit(ch);)
     89         res = res * 10 + ch - '0';
     90     return res;
     91 }
     92 inline void AddEdge(int x, int y, int val) {
     93     v[e] = y;
     94     cost[e] = val;
     95     next[e] = first[x];
     96     first[x] = e++;
     97 }
     98 void BFS(int x) {
     99     int i, y;
    100     queue<int> q;
    101     memset(vis, false, sizeof(vis));
    102     vis[x] = true;
    103     q.push(x);
    104     while (!q.empty()) {
    105         x = q.front();
    106         q.pop();
    107         for (i = first[x]; i != -1; i = next[i]) {
    108             y = v[i];
    109             if (!vis[y]) {
    110                 lct.bef[y] = x;
    111                 lct.key[y] = cost[i];
    112                 lct.belong[i >> 1] = y;
    113                 vis[y] = true;
    114                 q.push(y);
    115             }
    116         }
    117     }
    118 }
    119 int main() {
    120     int n, s, q, i;
    121     int x, y, val, cmd;
    122     while (~scanf("%d%d%d", &n, &q, &s)) {
    123         lct.Init();
    124         memset(first, -1, sizeof(first));
    125         for (e = 0, i = 1; i < n; i++) {
    126             x = INT(), y = INT(), val = INT();
    127             AddEdge(x, y, val);
    128             AddEdge(y, x, val);
    129         }
    130         BFS(1);
    131         while (q--) {
    132             cmd = INT();
    133             if (cmd) {
    134                 x = INT(), val = INT();
    135                 lct.Change(lct.belong[x - 1], val);
    136             } else {
    137                 x = INT();
    138                 printf("%d\n", lct.Query(s, x));
    139                 s = x;
    140             }
    141         }
    142     }
    143     return 0;
    144 }
  • 相关阅读:
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    lambda表达式
    VIM--保存和退出等命令
  • 原文地址:https://www.cnblogs.com/DrunBee/p/2653396.html
Copyright © 2011-2022 走看看