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来解决.并且为了代码 ...
随机推荐
- 使用PL/SQL将sql脚本数据导入数据库
一. PL/SQL登录到数据库,使用tools工具进行导入.使用plsql登录到需要导入数据的数据库.点击工具栏上[tools]--[Import tables] 二.commit;
- python008 Python3 字符串
var1 = 'Hello World!' var2 = "QQ603374730" Python 访问字符串中的值Python 不支持单字符类型,单字符也在Python也是作为一 ...
- 基于神经网络的embeddding来构建推荐系统
在之前的博客中,我主要介绍了embedding用于处理类别特征的应用,其实,在学术界和工业界上,embedding的应用还有很多,比如在推荐系统中的应用.本篇博客就介绍了如何利用embedding来构 ...
- bzoj 1702 贪心,前缀和
[Usaco2007 Mar]Gold Balanced Lineup 平衡的队列 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 807 Solved: ...
- msp430项目编程02
msp430中项目---液晶1602显示 1.液晶1602工作原理 2.电路原理说明 3.代码(静态显示) 4.代码(动态显示) 5.项目总结 msp430项目编程 msp430入门学习
- 视图中使用foreach报错问题
问题情境:thinkphp3.2版本,使用四层<foreach></foreach>循环变量时,报错以下错误: syntax error, unexpected 'endfor ...
- python学习之 - XML
xml模块定义:实现不同语言或程序之间进行数据交换的协议.格式如下:通过<>节点来区别数据结构如:<load-on-startup(这个是标签) test="value&q ...
- 工具--IIS Express
iisexpress-proxy https://github.com/icflorescu/iisexpress-proxy 适用于联调,比如app调用接口,开启端口后,app调用接口后会直接进入端 ...
- Download Software Top 10
We are quite rich in terms of web browsers! Nevertheless, it's a bit surprising to know that even so ...
- 鸟哥的Linux私房菜-----13、账号管理