描述
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.
样例输入
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
题意: 给一棵树,每个节点上有个颜色,很多询问,询问两种颜色,问从这两种颜色中各取一个节点,距离最大是多少。
题解链接
题解:处理出每种颜色的节点们的直径(也就是距离最大的点对)。然后对于两种询问颜色((a,b))的直径((au,av))和((bu,bv)),答案就是(max){$ dis(au,bu),dis(au,bv),dis(av,bu),dis(av,bv) (}。
)O(nlogn)(算一种颜色的节点们的直径的方法:增量添加节点同时维护直径,假设新加的节点是)z(,原直径是)(x,y)(,那么新直径是)(x,y),(x,z),(y,z)$中的一个。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<map>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
#define dbg(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<endl;
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn=1e5+1000;
int head[maxn];
struct edge
{
int to,next;
}e[maxn*2]; //
int tol=0;
void add(int u,int v)
{
e[++tol].to=v,e[tol].next=head[u],head[u]=tol;
}
string a[maxn];
map<string,int> mp;
PII d[maxn];
int mx[maxn];
int deep[maxn],fa[maxn][20];
void bfs(int rt)
{
queue<int>q;
deep[rt]=0;
fa[rt][0]=rt;
q.push(rt);
while(!q.empty())
{
int t=q.front();
q.pop();
for(int i=1;i<=19;i++)
fa[t][i] = fa[fa[t][i-1]][i-1];
for(int i = head[t];i;i=e[i].next)
{
int v = e[i].to;
if(v==fa[t][0])continue;
deep[v]=deep[t]+1;
fa[v][0]=t;
q.push(v);
}
}
}
int lca(int u,int v)
{
if(deep[u]>deep[v])swap(u,v);
int hu=deep[u],hv=deep[v];
int tu=u,tv=v;
for(int det=hv-hu,i=0;det;det>>=1,i++)
if(det&1)
tv = fa[tv][i];
if(tu == tv)return tu;
for(int i =19; i>=0; i--)
{
if(fa[tu][i] == fa[tv][i]) continue;
tu = fa[tu][i];
tv = fa[tv][i];
}
return fa[tu][0];
}
int dist(int u,int v)
{
int f=lca(u,v);
return deep[u]+deep[v]-2*deep[f]+1;
}
char s[100];
int id[maxn];
int main()
{
int n,q;
while(~scanf("%d%d",&n,&q))
{
mp.clear();
tol=0;
int cnt=0;
rep(i,1,n+1)
{
scanf("%s",s);
a[i]=s;
head[i]=0;
if(!mp.count(a[i]))
{
mp[a[i]]=++cnt;
id[i]=cnt;
d[cnt]=make_pair(i,i);
mx[cnt]=1;
}
else
{
id[i]=mp[a[i]];
}
}
rep(i,1,n)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
bfs(1);
rep(i,1,n+1)
{
int t=id[i];
int x=d[t].fi,y=d[t].se;
int tt=dist(x,i);
if(tt>mx[t])
{
mx[t]=tt;
d[t]=make_pair(x,i);
}
tt=dist(y,i);
if(tt>mx[t])
{
mx[t]=tt;
d[t]=make_pair(y,i);
}
}
while(q--)
{
char s1[100],s2[100];
scanf("%s%s",s1,s2);
if((!mp.count(s1))||(!mp.count(s2)))
{
puts("-1");
continue;
}
int ans=0;
int t=mp[s1];
int au=d[t].fi,av=d[t].se;
t=mp[s2];
int bu=d[t].fi,bv=d[t].se;
ans=max(ans,dist(au,bv));
ans=max(ans,dist(av,bu));
ans=max(ans,dist(au,bu));
ans=max(ans,dist(av,bv));
printf("%d
",ans);
}
}
return 0;
}