HDU 4547 CD operation (LCA + BFS)

Source: Internet
Author: User

Description:
Under Windows we can run DOS by cmd part of the function, where CD is a very interesting command, through the CD operation, we can change the current directory.
Here we simplify the problem, assuming there is only one root directory, CD operations are only two ways:
  
1. CD current directory name \...\ target directory name (can contain several directories in the middle, to ensure that the target directory through the absolute path can be reached)
2. CD. (Returns the parent directory of the current directory)
  
Now given the current directory and a target directory, how many CD operations will be required at least several times to turn the current directory into the target directory?

Input:

The first line of input data contains an integer T (t<=20), indicating the number of samples;
The first line of each sample is two integers n and m (1<=n,m<=100000), indicating that there are n directories and M queries;
Next N-1 row two directory name a B (the directory name is a string containing only numbers or letters, which is less than 40), indicating that A's parent directory is B.
The last m row of two directory names a B, indicating the minimum number of CD operations to ask to change the current directory from A to B.
Data is guaranteed to be legitimate, there must be a root directory, each directory can be accessed from the root directory.

Output:
Please output the result of each query, with one row for the output of each.

Sample Input:
2
3 1
B A
C A
B C

3 2
B A
C B
A C
C A
Sample Output:
2
1
2
#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <cmath > #include <vector> #include <queue> #include <stack> #include <cstring> #include <map> #include <set> #define LL long longusing namespace Std;const int maxn=100010;int rmq[2*maxn];//build RMQ Array//********* The ST algorithm, which contains initialization init (n) and query (S,T) functions//points numbering starting from 1, 1-n. Returns the subscript of the minimum value//***************************struct    st{int Mm[2*maxn];//mm[i] represents the highest bit of I, mm[1]=0,mm[2]=1,mm[3]=1,mm[4]=2 int dp[maxn*2][20];        void init (int n) {mm[0]=-1;            for (int i=1;i<=n;i++) {mm[i]= ((i& (i-1)) ==0?mm[i-1]+1:mm[i-1]);        Dp[i][0]=i; } for (int j=1;j<=mm[n];j++) for (int i=1;i+ (1&LT;&LT;J) -1<=n;i++) dp[i][j]=rmq[dp[i][j-1]    ]<rmq[dp[i+ (1<< (j-1))][j-1]]?dp[i][j-1]:d p[i+ (1<< (j-1))][j-1];    } int query (int a,int b)//Query the subscript {if (a>b) swap (b) for the minimum value between A and B;    int k=mm[b-a+1];    Return rmq[dp[a][k]]<rmq[dp[b-(1<<k) +1][k]]?dp[a][k]:d p[b-(1<<k) +1][k]; struct node{int to,next;};/with}};//edges defined * ******************************************LCA conversion to RMQ problem MAXN is the maximum node count. The array of St and the F,edge to be set to 2*MAXNF are Euler sequences, RMQ is the depth sequence, and P is the subscript for the first occurrence of a point in F *********************************************/struct lca2rmq{int n;//Node number node edge[2*maxn];//The edge of the tree because it is built without an edge, so it is twice times the int tol;//side of the count int head[maxn];//head node bool Vis[ma    The xn];//access token int f[2*maxn];//f is a Euler sequence, which is the order of the DFS traversal int p[maxn];//a point in F where the first occurrence of the position int cnt;    St St;        void init (int n)//n is the total number of points, which can start at 0 or start with 1 {this->n=n;        tol=0;    memset (head,-1,sizeof (head));        } void Addedge (int a,int b)//plus side {edge[tol].to=b;        Edge[tol].next=head[a];        head[a]=tol++;        Edge[tol].to=a;        EDGE[TOL].NEXT=HEAD[B];    head[b]=tol++;    } int query (int a,int b)//pass in two nodes and return their LCA number {return f[st.query (P[a],p[b])]; } void Dfs (int a,int lev) {vis[a]=true;        ++cnt;//first, to ensure that the F sequence and RMQ sequence starting from 1 f[cnt]=a;//Euler sequence, numbering starting from 1, a total of 2*n-1 elements rmq[cnt]=lev;//rmq array is the depth sequence p[a]=cnt;            for (int i=head[a];i!=-1;i=edge[i].next) {int v=edge[i].to;            if (Vis[v]) continue;            DFS (V,LEV+1);            ++cnt;            F[cnt]=a;        Rmq[cnt]=lev;        }} void solve (int root) {memset (vis,false,sizeof (VIS));        cnt=0;        DFS (root,0);    St.init (2*n-1); }}lca;vector<int>g[maxn];map<string, Int>mm;bool flag[maxn];int dis[maxn];void bfs (int root) {memset (dis    , 0, sizeof (DIS));    queue<int>q;    Dis[root] = 1;    Q.push (root); while (!        Q.empty ()) {int u = q.front ();        Q.pop ();        int sz = G[u].size ();            for (int i=0;i<sz;i++) {int v = g[u][i];                if (dis[v] = = 0) {Dis[v] = Dis[u] + 1;            Q.push (v);  }}}}int Main () {      int T, N, M;        scanf ("%d", &t);            while (t--) {scanf ("%d%d", &n, &m);            Lca.init (N);            for (int i=1;i<=n;i++) g[i].clear ();            Mm.clear ();            memset (flag, true, sizeof (flag));            int u, v;            int id = 0;            string s1, S2;                for (int i=1;i<n;i++) {cin>>s1>>s2;                if (mm[s1] = = 0) mm[s1] = ++id;                if (mm[s2] = = 0) mm[s2] = ++id;                U = mm[s2], v = mm[s1];                FLAG[V] = false;                Lca.addedge (U, v);            G[u].push_back (v);            } int root;                for (int i=1;i<=n;i++) if (Flag[i]) {root = i;            Break            } lca.solve (root);            BFS (root);                while (m--) {cin>>s1>>s2;                int u = mm[s1];            int v = mm[s2];    int tmp = lca.query (U, v);                int ans = dis[u]-dis[tmp];                if (tmp! = v) ans++;            printf ("%d\n", ans); }} return 0;}


HDU 4547 CD operation (LCA + BFS)

Contact Us

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.