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
#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
- SPOJ QTREE4Query on a tree IV
- SPOJ QTREE4 Query on a tree IV (边分治 + 堆)
- SPOJ 2666 QTREE4 - Query on a tree IV
- SPOJ QTREE4 Query on a tree IV(边分治)
- SPOJ-QTREE4 Query on a tree IV(边分治)
- SPOJ Query on a tree
- SPOJ QTREEQuery on a tree
- SPOJ Count on a tree
- SPOJ Query on a tree
- Count on a tree SPOJ
- SPOJ Query on a tree
- Count on a tree SPOJ
- Query on a tree SPOJ
- Query on a tree SPOJ
- [SPOJ 375]Query On a Tree(树链剖分)
- SPOJ 375. Query on a tree【树链剖分】
- SPOJ 375. Query on a tree【树链剖分】
- spoj cot Count on a tree
- redis-c api
- centos中分析java占用大量CPU资源的原因
- java程序优化
- 搁置已久的XD_JWXT的wp
- margin:0 auto无效原因
- SPOJ QTREE4Query on a tree IV
- 添加环境变量
- 什么是类?什么是对象?类和对象有什么关系?
- HDU 3932 Groundhog Build Home (模拟退火算法)
- spring MVC权限分配的实现
- ko驱动模块rmmod 后insmod 异常问题解决
- opencv图像写入视频详解
- jQuery使用ajaxSubmit()提交表单以及AjaxSubmit的一些用法
- struts2 配置文件的跳转总结