题目链接:http://hihocoder.com/problemset/problem/1062

题意裸,有个trick,导致我当年做的时候一直在WA...

那就是出现这种没有出现在关系中,但是依然可以知道他们关系的输入样例: LinDaiyu LinDaiyu

应该输出自己。

 #include <bits/stdc++.h>
using namespace std; const int maxn = ;
int n, m, q;
map<string, int> id;
string di[maxn];
vector<int> G[maxn];
int depth[maxn], fa[maxn], in[maxn];
char a[maxn], b[maxn];
int pre[maxn]; int find(int x) {
return x == pre[x] ? x : pre[x] = find(pre[x]);
} void unite(int x, int y) {
x = find(x);
y = find(y);
if(x != y) pre[x] = y;
} void dfs(int u, int p, int d) {
depth[u] = d;
fa[u] = p;
for(int i = ; i < G[u].size(); i++) {
int &v = G[u][i];
if(v == u || v == p) continue;
dfs(v, u, d+);
}
} int query(int u, int v) {
if(find(u) != find(v)) return -;
if(depth[u] < depth[v]) swap(u, v);
while(depth[u] > depth[v]) u = fa[u];
while(u != v) {
u = fa[u];
v = fa[v];
}
return u;
} int main() {
// freopen("in", "r", stdin);
while(~scanf("%d", &n)) {
id.clear(); m = ;
memset(depth, , sizeof(depth));
memset(in, , sizeof(in));
for(int i = ; i < maxn; i++) {
pre[i] = i;
G[i].clear();
}
for(int i = ; i < n; i++) {
scanf("%s %s", a, b);
if(id.find(a) == id.end()) {
id[a] = ++m;
di[m] = a;
}
if(id.find(b) == id.end()) {
id[b] = ++m;
di[m] = b;
}
G[id[a]].push_back(id[b]);
unite(id[a], id[b]);
in[id[b]]++;
}
for(int i = ; i <= m; i++) {
if(!in[i]) {
dfs(i, -, );
}
}
scanf("%d", &q);
while(q--) {
scanf("%s %s", a, b);
if(strcmp(a, b) == ) {
puts(a);
continue;
}
if(id.find(a) == id.end() || id.find(b) == id.end()) {
puts("-1");
continue;
}
int ret = query(id[a], id[b]);
if(ret == -) puts("-1");
else puts(di[ret].c_str());
}
}
return ;
}

[HIHO1062] 最近公共祖先·一(lca, 并查集, 二分, 神trick)的更多相关文章

  1. [HDOJ2586]How far away?(最近公共祖先, 离线tarjan, 并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 这题以前做过…现在用tarjan搞一发…竟然比以前暴力过的慢………… 由于是离线算法,需要Que ...

  2. 最近公共祖先问题 LCA

    2018-03-10 18:04:55 在图论和计算机科学中,最近公共祖先,LCA(Lowest Common Ancestor)是指在一个树或者有向无环图中同时拥有v和w作为后代的最深的节点. 计算 ...

  3. 最近公共祖先(LCA)---tarjan算法

    LCA(最近公共祖先).....可惜我只会用tarjan去做 真心感觉tarjan算法要比倍增算法要好理解的多,可能是我脑子笨吧略略略 最近公共祖先概念:在一棵无环的树上寻找两个点在这棵树上深度最大的 ...

  4. 最近公共祖先(LCA)学习笔记 | P3379 【模板】最近公共祖先(LCA)题解

    研究了LCA,写篇笔记记录一下. 讲解使用例题 P3379 [模板]最近公共祖先(LCA). 什么是LCA 最近公共祖先简称 LCA(Lowest Common Ancestor).两个节点的最近公共 ...

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

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

  6. 最近公共祖先:LCA及其用倍增实现 +POJ1986

    Q:为什么我在有些地方看到的是最小公共祖先? A:最小公共祖先是LCA(Least Common Ancestor)的英文直译,最小公共祖先与最近公共祖先只是叫法不同. Q:什么是最近公共祖先(LCA ...

  7. 洛谷 3379 最近公共祖先(LCA 倍增)

    洛谷 3379 最近公共祖先(LCA 倍增) 题意分析 裸的板子题,但是注意这题n上限50w,我用的边表,所以要开到100w才能过,一开始re了两发,发现这个问题了. 代码总览 #include &l ...

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

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

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

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

随机推荐

  1. Solved Unable to copy the source file ./installer/services.sh to the destination file /etc/vmware-t

    Sometimes when you intall vmwaretools there will be some problems such as "Unable to copy the s ...

  2. Codeforces Round #350 (Div. 2) D2 二分

    五一期间和然然打的团队赛..那时候用然然的号打一场掉一场...七出四..D1是个数据规模较小的题 写了一个暴力过了 面对数据如此大的D2无可奈何 现在回来看 一下子就知道解法了 二分就可以 二分能做多 ...

  3. Sharepoint 2013 发布功能(Publishing features)

    一.默认情况下,在创建网站集时,只有选择的模板为‘ Publishing Portal(发布门户)’与‘ Enterprise Wiki(企业 Wiki)’时才默认启用发布功能,如下图所示: 二.发布 ...

  4. jdk动态代理学习

    在jdk的好多底层代码中很多都使用jdk的动态代理,下面就写写简单的代码来look look. 老规矩先上代码: public interface SayDao { public String say ...

  5. XAMPP PHP redis 扩展

    1.php增加redis扩展 echo phpinfo();exit;查看php 版本以及 vc运行库 可知 X86 MSVC11  PHP5.6   首先把对应版本的php_redis.dll 和 ...

  6. mvc路由注意事项

    路由表中你增加的路由顺序是很重要的.我们自定义路由是增加在默认路由之前的. 假如你搞反了,那默认路由将永远替代调用自定义路由.

  7. SQL 的 ISNULL 与 NULLIF 运算符

    1.  ISNULL ISNULL(check_expression, replacement_value) 作用: 检查第一个参数是否为null. check_expression 与 replac ...

  8. Extjs 图片的自动缩放

    function resizeImage(obj) { var width = Ext.getCmp('welcome').getWidth(); //welcome 为一Panel的id 分割线下的 ...

  9. git设置hooks 钩子

    github是可以设置hooks的,看:在设置webhooks & services,可在Just the push event.是设定向你的服务器发请求,然后再做相应的处理. https:/ ...

  10. 借助Glances Monitor,密切关注你的系统

    两种方法安装 glances 通常可以有两种方法安装 glances.第一种是通过编译源代码的方式,这种方法比较复杂另外可能会遇到软件包依赖性问题.还有一种是使用特定的软件包管理工具来安装 glanc ...