HDOJ 5044 Tree
来源:互联网 发布:mac打开mpp 编辑:程序博客网 时间:2024/06/05 21:12
Tree
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2419 Accepted Submission(s): 442
Problem Description
You are given a tree (an acyclic undirected connected graph) with N nodes. The tree nodes are numbered from 1 to N
There are N - 1 edges numbered from 1 to N - 1.
Each node has a value and each edge has a value. The initial value is 0.
There are two kind of operation as follows:
● ADD1 u v k: for nodes on the path from u to v, the value of these nodes increase by k.
● ADD2 u v k: for edges on the path from u to v, the value of these edges increase by k.
After finished M operation on the tree, please output the value of each node and edge.
There are N - 1 edges numbered from 1 to N - 1.
Each node has a value and each edge has a value. The initial value is 0.
There are two kind of operation as follows:
● ADD1 u v k: for nodes on the path from u to v, the value of these nodes increase by k.
● ADD2 u v k: for edges on the path from u to v, the value of these edges increase by k.
After finished M operation on the tree, please output the value of each node and edge.
Input
The first line of the input is T (1 ≤ T ≤ 20), which stands for the number of test cases you need to solve.
The first line of each case contains two integers N ,M (1 ≤ N, M ≤105),denoting the number of nodes and operations, respectively.
The next N - 1 lines, each lines contains two integers u, v(1 ≤ u, v ≤ N ), denote there is an edge between u,v and its initial value is 0.
For the next M line, contain instructions “ADD1 u v k” or “ADD2 u v k”. (1 ≤ u, v ≤ N, -105 ≤ k ≤ 105)
The first line of each case contains two integers N ,M (1 ≤ N, M ≤105),denoting the number of nodes and operations, respectively.
The next N - 1 lines, each lines contains two integers u, v(1 ≤ u, v ≤ N ), denote there is an edge between u,v and its initial value is 0.
For the next M line, contain instructions “ADD1 u v k” or “ADD2 u v k”. (1 ≤ u, v ≤ N, -105 ≤ k ≤ 105)
Output
For each test case, print a line “Case #t:”(without quotes, t means the index of the test case) at the beginning.
The second line contains N integer which means the value of each node.
The third line contains N - 1 integer which means the value of each edge according to the input order.
The second line contains N integer which means the value of each node.
The third line contains N - 1 integer which means the value of each edge according to the input order.
Sample Input
24 21 22 32 4ADD1 1 4 1ADD2 3 4 24 21 22 31 4ADD1 1 4 5ADD2 3 2 4
Sample Output
Case #1:1 1 0 10 2 2Case #2:5 0 0 50 4 0
Source
2014 ACM/ICPC Asia Regional Shanghai Online
#pragma comment(linker, "/STACK:102400000,102400000")#include <cstdio>#include <cstring>#include <vector>#include <algorithm>#define BUG puts("BUG is here!");using namespace std;const int N = 100010;struct node{int v, next;};int head[N];vector<node> E;int level[N], pre[N], size[N], son[N];int lct[N], top[N];int n, m, pos;int fp[2][N], addv[2][N], d[N][2];void add_edge(int u, int v) {node tmp = {v, head[u]};head[u] = E.size();E.push_back(tmp);}void dfs(int u) {size[u] = 1;son[u] = 0;for (int i = head[u]; i != -1; i = E[i].next) {int v = E[i].v;if (v != pre[u]) {pre[v] = u;level[v] = level[u] + 1;dfs(v);size[u] += size[v];if (size[son[u]] < size[v])son[u] = v;}}}void build_tree(int u, int tp) {lct[u] = ++pos;top[u] = tp;fp[0][pos] = u;if (son[u])build_tree(son[u], tp);for (int i = head[u]; i != -1; i = E[i].next) {int v = E[i].v;if (v != pre[u] && v != son[u])build_tree(v, v);}}void modify(int a, int b, int k, int op) {int p1 = top[a], p2 = top[b];while (p1 != p2) {if (level[p1] > level[p2]) {swap(p1, p2);swap(a, b);}addv[op][lct[p2]] += k;addv[op][lct[b] + 1] -= k;b = pre[p2];p2 = top[b];}if (level[a] > level[b])swap(a, b);if (op == 0) {addv[op][lct[a]] += k;addv[op][lct[b] + 1] -= k;}else {addv[op][lct[son[a]]] += k;addv[op][lct[b] + 1] -= k;}}void Init() {memset(head, -1, sizeof(head));E.clear();scanf("%d%d", &n, &m);for (int i = 1; i < n; ++i) {scanf("%d%d", &d[i][0], &d[i][1]);add_edge(d[i][0], d[i][1]);add_edge(d[i][1], d[i][0]);}pos = 0;pre[1] = -1;level[1] = 1;size[0] = 0;memset(addv, 0, sizeof(addv));dfs(1);build_tree(1, 1);for (int i = 1; i < n; ++i) {if (level[d[i][0]] < level[d[i][1]])swap(d[i][0], d[i][1]);fp[1][d[i][0]] = i;}}int main() {int T, cnt = 0;scanf("%d", &T);while (T--) {Init();char str[5];int u, v, k;for (int i = 0; i < m; ++i) {scanf("%s%d%d%d", str, &u, &v, &k);modify(u, v, k, str[3] - '1');}int ans[2][N];for (int i = 1; i <= n; ++i) {addv[0][i] += addv[0][i - 1];addv[1][i] += addv[1][i - 1];ans[0][fp[0][i]] = addv[0][i];ans[1][fp[1][fp[0][i]]] = addv[1][i];}printf("Case #%d:\n", ++cnt);printf("%d", ans[0][1]);for (int i = 2; i <= n; ++i)printf(" %d", ans[0][i]);puts("");if (n > 1)printf("%d", ans[1][1]);for (int i = 2; i < n; ++i)printf(" %d", ans[1][i]);puts("");}return 0;}
0 0
- HDOJ 5044 Tree
- HDOJ 5044 Tree
- hdoj 5044 Tree 【树链剖分】
- hdoj Tree
- 【treap tree】 HDOJ 4006
- HDOJ 4925 Apple Tree
- HDOJ 3516 Tree Construction
- HDOJ 4786 Fibonacci Tree
- hdoj 2682 Tree
- 【HDOJ 5379】 Mahjong tree
- HDOJ--2682--Tree
- HDOJ 2682 Tree
- HDOJ 5498 Tree
- hdoj 4786 Fibonacci Tree
- Hdoj 4786 Fibonacci Tree
- hdu hdoj 5571 tree
- hdoj--2682--Tree()
- HDOJ 4786 - Fibonacci Tree
- CentOS7下的btrfs文件系统
- Java连接MySQL数据库
- 背包问题:动态规划和贪心算法
- js继承如何实现继承?
- 常规FTP服务器配置案例
- HDOJ 5044 Tree
- 物联网系统的搭建(三)
- 进程、线程同步互斥学习 —— 事件
- 物联网系统的搭建(四)
- minecraft通过花生壳怎样开服?
- 学习1:Android框架
- 26-06-2015 Intro to Java Chapter 30 笔记
- Linux下解压缩包命令
- IOS--C 语言 Lesson Two Print 指针