Exemplo n.º 1
0
void LCA(int u, int d)
{
    p[u] = u;
    dist[u] = d;
    int k;
    visit[u] = true;
    for (k = head[u]; k != 0; k = edge[k].next)
    {
        if (!visit[edge[k].to])
        {
            LCA(edge[k].to, d + 1);
            p[edge[k].to] = u;
        }
    }
    for (k = qhead[qhead_hash.findHash(id_to_name[u])]; k != 0; k = qedge[k].next)
    {
        std::vector<int>::iterator j, j_first, j_last;
        j_first = name_to_id[surnames.findHash(qedge[k].to)].begin();
        j_last = name_to_id[surnames.findHash(qedge[k].to)].end();
        for (j = j_first; j != j_last; ++j)
        {
            if (visit[*j])
            {
                int lca = find(*j);
                int query1 = qmap_hash.findHash(make_pair(id_to_name[u], qedge[k].to));
                int query2 = qmap_hash.findHash(make_pair(qedge[k].to, id_to_name[u]));
                qmap[query1] = max(qmap[query1], dist[u] + dist[*j] - 2 * dist[lca]);
                qmap[query2] = max(qmap[query2], dist[u] + dist[*j] - 2 * dist[lca]);
            }
        }
    }
}
Exemplo n.º 2
0
int main(int argc, char const *argv[])
{
    int n, q, a, b;
    char str[10], str2[10];
    while(~scanf("%d%d", &n, &q))
    {
        memset(head, 0, (n+1)*sizeof(int));
        memset(dist, 0, (n+1)*sizeof(int));
        memset(visit, 0, (n+1)*sizeof(bool));
        memset(qhead, 0, sizeof(qhead));
        memset(qmap, 0, sizeof(qmap));
        surnames.clear();
        qhead_hash.clear();
        qmap_hash.clear();
        for (int i = 0; i < maxn; ++i) id_to_name[i].clear();
        for (int i = 0; i < maxn; ++i) name_to_id[i].clear();

        for (int i = 0; i < n; ++i)
        {
            scanf("%s", str);
            id_to_name[i+1] = str;
            name_to_id[surnames.insert(str)].push_back(i+1);
        }
        for (int i = 1; i <= n-1; ++i)
        {
            scanf("%d%d", &a, &b);
            edge[i*2-1].to = b;
            edge[i*2-1].next = head[a];
            head[a] = i*2-1;
            edge[i*2].to = a;
            edge[i*2].next = head[b];
            head[b] = i*2;
        }
        for (int i = 1; i <= q; ++i)
        {
            scanf("%s%s", str, str2);
            if ((surnames.findHash(str) == -1) || (surnames.findHash(str2) == -1))
            {
                qq[i].a = qq[i].b = "";
                continue;
            }
            qq[i].a = str;
            qq[i].b = str2;
            if (qhead_hash.findHash(str) == -1) qhead[qhead_hash.insert(str)] = 0;
            if (qhead_hash.findHash(str2) == -1) qhead[qhead_hash.insert(str2)] = 0;

            qedge[i*2-1].to = str2;
            qedge[i*2-1].next = qhead[qhead_hash.findHash(str)];
            qhead[qhead_hash.findHash(str)] = i*2-1;
            qedge[i*2].to = str;
            qedge[i*2].next = qhead[qhead_hash.findHash(str2)];
            qhead[qhead_hash.findHash(str2)] = i*2;
            qmap[qmap_hash.insert(make_pair(str, str2))] = -1;
            qmap[qmap_hash.insert(make_pair(str2, str))] = -1;
        }
        LCA(1, 0);
        for (int i = 1; i <= q; ++i)
        {
            if (qq[i].a == "" || qq[i].b == "")
            {
                printf("-1\n");
            }
            else
            {
                printf("%d\n", qmap[qmap_hash.findHash(make_pair(qq[i].a, qq[i].b))] + 1);
            }
        }
    }
    return 0;
}