【POJ 1470】 Closest Common Ancestors
【题目链接】
【算法】
离线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的更多相关文章
- 【51.64%】【POJ 1330】Nearest Common Ancestors
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26416 Accepted: 13641 Description A roote ...
- 【POJ 1330】 Nearest Common Ancestors
[题目链接] 点击打开链接 [算法] 倍增法求最近公共祖先 [代码] #include <algorithm> #include <bitset> #include <c ...
- 【Poj 1330】Nearest Common Ancestors
http://poj.org/problem?id=1330 题目意思就是T组树求两点LCA. 这个可以离线DFS(Tarjan)-----具体参考 O(Tn) 0ms 还有其他在线O(Tnlogn) ...
- 【POJ1470】Closest Common Ancestors
Description Write a program that takes as input a rooted tree and a list of pairs of vertices. For e ...
- 【LCA/tarjan】POJ1470-Closest Common Ancestors
[题意] 给出一棵树和多组查询,求以每个节点为LCA的查询数有多少? [错误点] ①读入的时候,注意它的空格是随意的呀!一开始不知道怎么弄,后来看了DISCUSS区大神的话: 询问部分输入: scan ...
- 【LCA倍增】POJ1330-Nearest Common Ancestors
[知识点:离线算法&在线算法] 一个离线算法,在开始时就需要知道问题的所有输入数据,而且在解决一个问题后就要立即输出结果. 一个在线算法是指它可以以序列化的方式一个个的处理输入,也就是说在开始 ...
- 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)
POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...
- POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13372 Accept ...
随机推荐
- [Python3网络爬虫开发实战] 1.9.5-Scrapyrt的安装
Scrapyrt为Scrapy提供了一个调度的HTTP接口,有了它,我们就不需要再执行Scrapy命令而是通过请求一个HTTP接口来调度Scrapy任务了.Scrapyrt比Scrapyd更轻量,如果 ...
- 集训第四周(高效算法设计)L题 (背包贪心)
Description John Doe is a famous DJ and, therefore, has the problem of optimizing the placement of ...
- python面向对象编程设计与开发
一.什么是面向对象的程序设计 1.何为数据结构? 数据结构是指相互之间存在一种或多种特定关系的数据元素的集合,如列表.字典. 2.何为编程? 编程是指程序员用特定的语法+数据结构+算法,组成的代码,告 ...
- 【XML】-- C#读取XML中元素和属性的值
Xml是扩展标记语言的简写,是一种开发的文本格式. 啰嗦几句儿:老师布置的一个小作业却让我的脑细胞死了一堆,难的不是代码,是n多嵌套的if.foreach,做完这个,我使劲儿想:我一女孩,没有更多女孩 ...
- Git和SVN共存的方法
刚工作的时候都是用的cvs和svn,对git不熟悉,随着工作的需要,打分支和版本管理的需要,熟悉起来了git,这一用不可收拾,比svn远远好用,尤其是版本分支管理上,切换分支的方便性,现在这家公司还是 ...
- poj1655(dfs,树形dp,树的重心)(点分治基础)
题意:就是裸的求树的重心. #include<cstring> #include<algorithm> #include<cmath> #include<cs ...
- Mysql Replace语句的使用
Mysql Replace语句的语法: REPLACE [LOW_PRIORITY | DELAYED] [INTO] tbl_name [(col_name,...)] VALUES ({expr ...
- oracle链接不上的问题
使用plSql连接数据库看看,登录提示如下:ORA-12514:TNS:监听程序当前无法识别连接描述符中请求的服务. 查了许久的baidu也没有解决问题的方法.想起来看看oracle的服务是否开启,O ...
- io计算
http://www.cnblogs.com/yalong_xiang/archive/2011/11/15/2249530.html ぬ儱←OWEN★ windows下如何查看磁盘IO性能 复制 ...
- AIM Tech R3 div2 E Centroid
思路很明显了,假设是点x,则看它的子树中是否有大于n/2的,如果有,则在该子树中剪去它可以剪的且小于n/2的,接到点x上. 则统计出在以x点为根的子树中,它的各子树可以剪去的且小于n/2的最大子子树. ...