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来解决.并且为了代码 ...
随机推荐
- Leetcode 319.灯泡开关
灯泡开关 初始时有 n 个灯泡关闭.第 1 轮,你打开所有的灯泡.第 2 轮,每两个灯泡你关闭一次.第 3 轮,每三个灯泡切换一次开关(如果关闭则开启,如果开启则关闭).第 i 轮,每 i 个灯泡切换 ...
- Android TransitionDrawable:过渡动画Drawable
Android TransitionDrawable实现一种可以用动画表示的Drawable.写一个例子. package zhangphil.app; import android.graphics ...
- HDU-1087Super Jumping! Jumping! Jumping!
Super Jumping! Jumping! Jumping! ...
- Android应用程序项目结构
Android应用程序项目结构 [src]:JAVA源代码目录 [gen]:由系统自动生成的JAVA源码文件,不可修改,只可查看和使用 加载的和依赖的类库 [assets]:本地存储文件的一个文件夹 ...
- 有向图欧拉回路个数 BEST定理
有向图欧拉回路个数 BZOJ 3659 但是没有这道题了 直接贴一个别人的板子吧 欧拉回路:存在一条路径经过所有的边刚好1次 有向图欧拉回路存在充要条件:①图连通:②对于所有点都满足出度=入度 BE ...
- Codeforces 658D Bear and Polynomials【数学】
题目链接: http://codeforces.com/contest/658/problem/D 题意: 给定合法多项式,改变一项的系数,使得P(2)=0,问有多少种方法? 分析: 暴力求和然后依次 ...
- Codeforces 653D Delivery Bears【二分+网络流】
题目链接: http://codeforces.com/problemset/problem/653/D 题意: x个熊拿着相同重量的物品,从1号结点沿着路走到N号结点,结点之间有边相连,保证可以从1 ...
- 关于oracle存储过程的若干问题备忘
1.在oracle中,数据表别名不能加as,如: select a.appname from appinfo a;-- 正确select a.appname from appinfo as a;-- ...
- 简述HashMap和Hashtable的差别
1.HashMap继承AbstractMap类. Hashtable继承了Dictionary类. 2.HashMap同意有null的键和值. Hashtable不同意有null的键和值. ...
- 【转】ubuntu 下安装mongodb php 拓展的方法
按照上面的方法安装成功之后,写一个 mongodb 的php测试脚本,用来测试是否可以 正确连接上mongodb ,并查询结果. 参考:http://php.net/manual/en/class.m ...