HDU5274 Dylans loves tree (tree link separation) is a clever vertex weight update, hdu5274dylans

Source: Internet
Author: User

HDU5274 Dylans loves tree (tree link separation) is a clever vertex weight update, hdu5274dylans
Dylans loves treeAccepts: 49 Submissions: 262 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Description

Dylans has one
 
  
N
 Vertex Tree. Each vertex has a permission. The node number on the tree is
 
  
1 limit N
 . He got
 
  
Q
 Queries in the following format: ①
 
  
0 x y
 :
 
  
X
 Point permissions of vertices are modified
 
  
Y
 . ②
 
  
1 x y
 :
 
  
X y
 Does each vertex permission on the path appear an even number of times? Make sure that the number of occurrences of only one point permission on the path of each query is odd.
 
  
1 ≤ N, Q ≤ 100000
 , Point right
 
  
A [I] ε N
 , And both
 
  
≤ 100000
 
Input description
The first row is a positive integer.
 
  
T
 Indicates the number of data groups (
 
  
T ≤ 3
 And there is only one group of data at most.
 
  
N> 1000
 ) Number of the first row
 
  
N, Q
 Number of points and number of inquiries in the tree. Next
 
  
N−1
 One-to-one row
 
  
(X, y)
 Indicates an edge on the tree. Next line
 
  
N
 The number of vertices indicates the point right of each vertex. Next
 
  
Q
 Three rows per row
 
  
(Opt, x, y)
 Ask.
Output description
For each ② query, if all values are even numbers, "-1" is output; otherwise, an odd value is output.
Input example
13 21 22 31 1 11 1 21 1 3
Output example
-11
Hint
In the hack data, N and Q must be less than or equal to 10000, and there should be no extra spaces at the end of each row.
Solution:
 
# Pragma comment (linker, "/STACK: 1024000000,1024000000") # include <stdio. h> # include <iostream> # include <string. h >#include <map> using namespace std; const int N = 100005; # define LL _ int64struct EDG {int to, next;} edg [N * 2]; int eid, head [N]; int fath [N], deep [N], top [N], num [N], son [N], p [N], pos; void init () {pos = 0; eid = 0; memset (head,-1, sizeof (head);} void addEdg (int u, int v) {edg [eid]. to = v; edg [eid]. next = head [u]; h Ead [u] = eid ++; edg [eid]. to = u; edg [eid]. next = head [v]; head [v] = eid ++;} void dfs (int u, int pre, int d) {fath [u] = pre; num [u] = 1; deep [u] = d; son [u] =-1; for (int I = head [u]; I! =-1; I = edg [I]. next) {int v = edg [I]. to; if (v = pre) continue; dfs (v, u, d + 1); num [u] + = num [v]; if (son [u] =-1 | num [son [u] <num [v]) son [u] = v ;}} void getpos (int u, int root) {top [u] = root; p [u] = ++ pos; if (son [u] =-1) return; getpos (son [u], root); for (int I = head [u]; I! =-1; I = edg [I]. next) {int v = edg [I]. to; if (son [u] = v | v = fath [u]) continue; getpos (v, v) ;}} int root [N * 3]; // record the xorint a [N] Of all values in the current range; void build (int l, int r, int k) {if (l = r) {root [k] = a [l]; return;} int m = (l + r)> 1; build (l, m, k <1 ); build (m + 1, r, k <1 | 1); root [k] = root [k <1] ^ root [k <1 | 1];} void updata (int l, int r, int k, int id, int vule) {if (l = r) {root [k] = vule; return ;} int m = (l + r)> 1; if (id <= m) updata (l, m, K <1, id, vule); else updata (m + 1, r, k <1 | 1, id, vule ); root [k] = root [k <1] ^ root [k <1 | 1];} int query (int l, int r, int k, int L, int R) {if (L <= l & r <= R) {return root [k];} int m = (l + r)> 1, ans = 0; if (L <= m) ans ^ = query (l, m, k <1, L, R); if (m <R) ans ^ = query (m + 1, r, k <1 | 1, L, R); return ans;} void swp (int & aa, int & bb) {int tt = aa; aa = bb; bb = tt;} int Operat (int u, int v) {int fu = top [u], fv = top [v], ans = 0; while (fu! = Fv) {if (deep [fu] <deep [fv]) {swp (fu, fv); swp (u, v);} ans ^ = query (1, pos, 1, p [fu], p [u]); u = fath [fu]; fu = top [u];} if (deep [u]> deep [v]) swp (u, v); ans ^ = query (1, pos, 1, p [u], p [v]); return ans;} int main () {int T, n, q, val [N], u, v; scanf ("% d ", & T); while (T --) {scanf ("% d", & n, & q); init (); for (int I = 1; I <n; I ++) {scanf ("% d", & u, & v); addEdg (u, v) ;}for (int I = 1; I <= n; I ++) {scanf ("% d", & val [I]); val [I] ++;} dfs (, 1 ); getpos (1, 1); for (int I = 1; I <= n; I ++) a [p [I] = val [I]; build (1, pos, 1); while (q --) {int op, x, y; scanf ("% d", & op, & x, & y ); if (op = 0) updata (1, pos, 1, p [x], y + 1); // Add 1 else {printf ("% d \ n ", operat (x, y)-1); // minus 1 }}return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.