【题目链接】

点击打开链接

【算法】

离线tarjan求最近公共祖先

【代码】

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 910 int i,n,m,u,v,s,root,tot;
int head[MAXN],fa[MAXN],f[MAXN],ans[MAXN];
bool visited[MAXN];
vector<int> query[MAXN]; struct Edge
{
int to,nxt;
} e[MAXN]; inline int find(int x)
{
if (f[x] == x) return x;
return f[x] = find(f[x]);
}
inline void tarjan(int u)
{
int i,v;
visited[u] = true;
f[u] = u;
for (i = ; i < query[u].size(); i++)
{
v = query[u][i];
if (visited[v]) ans[find(v)]++;
}
for (i = head[u]; i; i = e[i].nxt)
{
v = e[i].to;
tarjan(v);
f[v] = u;
}
}
inline void add(int u,int v)
{
tot++;
e[tot] = (Edge){v,head[u]};
head[u] = tot;
} int main()
{ while (scanf("%d",&n) != EOF)
{
tot = ;
for (i = ; i <= n; i++)
{
head[i] = ;
fa[i] = ;
ans[i] = ;
visited[i] = false;
query[i].clear();
}
for (i = ; i <= n; i++)
{
scanf("%d : (%d)",&u,&s);
while (s--)
{
scanf("%d",&v);
add(u,v);
fa[v] = u;
}
}
for (i = ; i <= n; i++)
{
if (!fa[i])
root = i;
}
scanf("%d",&m);
for (i = ; i <= m; i++)
{
scanf(" (%d %d)",&u,&v);
query[u].push_back(v);
query[v].push_back(u);
}
tarjan(root);
for (i = ; i <= n; i++)
{
if (ans[i])
printf("%d:%d\n",i,ans[i]);
}
} return ; }

【POJ 1470】 Closest Common Ancestors的更多相关文章

  1. 【51.64%】【POJ 1330】Nearest Common Ancestors

    Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26416 Accepted: 13641 Description A roote ...

  2. 【POJ 1330】 Nearest Common Ancestors

    [题目链接] 点击打开链接 [算法] 倍增法求最近公共祖先 [代码] #include <algorithm> #include <bitset> #include <c ...

  3. 【Poj 1330】Nearest Common Ancestors

    http://poj.org/problem?id=1330 题目意思就是T组树求两点LCA. 这个可以离线DFS(Tarjan)-----具体参考 O(Tn) 0ms 还有其他在线O(Tnlogn) ...

  4. 【POJ1470】Closest Common Ancestors

    Description Write a program that takes as input a rooted tree and a list of pairs of vertices. For e ...

  5. 【LCA/tarjan】POJ1470-Closest Common Ancestors

    [题意] 给出一棵树和多组查询,求以每个节点为LCA的查询数有多少? [错误点] ①读入的时候,注意它的空格是随意的呀!一开始不知道怎么弄,后来看了DISCUSS区大神的话: 询问部分输入: scan ...

  6. 【LCA倍增】POJ1330-Nearest Common Ancestors

    [知识点:离线算法&在线算法] 一个离线算法,在开始时就需要知道问题的所有输入数据,而且在解决一个问题后就要立即输出结果. 一个在线算法是指它可以以序列化的方式一个个的处理输入,也就是说在开始 ...

  7. POJ 1470 Closest Common Ancestors 【LCA】

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

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

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

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

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

随机推荐

  1. google查找笔记

    1.可以选择MDN.WIKI等权威的网址资料

  2. ubuntu 安装python 编程环境

    1. 安装python sudo add-apt-repository ppa:fkrull/deadsnakessudo apt-get updatesudo apt-get install pyt ...

  3. 【转载】Raft 为什么是更易理解的分布式一致性算法

    一致性问题可以算是分布式领域的一个圣殿级问题了,关于它的研究可以回溯到几十年前. 拜占庭将军问题 Leslie Lamport 在三十多年前发表的论文<拜占庭将军问题>(参考[1]). 拜 ...

  4. CEO的作用

    看到有人讨论CEO的作用. 一个观点认为CEO有三大任务: 1)为公司确定战略,并与股东沟通 2)为公司其他职位找来合适的人员 3)保证公司随时有足够的钱 他认为,可能CEO会有其他的作用,但是这三点 ...

  5. Thawte SSL Web Server 多域型SSL证书

    Thawte SSL Web Server 多域型SSL证书,最多支持25个域名,需要验证域名所有权和申请单位信息,属于企业验证型SSL证书,提供40位/56位/128位,最高支持256位自适应加密. ...

  6. STM32F407 GPIO原理 个人笔记

    datasheet(STM32F407ZGT6.pdf)中,IO structure 为FT,表示容忍5V电压 后面的uart1_TX之类,表示端口复用 共有A~G7组IO口, 每组16个IO口:0~ ...

  7. Fiddler简介与Web抓包,远程抓包(IE、360、谷歌、火狐)

    Fiddler简介以及web抓包 一.Fiddler简介 简单来说,Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯.网上简介很多,我们不多说. 二 ...

  8. 计算1+2+...+100之和<for循环的学习>

    #include <stdio.h> /* 计算1+2+....+100 soulsjie 20170525 */ void main(){ int i; int s=0; for(i=0 ...

  9. iLBC 压缩比

    *---------------------------------------------------* *                                             ...

  10. [HDU5709]Claris Loves Painting(动态开点线段树+合并)

    题意:有n(<=1e5)个点的树,每个点都有颜色(颜色可能重复),有m(<=1e5)个询问,每次询问(x,d)问在x的子树中,与x的距离不超过d的节点有多少种不同的颜色.强制要求在线. 分 ...