传送门 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 14915   Accepted: 4745 Description Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the program determines…
题目链接: http://poj.org/problem?id=1470 题意: 给定若干有向边,构成有根数,给定若干查询,求每个查询的结点的LCA出现次数. 分析: 还是很裸的tarjan的LCA. 这题我真的要吐槽!!调试了好久啊!!不是MLE就是RE.... 查询数量没给,所以要用矩阵来存,这样还可以避免重复. 给边的时候不知道会不会有重复的点,所以讲道理应该用vector保存. getchar...我不知道为什么会RE... 其实ance数组可以不用的,只要每次处理子树的时候pa[v]…
Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13372   Accepted: 4340 Description Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the program determines the…
Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13370   Accepted: 4338 Description Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the program determines the…
任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 22519   Accepted: 7137 Description Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pa…
Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 15446   Accepted: 4944 Description Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the program determines the…
Closest Common Ancestors Time Limit: 10 Seconds      Memory Limit: 32768 KB Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the program determines the closest common ancestor of u and v in the tr…
LCA问题,用了离线的tarjan算法.输入输出参考了博客http://www.cnblogs.com/rainydays/archive/2011/06/20/2085503.htmltarjan算法是用了dfs+并查集的方式做的.这里输入输出有个不错的地方,就是用scanf("%[^0-9]", st);跳过非数字.里面用数组g来表示多维的树,还用并查集的id数组的-1来表示是否访问. #include <iostream> #include <cstdio>…
<题目链接> 题目大意:给你一棵树,然后进行q次询问,然后要你统计这q次询问中指定的两个节点最近公共祖先出现的次数. 解题分析:LCA模板题,下面用的是离线Tarjan来解决.并且为了代码的简洁,本代码用的是vector存图. #include<iostream> #include<cstdio> #include<cstring> #include<vector> using namespace std; ; vector<int>…
非常裸的模版题,只是Tarjan要好好多拿出来玩味几次 非常有点巧妙呢,tarjan,大概就是当前结点和它儿子结点的羁绊 WA了俩小时,,,原因是,这个题是多数据的(还没告诉你T,用scanf!=EOF来控制结束),更重要的是和这个和Codeforces不一样,Codeforces的多组数据好像会又一次開始程序似的,不用在程序里面写清零,但这个题是多数据用EOF来控制输入的,多数据在一个文件中都一次输进去了,所以要memset btw,加上一点memset代码,多了700B代码... #incl…