Codeforces 463E Caisa and Tree dfs + decomposition quality factor
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
# Include
# Include
# Include
# Include
# Include using namespace std; # define N Limit 5int prime [1229] = {, 71, 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,337,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,997,100,, listen, 1579,1583, 1597,1601, 1607,1609, 1613,1619, clerk, 1697,1699, clerk, 1801,1811, clerk, 1847,1861, 1867,1871, values, numbers, numbers, values, listen, 2521,2531, 2539,2543, 2549,2551, 2557,2579, 2591,2593, clerk, 2767,2777, 2789,2791, california, 2803,2819, 2833,2837, 2843,2851, 2857,2861, 2879,2887, California, 3011, California, 3083,3089, California, 3121,3104,,,, keys, numbers, numbers, hour, hour, 4591,4597, 4603,4621, 4637,4639, hour 7, 4663,4673, hour, 4703,4721, hour, 4733,4751, hour, listen, 4817,4831, 4861,4871, 4877,4889, clerk, 4919,4931, 4933,4937, clerk, 5099,5101, 5107,5113, clerk, clerk, clerk, clerk, 5231,5233, clerk, 5303,5309, 5323,5333, clerk, 5431,5437, 5441,5443, 5449,5471, 5477,5479, 5483,5501,, listen, 5851,5857, 5861,5867, 5869,5879, clerk, 5903,5923, 5927,5939, 5953,5981, clerk, 6131,6133, clerk, clerk,, listen, 62.16,6571, 6577,6581, clerk, clerk, 6 clerk, 6691,6701, clerk, 6719,6733, 6737,6761, clerk, 6781,6791, clerk, 6863,6869, hour, hour, 6911,6917, 6947,6949, 6959,6961, 6967,6971, hour, hour, 7001,7013, hour, 7211,7213, 7219,7229, clerk, clerk, 7433, clerk, 7547,7, clerk, 7573,7577, 7583,7589, listen, 7607, 7621, 7639,7643, 7649,7669, clerk, 7883, clerk, 7907,7919, 7927,7933, 7937,7949, listen, 7993,8009, 8011,8017, 8039,8053, 8059,8069, 8081,8087, 8089,8093, clerk, 8311,8317, california, 8423,8429, 8431,8443, 8447,8461, California, California, 8527,8537, California, Connecticut, 8647,8663, 8669,8677, California, 8693,8699,, listen, 9067,9091, 9103,9109, 9127,9385, clerk, 9157,9161, clerk, 9221,9227, clerk, 9257,9277, clerk, 9403,9413, california, 9431,9433, 9437,9439, 9461,9463, California, California, 9497,9511, California, large, small, medium, large, 9721,9733, 9739,9743, 9749,9767, records, 9787,9791, 9803,9811, 9817,9829, 9833,9839, 9851,9857, 9859,9871, 9883,9887, numbers, numbers, 9931,9941, numbers, 9973}; int n, q, ans [N], deep [N]; vector
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 points map with the u factor.
> P; void dfs (int u, int f, int dep) {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]
= 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 ;}