Network HDU - 3078
The usual way to measure connecting speed is lag, or network latency, referring the time taken for a sent packet of data to be received at the other end.
Now the network is on trial, and new photonic crystal fibers designed by ALPC42 is trying out, the lag on fibers can be ignored. That means, lag happened when message transport through the router. ALPC42 is trying to change routers to make the network faster, now he want to know that, which router, in any exactly time, between any pair of nodes, the K-th high latency is. He needs your help.
InputThere are only one test case in input file.
Your program is able to get the information of N routers and N-1 fiber connections from input, and Q questions for two condition: 1. For some reason, the latency of one router changed. 2. Querying the K-th longest lag router between two routers.
For each data case, two integers N and Q for first line. 0<=N<=80000, 0<=Q<=30000.
Then n integers in second line refer to the latency of each router in the very beginning.
Then N-1 lines followed, contains two integers x and y for each, telling there is a fiber connect router x and router y.
Then q lines followed to describe questions, three numbers k, a, b for each line. If k=0, Telling the latency of router a, Ta changed to b; if k>0, asking the latency of the k-th longest lag router between a and b (include router a and b). 0<=b<100000000.
A blank line follows after each case.
OutputFor each question k>0, print a line to answer the latency time. Once there are less than k routers in the way, print "invalid request!" instead.Sample Input
5 5 5 1 2 3 4 3 1 2 1 4 3 5 3 2 4 5 0 1 2 2 2 3 2 1 4 3 3 5
Sample Output
3 2 2 invalid request!
题解:单组case,适合Sparse Table来解。
1 #include <algorithm> 2 #include <cctype> 3 #include <cmath> 4 #include <cstdio> 5 #include <cstdlib> 6 #include <cstring> 7 #include <ctime> 8 #include <iomanip> 9 #include <iostream> 10 #include <map> 11 #include <queue> 12 #include <string> 13 #include <set> 14 #include <vector> 15 using namespace std; 16 17 const int N = 8e4 + 10; 18 19 vector<int> G[N]; 20 21 int n, q, dfsNum; 22 int vs[N << 1], dep[N << 1], first[N], Pa[N], va[N]; 23 24 struct cmp { 25 bool operator ()(int &a, int &b) { 26 return a < b; 27 } 28 }; 29 30 void dfs(int u, int f, int d) { 31 Pa[u] = f; 32 vs[++dfsNum] = u; 33 dep[dfsNum] = d; 34 first[u] = dfsNum; 35 for (int i = 0; i < G[u].size(); ++i) { 36 int v = G[u][i]; 37 if (v == f) continue; 38 dfs(v, u, d + 1); 39 vs[++dfsNum] = u; 40 dep[dfsNum] = d; 41 } 42 } 43 44 inline int getMin(int x, int y) { 45 return dep[x] < dep[y] ? x : y; 46 } 47 48 struct SparseTable { 49 int dp[20][N << 1]; 50 void init(int n) { 51 for (int i = 1; i <= n; ++i) dp[0][i] = i; 52 for (int i = 1; (1 << i) <= n; ++i) 53 for (int j = 1; j + (1 << i) - 1 <= n; ++j) 54 dp[i][j] = getMin(dp[i - 1][j], dp[i - 1][j + (1 << i - 1)]); 55 } 56 int RMQ(int l, int r) { 57 int k = 31 - __builtin_clz(r - l + 1); 58 return getMin(dp[k][l], dp[k][r - (1 << k) + 1]); 59 } 60 } st; 61 62 inline int lca(int u, int v) { 63 if (first[u] > first[v]) swap(u, v); 64 int idx = st.RMQ(first[u], first[v]); 65 return vs[idx]; 66 } 67 68 void Inite() { 69 dfsNum = 0; 70 dfs(1, -1, 0); 71 st.init(2 * n - 1); 72 } 73 74 void Solve(int u, int v, int k) { 75 int Lca = lca(u, v); 76 priority_queue<int, vector<int>, cmp>q; 77 while (u != Lca) { 78 q.push(va[u]); 79 u = Pa[u]; 80 } 81 while (v != Lca) { 82 q.push(va[v]); 83 v = Pa[v]; 84 } 85 q.push(va[Lca]); 86 for (int i = 1; (i < k) && (!q.empty()); i++) q.pop(); 87 if (q.empty()) printf_s("invalid request!\n"); 88 else printf_s("%d\n", q.top()); 89 } 90 91 int main() 92 { 93 scanf_s("%d%d", &n, &q); 94 for (int i = 1; i <= n; i++) scanf_s("%d", va + i); 95 for (int i = 2; i <= n; i++) { 96 int u, v; 97 scanf_s("%d%d", &u, &v); 98 G[u].push_back(v); 99 G[v].push_back(u); 100 } 101 Inite(); 102 for (int i = 1; i <= q; i++) { 103 int k, u, v; 104 scanf_s("%d%d%d", &k, &u, &v); 105 if (k == 0) va[u] = v; 106 else Solve(u, v, k); 107 } 108 }

浙公网安备 33010602011771号