HihoCoder - 1387(树的直径,树上距离)

1387 : A Research on “The Hundred Family Surnames”

时间限制: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

题意:给你一棵树,树上有很多种颜色,每个颜色会出现在很多节点.Q次询问,问两种颜色的树上最长路径长度是多少.

思路:树的直径来写,先求出每种颜色内部的直径,然后询问的时候假如a颜色直径端点为

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5+50;

struct edge
{
    int u,v,ne;
} e[maxn<<1];

int n,q,a[maxn];
map<string,int> mp;
int head[maxn],len;
int f[maxn],deep[maxn],anc[maxn][21];
int c[maxn][2],d[maxn];

void add(int u,int v)
{
    e[len].u = u;
    e[len].v = v;
    e[len].ne = head[u];
    head[u] = len++;
}

void dfs(int x,int p)
{
    deep[x] = p!= -1?deep[p]+1:1; 
    f[x] = p;
    anc[x][0] = p;

    for(int i = 1;i<= 20;i++)
        anc[x][i] = anc[anc[x][i-1]][i-1];

    for(int i = head[x];i!= -1;i = e[i].ne)
    {
        if(e[i].v == p) continue;
        dfs(e[i].v,x);
    }
    return ;
}

int lca(int x,int y)
{
    if(deep[x]< deep[y])
        swap(x,y);

    for(int i = 20;i>= 0;i--)
        if(deep[x]-(1<<i)>= deep[y])
            x = anc[x][i];
    if(x == y) return x;
    for(int i = 20;i>= 0;i--)
        if(anc[x][i]!= anc[y][i])
            x = anc[x][i],y = anc[y][i];
    return anc[x][0];
}

int get_dis(int x,int y)
{
    int tmp = lca(x,y);
    return deep[x]-deep[tmp]+deep[y]-deep[tmp]+1;
}

void update(int x,int y,int z)
{
    int dis,cl = a[z];
    dis = get_dis(x,z);
    if(dis> d[cl])
    {
        c[cl][0] = x,c[cl][1] = z;
        d[cl] = dis;
    }
    dis = get_dis(y,z);
    if(dis> d[cl])
    {
        c[cl][0] = y,c[cl][1] = z;
        d[cl] = dis;
    }
    return ;
}

void dfs2(int x,int p)
{
    int cl = a[x];
    if(c[cl][0]&&c[cl][1])
        update(c[cl][0],c[cl][1],x);
    else
    {
        c[cl][0] = c[cl][1] = x;
        d[cl] = 1;
    }

    for(int i = head[x];i!= -1;i = e[i].ne)
    {
        int v = e[i].v;
        if(v == p) continue;
        dfs2(v,x);
    }
    return ;
}

void init()
{
    mp.clear();
    mem(c,0);
    mem(head,-1);
    len = 0;
}

int main()
{
    while(~scanf("%d %d",&n,&q))
    {
        init();
        int cnt = 0;
        for(int i = 1;i<= n;i++)
        {
            string x;
            cin>>x;
            if(mp[x])
                a[i] = mp[x];
            else
            {
                mp[x] = ++cnt;
                a[i] = cnt;
            }
        }

        for(int i = 1,x,y;i< n;i++)
        {
            scanf("%d %d",&x,&y);
            add(x,y);
            add(y,x);
        }

        dfs(1,0);
        dfs2(1,0);

        while(q--)
        {
            string x,y; 
            cin>>x>>y;
            if(!mp[x]||!mp[y])
                printf("-1\n");
            else
            {
                int cl1 = mp[x],cl2 = mp[y];
                printf("%d\n",max(max(get_dis(c[cl1][0],c[cl2][0]),get_dis(c[cl1][0],c[cl2][1])),max(get_dis(c[cl1][1],c[cl2][0]),get_dis(c[cl1][1],c[cl2][1]))));
            }
        }   
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值