Question link: Click the open link
Question:
Gave a tree
Each vertex has a permission
Operation 1: 1 U indicates asking gcd (valueof (u), valueof (V ))! The point with the largest depth among all vertices of 1
[V is path (u, root); & V! = U]
Operation 2: 2 u w modify point permission
Ideas:
Because the number of operation 2 does not exceed 50, all the answers are pre-processed after each point right update. The answer is O (1), and the update is N * logn.
Each time the answer is calculated in DFS and the prime number stack is updated, the sub-tree of DFS is deleted from the stack. This ensures that the path to the root is stored in the stack at any time.
= Wrong in the meaning of the question. I started to think of it as the biggest vertex right. In fact, I can do it. I can maintain the prime number stack as monotonous, = It is said that any two operations can also be done .. I don't know what to do ..
# Include <stdio. h> # include <string. h >#include <iostream> # include <vector> # include <map> # include <algorithm> using namespace STD; # define N using 5int prime [1229] = {2, 3, 5, 7, 97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331, 33 7,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991, 99, 1283,1289, 1291,1297, 1301,1303, 1307,1319, large, large, 1373,1381, large, medium, small, medium, large, 1459,1471, 1481,1483, medium, small, medium, small, small, medium, 1559,1567, 157, 1579, 1583,1597, 1601,1607, 1609,1613, clerk, 1693,1697, clerk, 1747,1753, 1759,1777, clerk, 1871,1873,, 220 3,2207, 2213,2221, 2237,2239, clerk, 2297,2309, 2311,2333, clerk, 2411,2417, 2423,2437, clerk, clerk, 2473,2477, 2503,2521, 2531,2539, 2543,2549, 2551,2557, clerk, 2693,2699, 2707,2711, clerk, 2791,2797, 280, 2819,2833, 2837,2843, 2851,2857, 2861,2879, large, small, medium, large, small, medium, 3011,3019, 3023,3037, large, small, medium, 3137,20.3,, 349, 3,, 412, Clerk, clerk, 4513,4517, clerk, 4547, clerk, clerk, 4583,4591, clerk, 4751,4759, 4783,4787, 4789,4793, clerk, clerk, 481, Clerk, clerk, clerk, 5227,5231, 5233,5237, clerk, clerk, 5297,5303, clerk, 5443,5449, 5471,5477, 5479,5483, 5501,5503, 550 7,5519, 5521,5527, 5531,5557, clerk, 5717, 5737,5741, clerk, 5821,5827, 5839,5843, listen, 5857,5861, 5867,5869, listen, listen, 5923,5927, 5939,5953, 5981,5987, 6007,6011, clerk, 6199,6203, 621, Listen, 6571,6577, 6581,6599, 6607,6619, 6637,6653, clerk, 6833,6841, 6857,6863, 6869,6871, clerk, clerk, 690, Large, small, medium, small, large, 6997,7001, 7013,7019, 7027,7039, 7043,7057, 7069,7079, 7103,7109, large, large, small, large, 7207,7211, large, small, small, average, minimum, maximum, 7331,7333, minimum, maximum, 7411,7417, 7433,7451, maximum, minimum, maximum, minimum, 7517,7523, minimum, maximum, minimum, 7561,7573, 7577,7583, 7589,7591, minimum, 762, Large, large, small, medium, large, small, large, 7853,7867, 7873,7877, large, large, small, 7933,7937, 7949,7951, large, small, listen, 8017,8039, 8053,8059, 8069,8081, 8087,8089, 8093,8101, clerk, 8297,8311, 8317,8329, clerk, 836 Listen, listen, 8419,8423, 8429,8431, 8443,8447, 8461,8467, 8501,8513, 8521,8527, clerk, 8713,8719,, 909 ,, 979 ,}; Int N, Q, ANS [N], deep [N]; vector <int> G [N], V [N]; // v [I] is all the quality factors of the I point, and g [I] is a graph, P [u] And d [u] are the points Map <int, vector <int> P; void DFS (int u, int F, int dep) with the U factor) {ans [u] =-1; deep [u] = Dep; For (INT I = (INT) V [u]. size ()-1; I> = 0; I --) {int v = V [u] [I]; If (P [v]. size () = 0) {P [v]. push_back (U); continue;} If (ANS [u] =-1 | deep [ans [u] <deep [p [v] [p [v]. size ()-1]) ans [u] = P [v] [p [v]. size ()-1]; P [v]. push_back (U);} For (INT I = (INT) g [u]. size ()-1; I> = 0; I --) if (G [u] [I]! = F) DFS (G [u] [I], U, DEP + 1); For (INT I = (INT) V [u]. size ()-1; I> = 0; I --) {int v = V [u] [I]; P [v]. erase (P [v]. end ()-1) ;}} void fenjie (int u, int v) {v [u]. clear (); For (Int J = 0; prime [J] * prime [J] <= V; j ++) if (V % prime [J] = 0) {v [u]. push_back (prime [J]); While (V % prime [J] = 0) V/= prime [J];} If (V! = 1) V [u]. push_back (V);} void input () {for (int v, I = 1; I <= N; I ++) {scanf ("% d ", & V); G [I]. clear (); fenjie (I, V) ;}for (int u, V, I = 1; I <n; I ++) {scanf ("% d", & U, & V); G [u]. push_back (V); G [v]. push_back (u) ;}} int main () {int U, V, OP; while (~ Scanf ("% d", & N, & Q) {input (); DFS (1,-1, 0); While (Q --) {scanf ("% d", & OP, & U); If (OP = 1) printf ("% d \ n", ANS [u]); else {scanf ("% d", & V); fenjie (u, v); DFS (1,-1, 0) ;}} return 0 ;}
Codeforces 463e Caisa and tree DFS + decomposition quality factor