PS:http://www.cnblogs.com/kuangbin/archive/2013/05/17/3084435.html
#include<iostream>
#include<cstdio>
#include<string.h>
#include<cstring>
#include<string>
#include<stack>
#include<set>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#define LOCAL
#define ll long long
#define lll unsigned long long
#define MAX 1000009
#define eps 1e-8
#define INF 0x7fffffff
#define mod 1000000007
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
/*
题意:
想法:
*/
const int MAXN = 100010;
int rmq[2*MAXN];//rmq数组,就是欧拉序列对应的深度序列
int deep[MAXN];
struct ST
{
int mm[2*MAXN];
int dp[2*MAXN][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<<j) - 1 <= n; i++)
dp[i][j] = rmq[dp[i][j-1]] < rmq[dp[i+(1<<(j-1))][j-1]]?dp[i][j-1]:dp[i+(1<<(j-1))][j-1];
}
int query(int a,int b)//查询[a,b]之间最小值的下标
{
if(a > b)swap(a,b);
int k = mm[b-a+1];
return rmq[dp[a][k]] <= rmq[dp[b-(1<<k)+1][k]]?dp[a][k]:dp[b-(1<<k)+1][k];
}
};
//边的结构体定义
struct Edge
{
int to,next;
};
Edge edge[MAXN*2];
int tot,head[MAXN];
int F[MAXN*2];//欧拉序列,就是dfs遍历的顺序,长度为2*n-1,下标从1开始
int P[MAXN];//P[i]表示点i在F中第一次出现的位置
int cnt;
ST st;
void init()
{
tot = 0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v)//加边,无向边需要加两次
{
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void dfs(int u,int pre,int dep)
{
F[++cnt] = u;
rmq[cnt] = dep;
P[u] = cnt;
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(v == pre)continue;
dfs(v,u,dep+1);
F[++cnt] = u;
rmq[cnt] = dep;
}
}
void LCA_init(int root,int node_num)//查询LCA前的初始化
{
cnt = 0;
dfs(root,root,0);
st.init(2*node_num-1);
}
int query_lca(int u,int v)//查询u,v的lca编号
{
return F[st.query(P[u],P[v])];
}
bool flag[MAXN];
void bfs(int root)//广搜搜出深度序列
{
memset(deep,0,sizeof(deep));
queue<int>Q;
while(!Q.empty())Q.pop();
deep[root] = 1;
Q.push(root);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
//cout<<"U: "<<u<<endl;
for(int i = head[u]; i!=-1; i = edge[i].next)
{
int v = edge[i].to;
//cout<<"V: "<<v<<endl;
if(deep[v]==0)
{
deep[v] = deep[u] + 1;
Q.push(v);
}
}
}
}
int main()
{
//freopen("date.txt","r",stdin);
map<string,int>str;
int n,m;
int u,v,k;
string x,y;
int Q;
int id;
scanf("%d",&Q);
while(Q--)
{
id = 0;
scanf("%d%d",&n,&m);
init();
memset(flag,false,sizeof(flag));
st.init(n);//st的初始化!!!
str.clear();
for(int i = 1; i <= n-1; i++)
{
cin>>x>>y;
if(str[x]==0)str[x] = ++id;
if(str[y]==0)str[y] = ++id;
u = str[x];
v = str[y];
//cout<<u<<" "<<v<<endl;
flag[u] = true;//标记儿子
//addedge(u,v);
addedge(v,u);
}
int root;
for(int i = 1; i <= id; i++)
if(!flag[i])
{
root = i;
break;
}
// cout<<root<<endl;
LCA_init(root,n);
bfs(root);
/*for(int i = 1;i<=n;i++)
{
cout<<deep[i]<<endl;
}*/
while(m--)
{
cin>>x>>y;
u = str[x];
v = str[y];
int k = query_lca(u,v);//LCA
//cout<<k<<endl;
//cout<<deep[u]<<" "<<deep[k]<<endl;
int ans = deep[u] - deep[k];//算出深度差,相当于父亲到LCA
if(v!=k)//CD操作的第一步,只需要一步就可以到达儿子,所以相等就为0,不相等就为1
ans++;
printf("%d\n",ans);
}
}
return 0;
}