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. R语言的字符串处理

    R语言字符串的拼接 content<-paste("124235","789","124",sep="@") 运行 ...

  2. vim命令收集(持续中)

    保存: 按ESC键 跳到命令模式,然后: :w 保存文件但不退出vi:w file 将修改另外保存到file中,不退出vi:w! 强制保存,不推出vi:wq 保存文件并退出vi:wq! 强制保存文件, ...

  3. [译]rabbitmq 2.4 Multiple tenants: virtual hosts and separation

    我对rabbitmq学习还不深入,这些翻译仅仅做资料保存,希望不要误导大家. With exchanges, bindings, and queues under your belt, you mig ...

  4. iOS学习之界面通信

    一.属性传值 在SecondViewController.h里 #import <UIKit/UIKit.h> @interface SecondViewController : UIVi ...

  5. UIDatePicker倒计时 swift

    // // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...

  6. P1230: [Usaco2008 Nov]lites 开关灯

    嗯嗯,这是一道线段树的题,询问区间内亮着的灯的个数,我们可以把区间修改的线段树改一下,原本的求和改成若有奇数次更改则取反(总长度-亮着的灯个数),而判断是否奇数次只要数组加一个delta的值,upda ...

  7. jQuery插件开发总结

    jQuery插件的开发包括两种: 一种是类级别的插件开发$.extend,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法,比如:$.ajax, $.getJSON等.jQuery ...

  8. htop

    apt-get install htop

  9. 搭建SpringMVC+MyBatis开发框架二

    配置web.xml 用如下配置覆盖原有的web.xml内容 <?xml version="1.0" encoding="UTF-8"?> <w ...

  10. 8、android代码优化技术记录

    1.length.length().size的优化 举例: int array_one[] = {1,2,3,4,5,6,7,8,9,10}; int array_two[] = {1,2,3,4,5 ...