A Research on "The Hundred Family Surnames"(虚树上求最远点对)

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. 

Input

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.

Output

For every query , output the number of nodes on the longest happiness path. If the path does not exist, output  -1.

Sample Input

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

Sample Output

1
-1
3
3
4

 

 

 

 

 

题解

首先想到从同种的姓中选出几个代表的节点,然后来解决查询

由于它是一棵树,所以我们只需要把同种姓的最远的两个点记录下来,然后询问的时候直接4次LCA即可

可以利用虚树来求树上点集的最远点对

其中由于一个SB错误调了我3h+

树链剖分的时候要memset(我之前一直以为可以在dfs覆盖之前的结果),否则一些本来没有重儿子的点就会存在重儿子(上一次的结果)!!!

以后虚树DP的题一旦调不出来就一定要生成大样例

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 100005
const int INF=0x3f3f3f3f;

int fir[N],to[2*N],nxt[2*N],cd[2*N],cnt;
int dep[N],son[N],siz[N],top[N],fa[N];
int dfn[N],dc,stk[N],del[N],TP,dcon;
bool vis[N];
char ch1[6],ch2[6];
int D[N][2],DCT,pos,maxx;
struct node{
	char s[6];int id,num;
	node(){memset(s,0,sizeof(s));id=num=0;}
	bool operator < (const node &t)const{
		return strcmp(s,t.s)<0||(strcmp(s,t.s)==0&&dfn[id]<dfn[t.id]);
	}
}a[N],tmp,tt;

void adde(int a,int b,int c)
{
	to[++cnt]=b;nxt[cnt]=fir[a];fir[a]=cnt;cd[cnt]=c;
	to[++cnt]=a;nxt[cnt]=fir[b];fir[b]=cnt;cd[cnt]=c;
	//printf("%d %d %d\n",a,b,c);
}
void dfs1(int u)
{
	siz[u]=1;dep[u]=dep[fa[u]]+1;
	for(int v,p=fir[u];p;p=nxt[p])if((v=to[p])!=fa[u]){
		fa[v]=u;dfs1(v);siz[u]+=siz[v];
		if(siz[son[u]]<siz[v])son[u]=v;
	}
}
void dfs2(int u)
{
	dfn[u]=++dc;
	if(son[u])top[son[u]]=top[u],dfs2(son[u]);
	for(int v,p=fir[u];p;p=nxt[p])
		if((v=to[p])!=fa[u]&&v!=son[u])
			top[v]=v,dfs2(v);
}
int LCA(int x,int y)
{
	while(top[x]!=top[y]){if(dep[top[x]]<dep[top[y]])swap(x,y);x=fa[top[x]];}
	return dep[x]<dep[y]?x:y;
}
int dis(int x,int y){return dep[x]+dep[y]-2*dep[LCA(x,y)]+1;}
bool cmp(int x,int y){return dfn[x]<dfn[y];}

void build()
{
	TP=0;
	int tmp=dcon,i,lca;
	if(del[1]!=1)del[++dcon]=1,stk[++TP]=1;
	for(i=1;i<=tmp;i++){
		vis[del[i]]=1;
		if(TP<=1||(lca=LCA(stk[TP],del[i]))==stk[TP]){stk[++TP]=del[i];continue;}
		while(TP>1&&dep[stk[TP-1]]>=dep[lca]){
			adde(stk[TP-1],stk[TP],dep[stk[TP]]-dep[stk[TP-1]]);
			TP--;
		}
		if(lca!=stk[TP]){
			adde(lca,stk[TP],dep[stk[TP]]-dep[lca]);
			stk[TP]=lca;del[++dcon]=lca;
		}
		stk[++TP]=del[i];
	}
	while(TP>1){adde(stk[TP-1],stk[TP],dep[stk[TP]]-dep[stk[TP-1]]);TP--;}
}
void dfs(int u,int ff,int d)
{
	if(d>=maxx&&vis[u]){maxx=d;pos=u;}
	for(int p=fir[u];p;p=nxt[p])
		if(to[p]!=ff)dfs(to[p],u,d+cd[p]);
}
void solve()
{
	maxx=-1;dfs(1,0,0);D[++DCT][0]=pos;
	maxx=-1;dfs(pos,0,0);D[DCT][1]=pos;
}
void clean()
{
	for(int i=1;i<=dcon;i++)
		fir[del[i]]=vis[del[i]]=0;
	dcon=0;cnt=0;
}
int main()
{
	//freopen("1.in","r",stdin);
	//freopen("1.out","w",stdout);
	int n,m,i,u,v;
	while(~scanf("%d%d",&n,&m)){
		memset(a[n+1].s,0,sizeof(a[n+1].s));
		memset(fa,0,sizeof(fa));
		memset(dep,0,sizeof(dep));
		memset(son,0,sizeof(son));
		memset(siz,0,sizeof(siz));
		memset(top,0,sizeof(top));
		for(i=1;i<=n;i++){scanf("%s",a[i].s);a[i].id=i;}
		for(i=1;i<n;i++){
			scanf("%d%d",&u,&v);
			adde(u,v,0);
		}
		dc=DCT=0;dfs1(1);top[1]=1;dfs2(1);
		sort(a+1,a+n+1);
		dcon=0;memset(fir,0,sizeof(fir));cnt=0;
		
		for(i=1;i<=n;i++){
			del[++dcon]=a[i].id;
			a[i].num=DCT+1;
			if(strcmp(a[i].s,a[i+1].s)!=0){
				build();
				solve();
				clean();
			}
		}
		
		for(i=1;i<=m;i++){
			bool flag=1;int pos1=0,pos2=0;
			scanf("%s",tmp.s);tt=a[lower_bound(a+1,a+n+1,tmp)-a];
			if(strcmp(tt.s,tmp.s)==0) pos1=tt.num;
			else flag=0;
			scanf("%s",tmp.s);tt=a[lower_bound(a+1,a+n+1,tmp)-a];
			if(strcmp(tt.s,tmp.s)==0) pos2=tt.num;
			else flag=0;
			if(!flag) printf("-1\n");
			else printf("%d\n",max(max(dis(D[pos1][0],D[pos2][0]),dis(D[pos1][0],D[pos2][1])),max(dis(D[pos1][1],D[pos2][0]),dis(D[pos1][1],D[pos2][1]))));
		}
	}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值