# "Codeforces" E. Xenia and Tree (sub-block + LCA)

Source: Internet
Author: User

For two operations, for Operation 1, save the points to be modified each time, until the sqrt (m) point is saved, and the diagram is rebuilt once (the distance from each point to the nearest staining point is re-recorded)

For Operation 2, use the LCA to find the current point (i.e., the point that has not been updated to the graph) and the distance between the point and the point in the current graph and the nearest stain.

It's a good thing to say about LCA = =! Fill in these days.

The puzzle is here: http://codeforces.com/blog/entry/8800

`#include <cmath> #include <queue> #include <cstdio> #include <vector> #include <cstring># include<algorithm>using namespace Std;const int maxn = 100005;const int INF = 9999999;int N,m;vector<int>g[ma Xn];int pa[maxn][25];int dist[maxn];int deep[maxn];int vis[maxn];//--------------------    LCA---------------------------//multiplication method Lac for nearest public ancestor void Dfs (int pos,int d) {Deep[pos] = D;    int Size = G[pos].size (); for (int i = 0; i < Size; i++) DFS (g[pos][i],d + 1);} void Lca_init () {for (int j = 1; J <=; J + +) for (int i = 1; I <= n; i++) if (pa[i][j-1]! =-1 ) Pa[i][j] = pa[pa[i][j-1]][j-1];}    int LCA (int a,int b) {int J;    if (Deep[a] < deep[b]) swap (A, b);            Speak AB at the same depth for (int j = k; j >= 0; j--) {if (pa[a][j]! = 1 && deep[pa[a][j]] >= deep[b])    A = Pa[a][j];    }//if (a = = b) return b; Multiplication method for (int j = 0; J >=; j--) {if (pa[a][j]! =-1 && pa[a][j]! = Pa[b][j]) {a = Pa[a][j];        b = Pa[b][j]; }} return pa[a][0];}    --------------------------------------------------void BFs () {queue<int>q;    int HAVE[MAXN] = {0};            for (int i = 1; I <= n; i++) if (Vis[i]) {//If staining q.push (i);            Have[i] = 1;        Dist[i] = 0;        } while (!q.empty ()) {Int. now = Q.front (); Q.pop ();        int Size = G[now].size ();            for (int i = 0; i < Size; i++) if (!have[g[now][i]]) {Have[g[now][i]] = 1;            Dist[g[now][i]] = Dist[now] + 1;        Q.push (G[now][i]);            } if (pa[now][0]! =-1 &&!have[pa[now][0]]) {have[pa[now][0]] = 1;            Dist[pa[now][0]] = Dist[now] + 1;        Q.push (Pa[now][0]); }}}//--------------------------------------------------vector<int>vv;void init () {for (int i = 1; I <= n; i    + +) G[i].clear ();    Vv.clear (); memset (vis,-1,sizeof (Vis)); memset (Pa,-1,sizeof (PA));}        int main () {while (scanf ("%d%d", &n,&m)! = EOF) {init ();            for (int i = 0; i < n-1; i++) {int x, y;            scanf ("%d%d", &x,&y);            G[x].push_back (y);            Pa[y][0] = x;        Vis[y] = 0;            }//Find the root node and get the deep array for (int i = 1; I <= n; i++) if (vis[i] = = 1) {pa[i][0] = 1;            Vis[i] = 0;            VIS[1] = 1;            DFS (i,0);        Break        } lca_init ();        BFS ();        int calc = sqrt (m) + 1;            for (int i = 0; i < m; i++) {int op,v;            scanf ("%d%d", &op,&v);                if (op = = 1) {vv.push_back (v);                int S = Vv.size ();                    if (s = = Calc) {for (int i = 0; i < S; i++) vis[vv[i]] = 1;                    BFS ();                Vv.clear (); }} else{int S = Vv.sizE ();                int ans = dist[v];                    for (int i = 0; i < S; i++) {int c = LCA (V,vv[i]);                    int d = (Deep[v]-deep[c]) + (Deep[vv[i]]-deep[c]);                ans = min (ans,d);            } printf ("%d\n", ans); }}} return 0;}`

"Codeforces" E. Xenia and Tree (sub-block + LCA)

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.