正文
POJ 1470 Closest Common Ancestors(LCA&RMQ)
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
题意比较费劲:输入看起来很麻烦。处理括号冒号的时候是用%1s就可以。还有就是注意它有根节点。。。Q次查询
在线st算法
/*************************************************************************
> File Name: 3.cpp
> Author: Howe_Young
> Mail: 1013410795@qq.com
> Created Time: 2015年10月08日 星期四 19时03分30秒
************************************************************************/ #include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm> using namespace std;
typedef long long ll;
const int maxn = ;
struct Edge {
int to, next;
}edge[maxn<<];
int tot, head[maxn];
int ans[maxn];
int Euler[maxn<<];
int R[maxn];
int dep[maxn];
int dp[maxn<<][]; // RMQ
bool in[maxn];
int cnt;
void init()
{
cnt = ;
tot = ;
memset(head, -, sizeof(head));
memset(ans, , sizeof(ans));
memset(in, false, sizeof(in));
}
void addedge(int u, int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void dfs(int u, int depth)
{
Euler[++cnt] = u;
R[u] = cnt;
dep[cnt] = depth;
for (int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].to;
dfs(v, depth + );
Euler[++cnt] = u;
dep[cnt] = depth;
}
} void RMQ(int n)
{
for (int i = ; i <= n; i++) dp[i][] = i;
int m = (int)(log(n) / log());
for (int j = ; j <= m; j++)
{
for (int i = ; i + ( << j) - <= n; i++)
dp[i][j] = dep[dp[i][j - ]] < dep[dp[i + ( << (j - ))][j - ]] ? dp[i][j - ] : dp[i + ( << (j - ))][j - ];
}
}
int query(int u, int v)
{
int l = R[u], r = R[v];
if (l > r) swap(l, r);
int k = (int)(log(r - l + ) / log());
int lca = dep[dp[l][k]] < dep[dp[r - ( << k) + ][k]] ? dp[l][k] : dp[r - ( << k) + ][k];
return Euler[lca];
}
int main()
{
int n;
while (~scanf("%d", &n))
{
init();
char s1[], s2[];
int u, v, m;
for (int i = ; i < n; i++)
{
scanf("%d %1s %1s %d %1s", &u, s1, s1, &m, s2);
for (int j = ; j < m; j++)
{
scanf("%d", &v);
addedge(u, v);
in[v] = true;
}
}
for (int i = ; i <= n; i++)
if (!in[i])
{
dfs(i, );
break;
}
RMQ(cnt);
int Q;
scanf("%d", &Q);
while (Q--)
{
scanf("%1s %d %d %1s", s1, &u, &v, s2);
ans[query(u, v)]++;
}
for (int i = ; i <= n; i++)
if (ans[i])
printf("%d:%d\n", i, ans[i]);
}
return ;
}
离线tarjan算法:
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = ;//节点
const int maxm = ;//最大查询数
struct Edge {
int to, next;
}edge[maxn * ];
int tot, head[maxn];
struct Query {
int q, next;
int index;
}query[maxm * ];
int cnt, h[maxn];
int fa[maxn];
int r[maxn];
int ancestor[maxn];
int ans[maxm];
int Q;
bool vis[maxn]; void init(int n)
{
Q = ;
tot = ;
cnt = ;
memset(head, -, sizeof(head));
memset(h, -, sizeof(h));
memset(fa, -, sizeof(fa));
memset(ancestor, , sizeof(ancestor));
memset(vis, false, sizeof(vis));
for (int i = ; i <= n; i++) r[i] = ;
}
void addedge(int u, int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void addquery(int u, int v, int index)
{
query[cnt].q = v;
query[cnt].index = index;
query[cnt].next = h[u];
h[u] = cnt++;
}
int find(int x)
{
if (fa[x] == -) return x;
return fa[x] = find(fa[x]);
}
void Union(int x, int y)
{
int tx = find(x);
int ty = find(y);
if (tx != ty)
{
if (tx < ty)
{
fa[tx] = ty;
r[ty] += r[tx];
}
else
{
fa[ty] = tx;
r[tx] += r[ty];
}
}
}
void LCA(int u)
{
vis[u] = true;
ancestor[u] = u;
for (int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].to;
if (vis[v]) continue;
LCA(v);
Union(u, v);
ancestor[find(u)] = u;
}
for (int i = h[u]; i != -; i = query[i].next)
{
int v = query[i].q;
if (vis[v])
{
ans[query[i].index] = ancestor[find(v)];
}
}
}
int res[maxn];
bool in[maxn];
int main()
{
int n;
while (~scanf("%d", &n))
{
init(n);
memset(in, false, sizeof(in));
int u, v, m;
char ch[];
for (int i = ; i <= n; i++)
{
scanf("%d %1s %1s %d %1s", &u, ch, ch, &m, ch);
for (int j = ; j < m; j++)
{
scanf("%d", &v);
in[v] = true;
addedge(u, v);
addedge(v, u);
}
}
int q;
scanf("%d", &q);
while (q--)
{
scanf("%1s %d %d %1s", ch, &u, &v, ch);
addquery(u, v, Q);
addquery(v, u, Q++);
}
int root;
for (int i = ; i <= n; i++)
{
if (!in[i])
{
root = i;
break;
}
}
LCA(root);
memset(res, , sizeof(res));
for (int i = ; i < Q; i++)
res[ans[i]]++;
for (int i = ; i <= n; i++)
if (res[i])
printf("%d:%d\n", i, res[i]);
}
return ;
}
POJ 1470 Closest Common Ancestors(LCA&RMQ)的更多相关文章
-
poj 1470 Closest Common Ancestors LCA
题目链接:http://poj.org/problem?id=1470 Write a program that takes as input a rooted tree and a list of ...
-
POJ 1470 Closest Common Ancestors(LCA 最近公共祖先)
其实这是一个裸求LCA的题目,我使用的是离线的Tarjan算法,但是这个题的AC对于我来说却很坎坷……首先是RE,我立马想到数组开小了,然后扩大了数组,MLE了……接着把数组调整适当大小,又交了一发, ...
-
POJ 1470 Closest Common Ancestors LCA题解
本题也是找LCA的题目,只是要求多次查询.一般的暴力查询就必定超时了,故此必须使用更高级的方法,这里使用Tarjan算法. 本题处理Tarjan算法,似乎输入处理也挺麻烦的. 注意: 由于查询的数据会 ...
-
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】
任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000 ...
-
POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13372 Accept ...
-
POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13370 Accept ...
-
POJ 1470 Closest Common Ancestors
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 17306 Ac ...
-
poj——1470 Closest Common Ancestors
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 20804 Accept ...
随机推荐
-
Python Tutorial 学习(三)--An Informal Introduction to Python
3.1. 将Python用作计算器 3.1.1. Numbers 数 作为一个计算器,python支持简单的操作, '+','-','*','/'地球人都知道的加减乘除. ()可以用来改变优先级,同数 ...
-
关于HTML编辑页面(1)
1,<title>...</title> //省略号表示的是网页标题 2,<body>...</body>//省略号表示的是网页正文内容 3,在Drea ...
-
j
在Java程序或JSP程序中,其实有很多的代码段是可以重复使用的,比如对数据库的操作.用户的有效性检查及某些项特定功能的实现等.为了很好的解决这个问题,提高开发效率,Sun公司推出了JavaBean, ...
-
JS获取select选中的值
var oSel=oFl.getElementsByTagName('select')[0]; oSel.onchange=function(){ var indexselect=oSel.selec ...
-
C++例题1:输出可打印字符
#include<iostream>#include<stdlib.h>#include<cctype>int main(){ int i;char a=0; fo ...
-
理解Javascript 的闭包(closure)
要理解闭包的概念先从变量的作用域说去 一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域无非就是两种:全局变量和局部变量. Javascript语言的特殊之 ...
-
修改weblogic jvm启动参数
进入: D:\Oracle\Middleware\user_projects\domains\base_domain\startWebLogic.cmd 在call 上一行增加: set USER_M ...
-
Yaroslav and Sequence
Codeforces Round #182 (Div. 1) A:http://codeforces.com/contest/301/problem/A 题意:给你2*n-1个数,你每次可以选择n个连 ...
-
dwr消息推送和tomcat集群
网友的提问: 项目中用到了dwr消息推送.而服务端是通过一个http请求后 触发dwr中的推送方法.而单个tomcat中.服务器发送的http请求和用户都在一个tomcat服务器中.这样就能精准推送到 ...
-
Unity 动态载入Panel并实现淡入淡出
unity版本:4.5 NGUI版本:3.6.5 参考链接:http://tieba.baidu.com/p/3206366700,作者:百度贴吧 水岸上 动态载入NGUI控件,这里用Panel为例说 ...
题目链接:http://poj.org/problem?id=1470 Write a program that takes as input a rooted tree and a list of ...
其实这是一个裸求LCA的题目,我使用的是离线的Tarjan算法,但是这个题的AC对于我来说却很坎坷……首先是RE,我立马想到数组开小了,然后扩大了数组,MLE了……接着把数组调整适当大小,又交了一发, ...
本题也是找LCA的题目,只是要求多次查询.一般的暴力查询就必定超时了,故此必须使用更高级的方法,这里使用Tarjan算法. 本题处理Tarjan算法,似乎输入处理也挺麻烦的. 注意: 由于查询的数据会 ...
POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...
任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000 ...
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13372 Accept ...
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13370 Accept ...
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 17306 Ac ...
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 20804 Accept ...
-
Python Tutorial 学习(三)--An Informal Introduction to Python
3.1. 将Python用作计算器 3.1.1. Numbers 数 作为一个计算器,python支持简单的操作, '+','-','*','/'地球人都知道的加减乘除. ()可以用来改变优先级,同数 ...
-
关于HTML编辑页面(1)
1,<title>...</title> //省略号表示的是网页标题 2,<body>...</body>//省略号表示的是网页正文内容 3,在Drea ...
-
j
在Java程序或JSP程序中,其实有很多的代码段是可以重复使用的,比如对数据库的操作.用户的有效性检查及某些项特定功能的实现等.为了很好的解决这个问题,提高开发效率,Sun公司推出了JavaBean, ...
-
JS获取select选中的值
var oSel=oFl.getElementsByTagName('select')[0]; oSel.onchange=function(){ var indexselect=oSel.selec ...
-
C++例题1:输出可打印字符
#include<iostream>#include<stdlib.h>#include<cctype>int main(){ int i;char a=0; fo ...
-
理解Javascript 的闭包(closure)
要理解闭包的概念先从变量的作用域说去 一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域无非就是两种:全局变量和局部变量. Javascript语言的特殊之 ...
-
修改weblogic jvm启动参数
进入: D:\Oracle\Middleware\user_projects\domains\base_domain\startWebLogic.cmd 在call 上一行增加: set USER_M ...
-
Yaroslav and Sequence
Codeforces Round #182 (Div. 1) A:http://codeforces.com/contest/301/problem/A 题意:给你2*n-1个数,你每次可以选择n个连 ...
-
dwr消息推送和tomcat集群
网友的提问: 项目中用到了dwr消息推送.而服务端是通过一个http请求后 触发dwr中的推送方法.而单个tomcat中.服务器发送的http请求和用户都在一个tomcat服务器中.这样就能精准推送到 ...
-
Unity 动态载入Panel并实现淡入淡出
unity版本:4.5 NGUI版本:3.6.5 参考链接:http://tieba.baidu.com/p/3206366700,作者:百度贴吧 水岸上 动态载入NGUI控件,这里用Panel为例说 ...