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. WPF中多窗口共享静态属性

    由于我的DoubanFm在重新考虑之后,需要设置一个全局的CurrentSong,这个字段要让所有的VM都知道,而我同时又想把它作为我所有VM的共有属性.而且我想尽量减少代码的复制,提高重用.所以我做 ...

  2. hdu 4609 3-idiots <FFT>

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=4609 题意: 给定 N 个正整数, 表示 N 条线段的长度, 问任取 3 条, 可以构成三角形的概率为多 ...

  3. ASP.NET内置对象之Request传递请求对象

    Request对象是HttpRequest类的一个实例,Request对象用于读取客户端在Web请求期间发送的HTTP值.Request对象常用的属性如下所示. q      QueryString: ...

  4. Socket(2)

    UDP: UDP协议没有socket之间的虚拟链路,也就是说没有“握手”阶段,只是发送接收. 可以想象成直播,直播时如果中间网络不好,不会事后重新播放之前的,而是直接跳过. 也就是说一方只负责发送,一 ...

  5. asp.net实现手机号码归属地查询

    protected void Button1_Click(object sender, EventArgs e)        {            if (Regex.IsMatch(TextB ...

  6. Java之蛋疼的file Protocol

    file Protocol Opens a file on a local or network drive. Syntax file:///sDrives[|sFile] Tokens sDrive ...

  7. linux 禁止指定账号ssh登陆

    1 2 3 4 vim /etc/pam.d/sshd   #在第一行添加以下代码 auth       required     pam_listfile.so item=user sense=de ...

  8. 树莓派最简易Wifi配置

    树莓派最简易Wifi配置 相信我,连博客都会偷懒写个最简易给你看 前提,只有一根网线没有网络的前提下进行的. 基于Win10系统和树莓派2015-05-05-raspbian-wheezy.img测试 ...

  9. [原]Java修炼 之 基础篇(二)Java语言构成

    上次的博文中Java修炼 之 基础篇(一)Java语言特性我们介绍了一下Java语言的几个特性,今天我们介绍一下Java语言的构成.        所谓的Java构成,主要是指Java运行环境的组成, ...

  10. LeetCode-Largest Divisble Subset

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...