POJ - 1470 Closest Common Ancestors(离线Tarjan算法)
1、输出测试用例中是最近公共祖先的节点,以及这个节点作为最近公共祖先的次数。
2、最近公共祖先,离线Tarjan算法
3、
/*
POJ 1470
给出一颗有向树,Q个查询
输出查询结果中每个点出现次数
*/
/*
离线算法,LCATarjan
复杂度O(n+Q);
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std; const int MAXN=;
const int MAXQ=;//查询数的最大值 //并查集部分
int F[MAXN];//需要初始化为-1
int find(int x){
if(F[x]==-)return x;
return F[x]=find(F[x]);
}
void bing(int u,int v){
int t1=find(u);
int t2=find(v);
if(t1!=t2)
F[t1]=t2;
}
//***********************
bool vis[MAXN];//访问标记
int ancestor[MAXN];//祖先
struct Edge{
int to,next;
}edge[MAXN*];
int head[MAXN],tot;
void addedge(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
} struct Query{
int q,next;
int index;//查询编号
}query[MAXQ*];
int answer[MAXQ];//存储最后的查询结果,下标0 Q-1
int h[MAXQ];
int tt;
int Q; void add_query(int u,int v,int index){
query[tt].q=v;
query[tt].next=h[u];
query[tt].index=index;
h[u]=tt++;
query[tt].q=u;
query[tt].next=h[v];
query[tt].index=index;
h[v]=tt++;
} void init(){
tot=;
memset(head,-,sizeof(head));
tt=;
memset(h,-,sizeof(h));
memset(vis,false,sizeof(vis));
memset(F,-,sizeof(F));
memset(ancestor,,sizeof(ancestor));
}
void LCA(int u){
ancestor[u]=u;
vis[u]=true;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
if(vis[v])continue;
LCA(v);
bing(u,v);
ancestor[find(u)]=u;
}
for(int i=h[u];i!=-;i=query[i].next){
int v=query[i].q;
if(vis[v]){
answer[query[i].index]=ancestor[find(v)];
}
}
}
bool flag[MAXN];
int Count_num[MAXN];
int main(){
int n;
int u,v,k;
while(scanf("%d",&n)==){
init();
memset(flag,false,sizeof(flag));
for(int i=;i<=n;i++){
scanf("%d:(%d)",&u,&k);
while(k--){
scanf("%d",&v);
flag[v]=true;
addedge(u,v);
addedge(v,u);
}
}
scanf("%d",&Q);
for(int i=;i<Q;i++){
char ch;
cin>>ch;
scanf("%d %d)",&u,&v);
add_query(u,v,i);
}
int root;
for(int i=;i<=n;i++)
if(!flag[i]){
root=i;
break;
}
LCA(root);
memset(Count_num,,sizeof(Count_num));
for(int i=;i<Q;i++)
Count_num[answer[i]]++;
for(int i=;i<=n;i++)
if(Count_num[i]>)
printf("%d:%d\n",i,Count_num[i]);
}
return ;
}
POJ - 1470 Closest Common Ancestors(离线Tarjan算法)的更多相关文章
- POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)
POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...
- 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,离线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
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 17306 Ac ...
- poj——1470 Closest Common Ancestors
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 20804 Accept ...
- POJ 1470 Closest Common Ancestors(LCA&RMQ)
题意比较费劲:输入看起来很麻烦.处理括号冒号的时候是用%1s就可以.还有就是注意它有根节点...Q次查询 在线st算法 /*************************************** ...
- POJ 1470 Closest Common Ancestors (最近公共祖先LCA 的离线算法Tarjan)
Tarjan算法的详细介绍,请戳: http://www.cnblogs.com/chenxiwenruo/p/3529533.html #include <iostream> #incl ...
- POJ 1470 Closest Common Ancestors (模板题)(Tarjan离线)【LCA】
<题目链接> 题目大意:给你一棵树,然后进行q次询问,然后要你统计这q次询问中指定的两个节点最近公共祖先出现的次数. 解题分析:LCA模板题,下面用的是离线Tarjan来解决.并且为了代码 ...
随机推荐
- HDU1272 迷宫通路数
Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该 ...
- Lucene、Compass学习以及与SSH的整合
一.准备 个人在学习中采用Struts2 + Hibernate3.2 + Spring2.5 + Compass2.2.0, 一下图片为本次学习中用到的jar包: 图中圈出的jar包为本次学习的主要 ...
- 【BZOJ2006】超级钢琴(RMQ,priority_queue)
题意: 思路: 用三元组(i, l, r)表示右端点为i,左端点在[l, r]之间和最大的区间([l, r]保证是对于i可行右端点区间的一个子区间),我们用堆维护一些这样的三元组. 堆中初始的元素为每 ...
- java遍历文件夹及所有子文件
以前写代码循环文件夹和子文件时,总是自己写递归访问,今天研究lucene时,发现JDK给我们已经提供了访问遍历的方法,上代码: String str = "C:\\Users\\LLY\\D ...
- ThinkPHP __construct和_initialize的使用
ThinkPHP框架中的__construct和_initialize的使用 父类(PlatformController.class.php): class PlatformController ex ...
- strcpy c标准库函数
C语言标准库函数strcpy,把从src地址开始且含有NULL结束符的字符串复制到以dest开始的地址空间. 已知strcpy函数的原型是: char *strcpy(char *dst, const ...
- bzoj4161 (k^2logn求线性递推式)
分析: 我们可以写把转移矩阵A写出来,然后求一下它的特征多项式,经过手动计算应该是这样的p(x)=$x^k-\sum\limits_{i=1}^ka_i*x^{k-i}$ 根据Cayley-Hamil ...
- Linux查看日志三种命令(转载)
第一种:查看实时变化的日志(比较吃内存) 最常用的: tail -f filename (默认最后10行,相当于增加参数 -n 10) Ctrl+c 是退出tail命令 其他情况: tail -n 2 ...
- 【python】SHA1 算法
http://blog.163.com/sh_wenfen/blog/static/99708242007231103936938/
- Office WORD如何为每一页设置不同的页眉页脚
如下图所示,我想要为封面和目录,摘要等等设置不同的页眉页脚(一般封面和目录不需要页脚) 而从正文开始,套用相同的页眉和以页数作为页脚(注意"第一章 绪论"不是这个文档的第一页) ...