2016北京网络赛E hihocode1387 A Research on "The Hundred Family Surnames"(LCA+树直径)

思路:参照ICPC-CAMP的题解

题意: 给一棵树,每个节点上有个颜色,很多询问,询问两种颜色,问从这两种颜色中各取一个节点,距离最大是多少。

题解:处理出每种颜色的节点们的直径(也就是距离最大的点对)。然后对于两种询问颜色(a,b)(a,b)的直径(au,av)(au,av)(bu,bv)(bu,bv),答案就是\max\{dis(au,bu),dis(au,bv),dis(av,bu),dis(av,bv)\}max{dis(au,bu),dis(au,bv),dis(av,bu),dis(av,bv)}
O(n \log n)O(nlogn)算一种颜色的节点们的直径的方法:增量添加节点同时维护直径,假设新加的节点是zz,原直径是(x,y)(x,y),那么新直径是(x,y),(x,z),(y,z)(x,y),(x,z),(y,z)中的一个。
tips:证明考虑反证法。


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
#include<string>
using namespace std;
const int maxm = 19;
const int maxn = 1e5+7;
struct Edge
{
	int v,nxt;
}edge[maxn*20];
int head[maxn];
int tot = 0;
int lca[maxn][maxm],a[maxn],dep[maxn];
int dp[maxn][2];   //结点id的最左端点和最右端点 
int n,q;
void add_edge(int u,int v)
{
	edge[tot].v=v;
	edge[tot].nxt = head[u];
	head[u]=tot++;
}
void dfs(int u,int f)
{
	for(int i = head[u];~i;i=edge[i].nxt)
	{
		int v = edge[i].v;
		if(v==f)continue;
		dep[v]=dep[u]+1;
		lca[v][0]=u;
		for(int j = 1;j<maxm;j++)
		{
			int fa = lca[v][j-1];
			if(fa==-1)continue;
			lca[v][j]=lca[fa][j-1];
		}
		dfs(v,u);
	}
}
int up(int u,int d)
{
	for(int i = maxm-1;i>=0;i--)
	{
		if(d<(1<<i))continue;
		u=lca[u][i];
		d-=(1<<i);
	}
	return u;
}
int Lca(int x,int y)
{
	if(dep[x]>dep[y])swap(x,y);
	y=up(y,dep[y]-dep[x]);
	if(x==y)return x;
	for(int i = maxm-1;i>=0;i--)
	{
		if(lca[x][i]!=lca[y][i])
			x=lca[x][i],y=lca[y][i];
	}
	return lca[x][0];
}
int getdis(int u,int v)
{
	int root = Lca(u,v);
	return dep[u]+dep[v]-2*dep[root]+1;
}
void work(int u,int id)
{
	if(dp[id][0]==-1)
	{
		dp[id][0]=u;
		dp[id][1]=u;
		return;
	}
    int dis1 = getdis(u,dp[id][0]);
	int dis2 = getdis(u,dp[id][1]);
	int dis3 = getdis(dp[id][0],dp[id][1]);
    if(dis1>=dis2)       //当前u偏向右边
	{
		if(dis1>dis3)
			dp[id][1]=u;
	}
	else if (dis2>dis3)   
		dp[id][0]=u;
}
void dfs1(int u,int f)
{
    work(u,a[u]);
	for(int i = head[u];~i;i=edge[i].nxt)
	{
		int v = edge[i].v;
		if(v==f)continue;
		dfs1(v,u);
	}
}
map<string,int>vis;
int main()
{
    while(scanf("%d%d",&n,&q)!=EOF)
	{
		memset(dp,-1,sizeof(dp));
		memset(lca,-1,sizeof(lca));
		memset(head,-1,sizeof(head));
		tot = 0;
		vis.clear();
        string tmp;
		int cnt = 1;
		for(int i = 1;i<=n;i++)
		{
			cin >> tmp;
			if(!vis.count(tmp))
			{
				vis[tmp]=cnt;
                a[i]=cnt++;
			}
			else a[i]=vis[tmp];
		}
		for(int i = 1;i<=n-1;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			add_edge(u,v);
			add_edge(v,u);
		}
        dfs(1,-1);              //处理lca
        dfs1(1,-1);
		for(int i = 1;i<=q;i++)
		{
			string u,v;
			cin >> u >> v;
			if(!vis.count(u)|| !vis.count(v))
			{
				printf("-1\n");
				continue;
			}
			int st = vis[u];
			int ed = vis[v];
			int dis1 = getdis(dp[st][0],dp[ed][0]);
			int dis2 = getdis(dp[st][0],dp[ed][1]);
			int dis3 = getdis(dp[st][1],dp[ed][0]);
			int dis4 = getdis(dp[st][1],dp[ed][1]);
			printf("%d\n",max(max(dis1,dis2),max(dis3,dis4)));
		}
	}
}


时间限制: 3000ms
单点时限: 3000ms
内存限制: 256MB

描述

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


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值