【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 ...
随机推荐
- 集训第四周(高效算法设计)J题 (中途相遇法)
Description The SUM problem can be formulated as follows: given four lists A, B, C, D<tex2html_ ...
- HTML、CSS常用技巧
一.HTML 在介绍HTML之前,我们先看一下HTML的文档树结构,主要包括哪些: (一).头部标签 1,Doctype Doctype告诉浏览器使用什么样的HTML或XHTML规范来解析HTML文档 ...
- 在linux服务器上搭建Struts2项目运行环境
服务器上工作: 首先要先装java https://www.cnblogs.com/lamp01/p/8932740.html 然后装好tomcat https://www.cnblogs.com/y ...
- 【转】Comet:基于 HTTP 长连接的“服务器推”技术
原文链接:http://www.ibm.com/developerworks/cn/web/wa-lo-comet/ 很多应用譬如监控.即时通信.即时报价系统都需要将后台发生的变化实时传送到客户端而无 ...
- js获取指定字符前/后的字符串简单实例
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- xtu summer individual-4 D - Martian Strings
Martian Strings Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- WebLoad 脚本的用法
WebLoad 对于模拟一个HTTP 请求,一般都是 由以下三部分组成,并把这三部分包在一个Transaction里(从BeginTransation 到 EndTransaction 为止): ...
- HDU1024 多段最大和 DP
题目大意: 在n个数,求不重复的m段中的数据总和的最大值 令dp[i][j]表示将前j个数分成 i 段时得到的最大值(必取到第 j 个数) 状态转移可列为 dp[i][j]=Max(dp[i][j-1 ...
- POJ2367 拓扑排序 裸题 板子题
http://poj.org/problem?id=2367 队列版 #include <stdio.h> #include <math.h> #include <str ...
- Uva -1515 Pool construction(最小割)
输入一个字符矩阵,'.'代表洞,'#'代表草地.可以把草改成洞花费为d,或者把洞改成草花费为f,最后还要在草和洞之间修围栏花费为b. 首先把最外一圈的洞变成草,并累加花费. 增加一个源点和一个汇点,源 ...