#1387 : A Research on "The Hundred Family Surnames"
-
3 3 Chen Qian Zhuge 1 2 2 3 Chen Chen Chen Sun Zhuge Chen 4 2 Chen Chen Qian Qian 1 2 2 3 1 4 Chen Qian Qian Qian
样例输出
-
1 -1 3 3 4
描述
The Hundred Family Surnames is a classic Chinese text composed of common Chinese surnames. The book was composed in the early Song Dynasty. It originally contained 411 surnames, and was later expanded to 504. Of these, 444 are single-character surnames, and 60 are double-character surnames. (quoted from Wikipedia)
Li Lei, a student of Peking University, has done some research on that name book. He wrote a program to built a surname tree, as implied by the book. Here, "tree" is a concept of graph theory. On Li's surname tree, each node is a Chinese surname. A Chinese surname consists of only English letters and its length is no more than 5 letters.
There is a mysterious legend about this surname tree. If a pair of Chinese loves can find a path on the tree whose two end points are their surnames, they will have a happy marriage. The longer the path is , the more happiness they will have.
Now, we have many pairs of lovers, they want to find out the longest path whose two end points are their surnames.
输入
The input contains no more than 10 test cases.
For each case, the first line contains two positive integers N and Q(N,Q <= 105). N is the number of nodes on the tree which numbered from 1 to N, and Q is the number of queries.
Among the following N lines, the i-th line is the surname of node i .
In the next N-1 lines, each line contains two numbers x , y , meaning that there is an edge between node x and node y.
Then, each of the next Q lines is a query, which contains two strings meaning the surnames of two lovers.
输出
For every query , output the number of nodes on the longest happiness path. If the path does not exist, output -1.
/*
* Author: ktmzgl
* Created Time: 2016/9/25 9:04:08
* File Name: F:\Vim\code\9.25_pku_1005
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <time.h>
#include <map>
using namespace std;
const int maxint = -1u>>1;
const int maxn=200010;
int n,q;
int cnt,tot1;
map<string,int> m;
int id[maxn];
bool vis[maxn];
char s[10];
int dp[maxn][20];
int tot,head[maxn],ver[2*maxn],R[2*maxn],first[maxn],dir[maxn];
struct Node
{
int v;
int next;
};
struct Node e[maxn*2+10];
void AddNode(int from, int to)
{
e[tot1].v=to;
e[tot1].next=head[from];
head[from]=tot1++;
}
//ver:节点编号 R:深度 first:点编号位置 dir:距离
void dfs(int u ,int dep)
{
vis[u] = true; ver[++tot] = u; first[u] = tot; R[tot] = dep;
for(int k=head[u]; k!=-1; k=e[k].next)
if( !vis[e[k].v] )
{
int v = e[k].v;
dir[v] = dir[u] + 1;
dfs(v,dep+1);
ver[++tot] = u; R[tot] = dep;
}
}
void ST(int n)
{
for(int i=1;i<=n;i++)
dp[i][0] = i;
for(int j=1;(1<<j)<=n;j++)
{
for(int i=1;i+(1<<j)-1<=n;i++)
{
int a = dp[i][j-1] , b = dp[i+(1<<(j-1))][j-1];
dp[i][j] = R[a]<R[b]?a:b;
}
}
}
//中间部分是交叉的。
int RMQ(int l,int r)
{
int k=0;
while((1<<(k+1))<=r-l+1)
k++;
int a = dp[l][k], b = dp[r-(1<<k)+1][k]; //保存的是编号
return R[a]<R[b]?a:b;
}
int LCA(int u ,int v)
{
int x = first[u] , y = first[v];
if(x > y) swap(x,y);
int res = RMQ(x,y);
return ver[res];
}
int Class[maxn][2];
void init()
{
cnt=1;
tot1=tot=0;
dir[1]=0;
m.clear();
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
memset(Class,0,sizeof(Class));
}
int dis(int x,int y)
{
return dir[x] + dir[y] - 2*dir[LCA(x,y)]+1;
}
int main()
{
while(scanf("%d%d",&n,&q)!=EOF)
{
init();
for(int i=1;i<=n;i++)
{
scanf("%s",s);
if(m[s])
{
id[i]=m[s];
}
else
{
id[i]=m[s]=cnt++;
}
}
for(int i=0;i<n-1;i++)
{
int x,y;
scanf("%d%d",&x,&y);
AddNode(x,y);
AddNode(y,x);
}
dfs(1,1);
ST(n*2-1);
for(int i=1;i<=n;i++)
{
if(Class[id[i]][0]==0&&Class[id[i]][1]==0)
{
Class[id[i]][0]=Class[id[i]][1]=i;
}
else
{
int x=Class[id[i]][0];
int y=Class[id[i]][1];
int z=i;
int len1=dir[x] + dir[y] - 2*dir[LCA(x,y)]+1;
int tmp=len1;
int len2=dir[x] + dir[z] - 2*dir[LCA(x,z)]+1;
int len3=dir[y] + dir[z] - 2*dir[LCA(y,z)]+1;
if(len2>len1)
{
tmp=len2;
Class[id[i]][0]=x,Class[id[i]][1]=z;
}
if(len3>tmp)
{
Class[id[i]][0]=y,Class[id[i]][1]=z;
}
}
}
int s1,s2;
for(int i=0;i<q;i++)
{
scanf("%s",s),s1=m[s];
scanf("%s",s),s2=m[s];
if(s1==0||s2==0) {
printf("-1\n");
continue;
}
int a=Class[s1][0],b=Class[s1][1];
int c=Class[s2][0],d=Class[s2][1];
//cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
int ans=max(dis(a,c),dis(a,d));
ans=max(ans,max(dis(b,c),dis(b,d)));
printf("%d\n",ans);
}
}
}