SPOJ QTREE4Query on a tree IV

来源:互联网 发布:红色高棉 知乎 编辑:程序博客网 时间:2024/06/03 20:13

Description

You are given a tree (an acyclic undirected connected graph) with N nodes, and nodes numbered 1,2,3...,N. Each edge has an integer value assigned to it(note that the value can be negative). Each node has a color, white or black. We define dist(a, b) as the sum of the value of the edges on the path from node a to node b.

All the nodes are white initially.

We will ask you to perfrom some instructions of the following form:

  • C a : change the color of node a.(from black to white or from white to black)
  • A : ask for the maximum dist(a, b), both of node a and node b must be white(a can be equal to b). Obviously, as long as there is a white node, the result will alway be non negative.

Input

  • In the first line there is an integer N (N <= 100000)
  • In the next N-1 lines, the i-th line describes the i-th edge: a line with three integers a b c denotes an edge between a, b of value c (-1000 <= c <= 1000)
  • In the next line, there is an integer Q denotes the number of instructions (Q <= 100000)
  • In the next Q lines, each line contains an instruction "C a" or "A"

Output

For each "A" operation, write one integer representing its result. If there is no white node in the tree, you should write "They have disappeared.".

Example

Input:31 2 11 3 17AC 1AC 2AC 3AOutput:220They have disappeared.

Some new test data cases were added on Apr.29.2008, all the solutions have been rejudged.

Hint

Added by:Qu JunDate:2008-04-25Time limit:0.100s-0.646sSource limit:50000BMemory limit:1536MBCluster:Cube (Intel G860)Languages:All except: C99 strict ERL JS NODEJS PERL 6 VB.netResource:williambzoj1095的进阶版,时限卡的有点紧,点分治必须各种优化才能过。

#include<set>#include<queue>#include<cstdio>#include<vector>#include<iostream>#include<algorithm>using namespace std;const int low(int x) { return x&-x; }const int INF = 0x7FFFFFFF;const int maxn = 2e5 + 10;int n, x, y, z, f[maxn], cnt;char ch[maxn];struct Tree{int ft[maxn], nt[maxn], u[maxn], v[maxn], sz;int FT[maxn], NT[maxn * 10], U[maxn * 10], V[maxn * 10], SZ;int mx[maxn], ct[maxn], vis[maxn];struct heap{priority_queue<int> p, q;void clear(){while (!p.empty()) p.pop();while (!q.empty()) q.pop();}void add(int x) { p.push(x); }void del(int x) { q.push(x); }int top(){while (true){if (p.empty()) return -INF;else if (!q.empty() && p.top() == q.top()) p.pop(), q.pop();else return p.top();}}int toptwo(){int x = top();del(x);int y = top();add(x);if (y == -INF) return x == -INF ? x : 0;else return max(x + y, 0);}};heap p[maxn], pp[maxn], ans;void clear(int n){mx[SZ = sz = 0] = INF;for (int i = 1; i <= n; i++){FT[i] = ft[i] = - 1;f[i] = 1; vis[i] = 0;}}void AddEdge(int x, int y, int z){u[sz] = y;v[sz] = z; nt[sz] = ft[x];ft[x] = sz++;u[sz] = x;v[sz] = z; nt[sz] = ft[y];ft[y] = sz++;}void ADDEDGE(int x, int y, int z){U[SZ] = y;V[SZ] = z;NT[SZ] = FT[x];FT[x] = SZ++;}int dfs(int x, int fa, int sum){int y = mx[x] = (ct[x] = 1) - 1;for (int i = ft[x]; i != -1; i = nt[i]){if (vis[u[i]] || u[i] == fa) continue;int z = dfs(u[i], x, sum);ct[x] += ct[u[i]];mx[x] = max(mx[x], ct[u[i]]);y = mx[y] < mx[z] ? y : z;}mx[x] = max(mx[x], sum - ct[x]);return mx[x] < mx[y] ? x : y;}void find(int rt, int x, int fa, int len){ADDEDGE(x, rt, len);for (int i = ft[x]; i != -1; i = nt[i]){if (vis[u[i]] || u[i] == fa) continue;find(rt, u[i], x, len + v[i]);}}void get(int rt, int x, int fa, int len){pp[rt].add(len);for (int i = ft[x]; i != -1; i = nt[i]){if (vis[u[i]] || u[i] == fa) continue;get(rt, u[i], x, len + v[i]);}}int build(int x, int sum){int y = dfs(x, -1, sum);find(y, y, -1, 0);vis[y] = 1;p[y].clear();p[y].add(0);for (int i = ft[y]; i != -1; i = nt[i]){if (vis[u[i]]) continue;int z = build(u[i], ct[u[i]] > ct[y] ? sum - ct[y] : ct[u[i]]);pp[z].clear(); get(z, u[i], y, v[i]);p[y].add(pp[z].top());}ans.add(p[y].toptwo()); vis[y] = 0;return y;}void change(int x){if (f[x] ^= 1) ++cnt; else --cnt;int a = p[x].toptwo(), b, c, d;f[x] ? p[x].add(0) : p[x].del(0);b = p[x].toptwo();if (a != b) ans.del(a), ans.add(b);for (int i = FT[x]; NT[i] != -1; i = NT[i]){int y = U[i], z = U[NT[i]], len = V[NT[i]];a = pp[y].top();f[x] ? pp[y].add(len) : pp[y].del(len);b = pp[y].top();if (a != b){c = p[z].toptwo();p[z].del(a), p[z].add(b);d = p[z].toptwo();if (c != d) ans.del(c),ans.add(d);}}}}solve;int main(){while (scanf("%d", &n) != EOF){solve.clear(cnt = n);for (int i = 1; i < n; i++){scanf("%d%d%d", &x, &y, &z);solve.AddEdge(x, y, z);}solve.build(1, n);scanf("%d", &x);while (x--){scanf("%s", ch);if (ch[0] == 'A'){if (cnt) printf("%d\n", solve.ans.top());else printf("They have disappeared.\n");}else{scanf("%d", &y);solve.change(y);}}}return 0;}

0 0
原创粉丝点击