poj1470 Closest Common Ancestors [ 离线LCA tarjan ]
Time Limit: 2000MS | Memory Limit: 10000K | |
Total Submissions: 14915 | Accepted: 4745 |
Description
Input
nr_of_vertices
vertex:(nr_of_successors) successor1 successor2 ... successorn
...
where vertices are represented as integers from 1 to n ( n <= 900 ). The tree description is followed by a list of pairs of vertices, in the form:
nr_of_pairs
(u v) (x y) ...
The input file contents several data sets (at least one).
Note that white-spaces (tabs, spaces and line breaks) can be used freely in the input.
Output
For example, for the following tree:

Sample Input
5
5:(3) 1 4 2
1:(0)
4:(0)
2:(1) 3
3:(0)
6
(1 5) (1 4) (4 2)
(2 3)
(1 3) (4 3)
Sample Output
2:1
5:5
Hint
Source
思路:离线lca ,利用tarjan算法。
tarjan算法的步骤是(当dfs到节点u时):
1. 在并查集中建立仅有u的集合,设置该集合的祖先为u
2. 对u的每个孩子v:
2.1 tarjan之
2.2 合并v到父节点u的集合,确保集合的祖先是u
3. 设置u为已遍历
4. 处理关于u的查询,若查询(u,v)中的v已遍历过,则LCA(u,v)=v所在的集合的祖先
14006525 | njczy2010 | 1470 | Accepted | 2992K | 516MS | G++ | 1979B | 2015-03-25 19:29:20 |
#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
#include <algorithm> #define ll long long
int const N = ;
int const M = ;
int const inf = ;
ll const mod = ; using namespace std; int n,m;
vector<int> bian[N];
vector<int> query[N];
int cnt[N];
int fa[N];
int vis[N];
int degree[N]; int findfa(int x)
{
return fa[x] == x ? fa[x] : fa[x] = findfa(fa[x]);
} void ini()
{
int i,j;
int k,u,v;
memset(cnt,,sizeof(cnt));
memset(vis,,sizeof(vis));
memset(degree,,sizeof(degree));
for(i=;i<=n;i++){
bian[i].clear();
query[i].clear();
fa[i]=i;
}
for(i=;i<=n;i++){
scanf("%d:(%d)",&u,&k);
for(j=;j<k;j++){
scanf("%d",&v);
bian[u].push_back(v);
degree[v]++;
}
}
scanf("%d",&m);
for(i=;i<=m;i++){
while(getchar()!='(') ;
scanf("%d%d",&u,&v);
while(getchar()!=')') ;
query[u].push_back(v);
query[v].push_back(u);
}
} void tarjan(int u,int f)
{
vector<int>::iterator it;
int v;
for(it=bian[u].begin();it!=bian[u].end();it++){
v=*it;
tarjan(v,u);
} for(it=query[u].begin();it!=query[u].end();it++){
v=*it;
if(vis[v]==) continue;
cnt[ findfa(v) ]++;
}
vis[u]=;
fa[u]=f;
} void solve()
{
int i;
for(i=;i<=n;i++){
if(degree[i]==){
tarjan(i,-);
}
}
} void out()
{
int i;
for(i=;i<=n;i++){
// printf("%d:%d\n",i,cnt[i]);
if(cnt[i]!=){
printf("%d:%d\n",i,cnt[i]);
}
}
} int main()
{
//freopen("data.in","r",stdin);
//scanf("%d",&T);
// for(cnt=1;cnt<=T;cnt++)
//while(T--)
while(scanf("%d",&n)!=EOF)
{
ini();
solve();
out();
}
}
poj1470 Closest Common Ancestors [ 离线LCA tarjan ]的更多相关文章
- POJ 1470 Closest Common Ancestors【LCA Tarjan】
题目链接: http://poj.org/problem?id=1470 题意: 给定若干有向边,构成有根数,给定若干查询,求每个查询的结点的LCA出现次数. 分析: 还是很裸的tarjan的LCA. ...
- POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13372 Accept ...
- POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13370 Accept ...
- POJ 1470 Closest Common Ancestors 【LCA】
任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000 ...
- poj----(1470)Closest Common Ancestors(LCA)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 15446 Accept ...
- ZOJ 1141:Closest Common Ancestors(LCA)
Closest Common Ancestors Time Limit: 10 Seconds Memory Limit: 32768 KB Write a program that tak ...
- POJ1470 Closest Common Ancestors
LCA问题,用了离线的tarjan算法.输入输出参考了博客http://www.cnblogs.com/rainydays/archive/2011/06/20/2085503.htmltarjan算 ...
- POJ 1470 Closest Common Ancestors (模板题)(Tarjan离线)【LCA】
<题目链接> 题目大意:给你一棵树,然后进行q次询问,然后要你统计这q次询问中指定的两个节点最近公共祖先出现的次数. 解题分析:LCA模板题,下面用的是离线Tarjan来解决.并且为了代码 ...
- POJ1470 Closest Common Ancestors 【Tarjan的LCA】
非常裸的模版题,只是Tarjan要好好多拿出来玩味几次 非常有点巧妙呢,tarjan,大概就是当前结点和它儿子结点的羁绊 WA了俩小时,,,原因是,这个题是多数据的(还没告诉你T,用scanf!=EO ...
随机推荐
- fetch和axios区别,proxy代理配置
1.今天使用fetch调用接口时使用console.log(res.data)始终是undefined,使用anxios请求则可以成功请求到数据,非常奇怪,于是查了一圈,才搞明白是我自以为了,哎,浪费 ...
- PMP项目管理学习笔记(3)——过程框架
所有项目,不论大小,都可以分解为过程组: 如果项目规模很大,可以分阶段管理,每个阶段都要经过上面的五个过程组,从启动到收尾. 阶段的类型: 顺序阶段: 一个阶段完成后进入下个阶段. 重叠阶段: 有时需 ...
- QTableWidget表头样式
转载请注明出处:http://www.cnblogs.com/dachen408/p/7742680.html QTableView { background-color: rgba(255, 255 ...
- SQLite -分离数据库
SQLite -分离数据库 SQLite分离DTABASE语句用于分离和分离命名数据库从一个数据库连接之前附加使用附加语句.如果相同的数据库文件已附加多个别名,然后分离命令将断开只有名字和其他依附仍将 ...
- Java8函数式编程和lambda表达式
文章目录函数式编程JDK8接口新特性函数接口方法引用函数式编程函数式编程更多时候是一种编程的思维方式,是一种方法论.函数式与命令式编程区别主要在于:函数式编程是告诉代码你要做什么,而命令式编程则是告诉 ...
- Codeforces Round #539 (Div. 2) C. Sasha and a Bit of Relax(前缀异或和)
转载自:https://blog.csdn.net/Charles_Zaqdt/article/details/87522917 题目链接:https://codeforces.com/contest ...
- billu_b0x靶场刷题
https://www.vulnhub.com/ 里面有很多安全环境,只要下载相关镜像,在虚拟机上面搭建运行就可以练习对应靶场了. 第一步.信息收集 nmap扫描内网开放80端口的存活主机 nmap ...
- vue iview render里面 没有双向绑定 renderHeader 要序列化 反序列 一下
vue iview render里面 没有双向绑定 renderHeader 要序列化 反序列 一下 renderHeader: (h, params) => { return [ h('Rad ...
- zk伪集群部署
jdk 配置 # tar xf jdk-8u161-linux-x64.tar.gz -C /usr/local/ # vim /etc/profile.d/jdk.sh export JAVA_HO ...
- 图片充当li标签列表标志
默认情况下,浏览器使用一个黑圆圈作为列表标志,可以用图片取代它: ul {list-style: none} ul li{ background-image: url("img/logo_0 ...