• 【URAL】1553 Caves and Tunnels


      1 #include<cstdio>
      2 #include<cstring>
      3 #include<queue>
      4 #include<iostream>
      5 #include<algorithm>
      6 #define MAXN 100010
      7 #define MAXM 200010
      8 using namespace std;
      9 bool vis[MAXN];
     10 int first[MAXN], second[MAXM], v[MAXM], e;
     11 struct LCT {
     12     int bef[MAXN];
     13     int next[MAXN][2], pre[MAXN], key[MAXN], val[MAXN];
     14     void Init() {
     15         memset(next, 0, sizeof(next));
     16         memset(pre, 0, sizeof(pre));
     17         memset(key, 0, sizeof(key));
     18     }
     19     inline void PushUp(int x) {
     20         val[x] = max(key[x], max(val[next[x][0]], val[next[x][1]]));
     21     }
     22     inline void Rotate(int x, int kind) {
     23         int y, z;
     24         y = pre[x];
     25         z = pre[y];
     26         next[y][!kind] = next[x][kind];
     27         pre[next[x][kind]] = y;
     28         next[z][next[z][1] == y] = x;
     29         pre[x] = z;
     30         next[x][kind] = y;
     31         pre[y] = x;
     32         PushUp(y);
     33     }
     34     void Splay(int x) {
     35         int rt;
     36         for (rt = x; pre[rt]; rt = pre[rt])
     37             ;
     38         if (rt != x) {
     39             bef[x] = bef[rt];
     40             bef[rt] = 0;
     41             while (pre[x]) {
     42                 if (next[pre[x]][0] == x)
     43                     Rotate(x, 1);
     44                 else
     45                     Rotate(x, 0);
     46             }
     47             PushUp(x);
     48         }
     49     }
     50     void Access(int x) {
     51         int father;
     52         for (father = 0; x; x = bef[x]) {
     53             Splay(x);
     54             bef[next[x][1]] = x;
     55             pre[next[x][1]] = 0;
     56             next[x][1] = father;
     57             pre[father] = x;
     58             bef[father] = 0;
     59             father = x;
     60             PushUp(x);
     61         }
     62     }
     63     void Change(int x, int val) {
     64         key[x] += val;
     65         Splay(x);
     66     }
     67     int Query(int x, int y) {
     68         Access(y);
     69         for (y = 0; x; x = bef[x]) {
     70             Splay(x);
     71             if (!bef[x])
     72                 return max(key[x], max(val[next[x][1]], val[y]));
     73             bef[next[x][1]] = x;
     74             pre[next[x][1]] = 0;
     75             next[x][1] = y;
     76             pre[y] = x;
     77             bef[y] = 0;
     78             y = x;
     79             PushUp(x);
     80         }
     81         return 0;
     82     }
     83 } lct;
     84 int INT() {
     85     int res;
     86     char ch;
     87     while (ch = getchar(), !isdigit(ch))
     88         ;
     89     for (res = ch - '0'; ch = getchar(), isdigit(ch);)
     90         res = res * 10 + ch - '0';
     91     return res;
     92 }
     93 char CHAR() {
     94     char res;
     95     while (res = getchar(), !isalpha(res))
     96         ;
     97     return res;
     98 }
     99 inline void AddEdge(int x, int y) {
    100     v[e] = y;
    101     second[e] = first[x];
    102     first[x] = e++;
    103 }
    104 void BFS(int x) {
    105     int i, y;
    106     queue<int> q;
    107     memset(vis, false, sizeof(vis));
    108     vis[x] = true;
    109     q.push(x);
    110     while (!q.empty()) {
    111         x = q.front();
    112         q.pop();
    113         for (i = first[x]; i != -1; i = second[i]) {
    114             y = v[i];
    115             if (!vis[y]) {
    116                 lct.bef[y] = x;
    117                 vis[y] = true;
    118                 q.push(y);
    119             }
    120         }
    121     }
    122 }
    123 int main() {
    124     char ch;
    125     int n, i, q, x, y;
    126     while (~scanf("%d", &n)) {
    127         lct.Init();
    128         memset(first, -1, sizeof(first));
    129         for (e = 0, i = 1; i < n; i++) {
    130             x = INT(), y = INT();
    131             AddEdge(x, y);
    132             AddEdge(y, x);
    133         }
    134         BFS(1);
    135         q = INT();
    136         while (q--) {
    137             ch = CHAR(), x = INT(), y = INT();
    138             if (ch == 'I')
    139                 lct.Change(x, y);
    140             else
    141                 printf("%d\n", lct.Query(x, y));
    142         }
    143     }
    144     return 0;
    145 }
  • 相关阅读:
    scrapy 随机UserAgent
    Scrapy使用中间件捕获Spider抛出的异常
    10.16-arrarylist
    10.15_package_2
    10.14_package_1
    10.13_enum_2
    10.12-enum_1
    10.11-java的接口2
    10.10-3对象和类_动手动脑-java的接口
    10.9-java的封装
  • 原文地址:https://www.cnblogs.com/DrunBee/p/2651786.html
Copyright © 2020-2023  润新知