Title Link: Http://codeforces.com/problemset/problem/593/D
---------------------------------------------------------------------------------------------------------------
Just read the topic feel must use the tree chain and other methods, then give up, however, we want to find that the topic has the following properties:
A $ long long $ within the number if the number is greater than $1$ dozens of times can be removed to $0$
So we can consider the $lca$.
But if the length of the $1$ side too much complexity is not reliable then we will observe the modification of the length of the edge is only reduced
So we can do path compression for $1$-length edges.
But the compression here does not change the structure of the original tree because changing the structure of the tree can be a lot of trouble.
All we need to do is put a point connected to the length of the $1$ in a set, if a point within the collection needs to look up
We'll just find the top of the collection.
$ ($ Although the point at the top of the collection is not necessarily a two-point $lca $ but because the side length in the collection are $1$ actual results are equivalent $) $
1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5 using namespacestd;6 Const intN = 2e5 +Ten;7 intFirste[n], Nexte[n <<1], V[n <<1];8 Long LongW[n <<1];9 intFa[n], d[n], aim[n], fa2[n];Ten Long LongDist[n]; One intN, m, E =1; A voidBuild_edge (intXintYLong Longz) - { -Nexte[++e] =Firste[x]; theFIRSTE[X] =e; -V[e] =y; -W[e] =Z; - } + intFINDF (intx) - { + if(Fa2[x] = =x) A returnx; at returnFA2[X] =findf (fa2[x]); - } - voidDfsintu) - { - for(intp = Firste[u]; P p =Nexte[p]) - if(!Fa[v[p]]) in { -Aim[p/2] =V[p]; toFA[V[P]] =u; +D[V[P]] = D[u] +1; -DIST[V[P]] =W[p]; the if(W[p] = =1) *FA2[V[P]] =findf (u); $ DFS (v[p]);Panax Notoginseng } - } the Long LongCheckintXintYLong Longz) + { A while(X! =y) the { + if(D[x] <D[y]) - swap (x, y); $ if(Dist[x] = =1) $x =findf (x); - Else - { theZ/=Dist[x]; -x =Fa[x];Wuyi if(!z) the returnZ; - } Wu } - returnZ; About } $ intMain () - { -scanf"%d%d", &n, &m); - intx, y; A Long LongZ; + for(inti =1; I < n; ++i) the { -scanf"%d%d%lld", &x, &y, &z); $ Build_edge (x, y, z); the Build_edge (y, x, z); the } thefa[1] =1; thedist[1] =1; - for(inti =1; I <= N; ++i) inFa2[i] =i; theDfs1); the while(m--) About { thescanf"%d", &x); the if(X &1) the { +scanf"%d%d%lld", &x, &y, &z); -printf"%lld\n", check (x, Y, z)); the }Bayi Else the { thescanf"%d%lld", &x, &z); - intU =Aim[x]; -Dist[u] =Z; the if(Z = =1) theFa2[u] =findf (Fa[u]); the } the } - return 0; the}
Codeforces 593D Happy Tree Party