题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4547

思路:这题的本质还是LCA问题,但是需要注意的地方有:

1、如果Q中u,v的lca为u,那么只需一步u->...->v。

2、如果Q中u,v的lca为v,那么需abs(dist[u]  - dist[v])步。

3、否则以上情况都不满足,那么需abs(dist[v] - dist[lca(u, v)])+1步。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
using namespace std; const int MAX_N = (400000 + 20000);
struct Edge {
int v, next;
} edge[MAX_N]; int NE, cnt, head[MAX_N], Indegree[MAX_N];
map<string, int > mp;
void Init()
{
mp.clear();
cnt = NE = 0;
memset(head, -1, sizeof(head));
memset(Indegree, 0, sizeof(Indegree));
} void Insert(int u, int v)
{
edge[NE].v = v;
edge[NE].next = head[u];
head[u] = NE++;
} struct q_edge {
int u, v, id, next;
} q_ee[MAX_N]; int q_ne, q_head[MAX_N];
void q_init()
{
q_ne = 0;
memset(q_head, -1, sizeof(q_head));
} void q_insert(int u, int v, int id)
{
q_ee[q_ne].u = u;
q_ee[q_ne].v = v;
q_ee[q_ne].id = id;
q_ee[q_ne].next = q_head[u];
q_head[u] = q_ne++;
} int N, M, ans[MAX_N], dist[MAX_N];
int parent[MAX_N], lca[MAX_N];
bool vis[MAX_N]; int Find(int x)
{
if (x == parent[x]) {
return parent[x];
}
return parent[x] = Find(parent[x]);
} void dfs(int u)
{
parent[u] = u;
vis[u] = true;
for (int i = q_head[u]; ~i; i = q_ee[i].next) {
int v = q_ee[i].v, id = q_ee[i].id;
if (vis[v]) { lca[id] = Find(v);
}
} for (int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].v;
if (!vis[v]) {
dist[v] = dist[u] + 1;
dfs(v);
parent[v] = u;
}
}
} int main()
{
int Cas;
cin >> Cas;
while (Cas--) {
cin >> N >> M; Init(); for (int i = 1; i < N; ++i) {
string str1, str2;
cin >> str1 >> str2;
if (mp.find(str1) == mp.end()) mp[str1] = ++cnt;
if (mp.find(str2) == mp.end()) mp[str2] = ++cnt; Indegree[mp[str1]]++;
Insert(mp[str2], mp[str1]);
} q_init(); for (int i = 1; i <= M; ++i) {
string str1, str2;
cin >> str1 >> str2;
q_insert(mp[str1], mp[str2], i);
q_insert(mp[str2], mp[str1], i);
} //from root;
memset(vis, false, sizeof(vis));
for (int i = 1; i <= cnt; ++i) {
if (!Indegree[i]) {
dist[i] = 0;
dfs(i);
break;
}
} for (int i = 0; i < q_ne; ++i) {
if (!(i & 1)) {
if (q_ee[i].u == q_ee[i].v) {
puts("0");
} else if (q_ee[i].u == lca[q_ee[i].id]) {
puts("1");
} else if (q_ee[i].v == lca[q_ee[i].id]) {
printf("%d\n", abs(dist[q_ee[i].v] - dist[q_ee[i].u]));
} else {
printf("%d\n", abs(dist[q_ee[i].u] - dist[lca[q_ee[i].id]]) + 1);
}
}
} }
return 0;
}



hdu 4547(LCA)的更多相关文章

  1. 洛谷P3379 【模板】最近公共祖先(LCA)

    P3379 [模板]最近公共祖先(LCA) 152通过 532提交 题目提供者HansBug 标签 难度普及+/提高 提交  讨论  题解 最新讨论 为什么还是超时.... 倍增怎么70!!题解好像有 ...

  2. 图论--最近公共祖先问题(LCA)模板

    最近公共祖先问题(LCA)是求一颗树上的某两点距离他们最近的公共祖先节点,由于树的特性,树上两点之间路径是唯一的,所以对于很多处理关于树的路径问题的时候为了得知树两点的间的路径,LCA是几乎最有效的解 ...

  3. 面试题6:二叉树最近公共节点(LCA)《leetcode236》

    Lowest Common Ancestor of a Binary Tree(二叉树的最近公共父亲节点) Given a binary tree, find the lowest common an ...

  4. P3379 【模板】最近公共祖先(LCA)

    P3379 [模板]最近公共祖先(LCA) 题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询 ...

  5. A * B Problem Plus HDU - 1402 (FFT)

    A * B Problem Plus HDU - 1402 (FFT) Calculate A * B.  InputEach line will contain two integers A and ...

  6. 洛谷P3379 【模板】最近公共祖先(LCA)(dfs序+倍增)

    P3379 [模板]最近公共祖先(LCA) 题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询 ...

  7. 「LuoguP3379」 【模板】最近公共祖先(LCA)

    题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询问的个数和树根结点的序号. 接下来N-1行每 ...

  8. 洛谷——P3379 【模板】最近公共祖先(LCA)

    P3379 [模板]最近公共祖先(LCA) 题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询 ...

  9. luogo p3379 【模板】最近公共祖先(LCA)

    [模板]最近公共祖先(LCA) 题意 给一个树,然后多次询问(a,b)的LCA 模板(主要参考一些大佬的模板) #include<bits/stdc++.h> //自己的2点:树的邻接链表 ...

随机推荐

  1. Codeforces Round #238 (Div. 2) D. Toy Sum(想法题)

     传送门 Description Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to s ...

  2. 【Beta】Scrum5.5

    Info 时间:2016.12.13 21:45 时长:15min 地点:大运村1号公寓5楼楼道 类型:日常Scrum会议 NXT:2016.12.15 21:30 Task Report Name ...

  3. ppt2013技术整理

    1. 显示选择窗格 便于选择该页的所有元素.分组.隐藏与显示等. 位于:开始-编辑-选择-选择窗格 2. 显示动画窗格 便于调节页面中元素的动画状态. 位于:动画-高级动画-动画窗格 3. 绑定动画触 ...

  4. R语言读取本地文件注意事项

    R里面应该用/,而不是\ ,或者用两个\\   R区分大小写,所以应该用C:,而不是c:

  5. 30个要点帮你完成java代码优化

    通过java代码规范来优化程序,优化内存使用情况,防止内存泄露 可供程序利用的资源(内存.CPU时间.网络带宽等)是有限的,优化的目的就是让程序用尽可能少的资源完成预定的任务.优化通常包含两方面的内容 ...

  6. Python学习笔记——条件和循环

    1.条件表达式 >>> x = 3 >>> x = 1 if x<3 else 2 >>> x 2 2.for语句用于序列类型 <1& ...

  7. Loadrunner安装

    安装参考网址:http://www.cnblogs.com/yangxia-test/archive/2012/10/30/2746621.html 本人验证过的,不自己写了 另附Loadrunner ...

  8. 超酷jQuery进度条加载动画集合

    在丰富多彩的网页世界中,进度条加载动画的形式非常多样,有利用gif图片实现的loading动画,也有利用jQuery和CSS3实现的进度加载动画,本文主要向大家介绍很多jQuery和CSS3实现的进度 ...

  9. GitHub使用教程

    一直以来都想使用Git来管理自己平时积累的小代码,就是除了工作之外的代码了.有时候自己搞个小代码,在公司写了,就要通过U盘或者网盘等等一系列工具进行Copy,然后回家才能继续在原来的基础上作业.Cop ...

  10. word20161209

    failback / 故障回复 failback policy / 故障回复策略 failed / 失败 failover / 故障转移 failover policy / 故障转移策略 failov ...