Tarjan算法的详细介绍,请戳:

http://www.cnblogs.com/chenxiwenruo/p/3529533.html

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <string>
#include <vector>
/*
AC
一开始读取数据的方式并不好,运行900多ms。
后来参照了别人的读取方式,600+ms。
*/
using namespace std;
const int maxn=;
int n,m;
int anc[maxn]; //记录以i为公共祖先的个数对
int indegree[maxn]; //记录入度
int vis[maxn];
vector<int> query[maxn]; //存储要查询的对 int head[maxn];
int tot; struct Edge{
int to,next;
}edge[maxn]; void add(int i,int j){
edge[tot].next=head[i];
edge[tot].to=j;
head[i]=tot++;
}
//并查集
struct UF{
int fa[maxn];
void init(){
for(int i=;i<=n;i++)
fa[i]=i;
}
int find_root(int x){
if(fa[x]!=x)
fa[x]=find_root(fa[x]);
return fa[x];
}
void Union(int u,int v){
fa[v]=fa[u];
}
}uf; void LCA(int u){
int v;
for(int k=head[u];k!=-;k=edge[k].next){
v=edge[k].to;
LCA(v);
uf.Union(u,v);
}
vis[u]=;
for(int i=;i<query[u].size();i++){
v=query[u][i];
if(vis[v]){
anc[uf.fa[uf.find_root(v)]]++;
}
}
}
int main()
{
int u,v,num,root;
char ch;
while(scanf("%d",&n)!=EOF){
tot=;
memset(head,-,sizeof(head));
memset(indegree,,sizeof(indegree));
for(int i=;i<maxn;i++)
query[i].clear();
for(int i=;i<=n;i++){
scanf("%d:(%d)",&u,&num); //scanf的读取太强了
for(int j=;j<=num;j++){
scanf("%d",&v);
add(u,v);
indegree[v]++;
}
}
scanf("%d",&m);
while(m--){//这个读取方法比较妙
while(getchar()!='(');
scanf("%d%d",&u,&v);
query[u].push_back(v);
query[v].push_back(u);
}
while(getchar()!=')'); //别忘了读取最后的')' //寻找根节点
for(int i=;i<=n;i++)
if(!indegree[i])
root=i;
memset(vis,,sizeof(vis));
memset(anc,,sizeof(anc));
uf.init();
LCA(root);
for(int i=;i<=n;i++){
if(anc[i]){
printf("%d:%d\n",i,anc[i]);
}
}
}
return ;
}

POJ 1470 Closest Common Ancestors (最近公共祖先LCA 的离线算法Tarjan)的更多相关文章

  1. POJ1470Closest Common Ancestors 最近公共祖先LCA 的 离线算法 Tarjan

    该算法的详细解释请戳: http://www.cnblogs.com/Findxiaoxun/p/3428516.html #include<cstdio> #include<alg ...

  2. POJ 1330 Nearest Common Ancestors (最近公共祖先LCA + 详解博客)

    LCA问题的tarjan解法模板 LCA问题 详细 1.二叉搜索树上找两个节点LCA public int query(Node t, Node u, Node v) { int left = u.v ...

  3. POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)

    POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...

  4. POJ 1470 Closest Common Ancestors 【LCA】

    任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000 ...

  5. POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13372   Accept ...

  6. POJ 1470 Closest Common Ancestors

    传送门 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 17306   Ac ...

  7. POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13370   Accept ...

  8. poj——1470 Closest Common Ancestors

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 20804   Accept ...

  9. POJ1330Nearest Common Ancestors最近公共祖先LCA问题

    用的离线算法Tarjan 该算法的详细解释请戳 http://www.cnblogs.com/Findxiaoxun/p/3428516.html 做这个题的时候,直接把1470的代码copy过来,改 ...

随机推荐

  1. Android体系结构

    由图可知,android被分成4个层次,以linux为核心,针对手机进行专门的优化,提供了android操作系统最基本的功能,在此之上又分为android runtime和libraries.其中Da ...

  2. 苹果HomeKit如何牵动全国智能硬件格局

    苹果HomeKit如何牵动全国智能硬件格局 2014-06-23 15:48 发表      系统分类:消费电子      自定义分类:默认 标签:智能家居 苹果在6月的WWDC开发者大会上发布了包括 ...

  3. ssh公钥免密码登录

    1,生成公钥 ssh-keygen -t rsa 2,上传至服务器 将个人电脑的公钥添加到服务器 cat id_rsa.pub >> ~/.ssh/authorized_keys 3,本地 ...

  4. [译]rabbitmq 2.1 Consumers and producers (not an economics lesson)

    我对rabbitmq学习还不深入,这些翻译仅仅做资料保存,希望不要误导大家. For now, all you need to know is that producers create messag ...

  5. 用Swift重写公司OC项目(Day2)--创建OC与Swift的桥接文件,进而调用OC类库

    昨天把项目中的图标以及启动转场图片弄好了,那么今天,我们可以开始慢慢进入到程序的编写当中了. 由于swift较新,所以类库还不够完善,但是不用担心,苹果早就出了解决方案,那就是使用桥接文件,通过桥接文 ...

  6. MVC4.0 WebApi如何设置api支持namespace

    1.自定义HttpControllerSelector /// <summary> /// 设置api支持namespace /// </summary> public cla ...

  7. 17.Quartus 怎么回读CPLD里面的东西

    可以使用Quartus® II Programmer的“Examine”特性回读编程目标文件(.POF)是CPLD不是FPGA 先用auto检测加没加加密位,然后执行ex,然后save,Examine ...

  8. < java.util >-- Set接口

    Set接口中的方法和Collection中方法一致的.Set接口取出方式只有一种,迭代器. |--HashSet:底层数据结构是哈希表,线程是不同步的.无序,高效: HashSet集合保证元素唯一性: ...

  9. CommonsChunkPlugin的使用(关于angular2中的polyfills和vendor的疑问解决)

    seed: angular2-webpack-starter(在github上可以找到) polyfills:提供api以方便兼容不同的浏览器 vendor:项目插件扩展 在学习ng2中一直不明白为什 ...

  10. web 性能忧化(IIS篇)

    1. 调整IIS 7应用程序池队列长度 由原来的默认1000改为65535. IIS Manager > ApplicationPools > Advanced Settings 2.   ...