Count on the path
Time Limit: 5000/2500 MS (Java/others) memory limit: 131072/131072 K (Java/Others)
Total submission (s): 92 accepted submission (s): 10
Problem descriptionbobo has a tree, whose vertices are conveniently labeled by 1, 2 ,..., N.
Let f (a, B) be the minimum of vertices
NotOn the path between vertices A and B.
There are Q queries (ui, vi) for the value of F (ui, vi). Help Bobo answer them.
Inputthe input consists of several tests. For each tests:
The first line contains 2 integers n, Q (4 ≤ n ≤ 106, 1 ≤ q ≤ ). each of the following (n-1) lines contain 2 integers AI, Bi denoting an edge between vertices AI and Bi (1 ≤ AI, Bi ≤ n ). each of the following Q lines contains 2 integer u'' I, v'' I (1 ≤ UI, VI ≤ n ).
The queries are encrypted in the following manner.
U1 = u'1, V1 = V' 1.
For I ≥ 2, UI = u' I fill F (ui-1, vi-1), Vi = V 'I fill F (ui-1, vi-1 ).
Note your denotes bitwise exclusive-or.
It is guaranteed that f (a, B) is defined for all A, B.
The task contains huge inputs. 'scanf' in G ++ is considered too slow to get accepted. you may (1) Submit the solution in C ++; or (2) Use hand-written input utilities.
Outputfor each tests:
For each queries, a single number denotes the value.
Sample Input
4 11 21 31 42 35 21 21 32 42 51 27 6
Sample output
431
Authorxiaoxu Guo (ftiasch)
Given a tree, find the minimum number without passing through the path.
Take 1 as the root, and then add 1 without passing through, then the answer is 1 directly, otherwise it is preprocessing,
The data size is very large, so only BFS can be used, and a read/write plug-in is required. The specific process code is explained in detail.
Code:
/*************************************** * ******** Author: rabbitcreated time: 2014/8/6 10: 44: 17 file name: 5. CPP *************************************** * ********/# pragma comment (linker, "/Stack: 102400000,102400000") # include <stdio. h> # include <iostream> # include <algorithm> # include <sstream> # include <stdlib. h> # include <string. h> # include <limits. h> # include <string> # include <time. h> # include <math. h> # In Clude <queue> # include <stack> # include <set> # include <map> using namespace STD; # define INF 0x3f3f3f3f # define EPS 1e-8 # define PI ACOs (-1.0) typedef long ll; int fun () {char ch; int flag = 1, a = 0; while (CH = getchar ()) if (CH> = '0' & Ch <= '9') | CH = '-') break; If (CH = '-') flag =-1; else a = CH-'0'; while (CH = getchar () {If (CH> = '0' & Ch <= '9 ') A = 10 * A + CH-'0'; else break;} return flag * A;} const int maxn = 1001000; int head [Maxn], Tol; int subtree [maxn]; // minimum subtree label int belong [maxn]; // The minimum subtree label associated with root node 1. Int child [maxn] [4]; // The first 4 digits of the Child tree. Int que [maxn]; // query queue. Int path [maxn]; // path [u] indicates the minimum number from the root node to the U path in the subtree of the son of Root Node 1. Int Fa [maxn]; // father ID. Int Dep [maxn]; // depth array struct edge {int next, to;} edge [2 * maxn]; void addedge (int u, int V) {edge [tol]. to = V; edge [tol]. next = head [u]; head [u] = tol ++;} int main () {// freopen ("data. in "," r ", stdin); // freopen (" data. out "," W ", stdout); int n, m; while (~ Scanf ("% d", & N, & M) {memset (Head,-1, sizeof (head); Tol = 0; for (INT I = 1; I <n; I ++) {int U, V; u = fun (); V = fun (); addedge (u, v ); addedge (v, U);} int front = 0, rear = 0; Dep [1] = 0; Fa [1] =-1; que [rear ++] = 1; while (front! = Rear) {int u = que [Front ++]; for (INT I = head [u]; I! =-1; I = edge [I]. next) {int v = edge [I]. to; If (V = Fa [u]) continue; Dep [v] = Dep [u] + 1; Fa [v] = u; que [rear ++] = V ;}}for (INT I = 1; I <= N; I ++) for (Int J = 0; j <4; j ++) child [I] [J] = inf; For (INT I = rear-1; I> = 0; I --) {int u = que [I]; subtree [u] = min (u, child [u] [0]); int P = Fa [u]; If (P =-1) continue; child [p] [3] = subtree [u]; sort (child [p], child [p] + 4);} front = 0, rear = 0; path [1] = inf; belong [1] =-1; for (INT I = head [1]; I! =-1; I = edge [I]. next) {int u = edge [I]. to; path [u] = inf; belong [u] = subtree [u]; que [rear ++] = u ;}while (front! = Rear) {int u = que [Front ++]; for (INT I = head [u]; I! =-1; I = edge [I]. next) {int v = edge [I]. to; If (V = Fa [u]) continue; path [v] = min (path [u], child [u] [subtree [v] = Child [u] [0]); belong [v] = belong [u]; que [rear ++] = V;} path [u] = min (path [u], child [u] [0]); // The minimum number of the Child tree of U.} Int last = 0; while (M --) {int U, V; u = fun (); V = fun (); U ^ = last; V ^ = last; if (u> V) Swap (u, v); If (u! = 1 & belong [u] = belong [v]) Last = 1; // assume that the child tree does not pass through 1 and belongs to the son of the same root node, the answer is 1 else {int I = 0; while (child [1] [I] = belong [u] | child [1] [I] = belong [v]) I ++; // skip the child tree that contains u and v. Last = u = 1? Path [v]: min (path [u], path [v]); // The path is divided into two sections, taking the minimum value. Last = min (last, child [1] [I]); // minimum value other than the subtree where v is located.} Printf ("% d \ n", last) ;}} return 0 ;}
HDU 4916 tree DP