用树链剖分求LCA的模板: 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 const int maxn=40005; 5 int n,m; 6 int head[maxn],to[maxn<<1],w[maxn<<1],nxt[maxn<<1],tot; 7 int fa[maxn],dep[maxn],son[maxn],top[maxn],size[ma…
Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. Node x is an ancestor of…
Code: #include<cstdio> #include<cstring> #include<vector> #include<algorithm> using namespace std; const int maxn=40000+3; typedef long long ll; ll dis[maxn]; int son[maxn],siz[maxn],rank1[maxn],p[maxn],top[maxn]; struct Edge { int…
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each node is labeled with an…