POJ 1470 Closest Common Ancestors (最近公共祖先LCA 的离线算法Tarjan)
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)的更多相关文章
- POJ1470Closest Common Ancestors 最近公共祖先LCA 的 离线算法 Tarjan
该算法的详细解释请戳: http://www.cnblogs.com/Findxiaoxun/p/3428516.html #include<cstdio> #include<alg ...
- POJ 1330 Nearest Common Ancestors (最近公共祖先LCA + 详解博客)
LCA问题的tarjan解法模板 LCA问题 详细 1.二叉搜索树上找两个节点LCA public int query(Node t, Node u, Node v) { int left = u.v ...
- 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
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 17306 Ac ...
- 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: 20804 Accept ...
- POJ1330Nearest Common Ancestors最近公共祖先LCA问题
用的离线算法Tarjan 该算法的详细解释请戳 http://www.cnblogs.com/Findxiaoxun/p/3428516.html 做这个题的时候,直接把1470的代码copy过来,改 ...
随机推荐
- Sql Server 语句
##目录 #####清除缓存 DBCC FREEPROCCACHE; DBCC DROPCLEANBUFFERS; SELECT stock.IdStock, stock.Descr FROM [In ...
- Android--用DownLoadManager下载完成后启动安装
当我们用系统的服务DownLoadManager下载完成后,系统会发送一个广播,我们只需要注册一个广播,然后在广播里面写如一些相应的操作. 1.注册广播 completeReceiver = new ...
- JavaScript高级程序设计之location对象
location对象用来处理URL的相关信息 1.获取查询字符串 // 获取查询字符串对象 var getQueryStringArgs = function () { ? location.sear ...
- 取精华、去糟粕!适合iOS开发者的15大网站推荐
iOS开发者若想使技艺达到炉火纯青的地步,就要不断借鉴他人的有益经验,紧跟新兴科技和工具的步伐.除了Apple的开发者中心,其他网站上的文章和资源也具备参考价值,若能学得一二,必能锦上添花.不过,时间 ...
- wordpress nginx 开启链接为静态
使用固定连接里的自定义 /%postname%/ 日志标题的缩略版本(日志/页面编辑界面上的日志别名).因此“This Is A Great Post!”在URI中会变成this-is-a-great ...
- 慎把“DataContext”静态化 或则单例
之前在项目里由于把DataContext静态化,最后在测试阶段发现了很多奇怪的问题,后来经过同事的指点 然后上网搜了一翻终于发现 MSDN上说: "请不要试图重用 DataContext ...
- text-overflow 与 word-wrap:设置使用一个省略标记...标示对象内文本的溢出。
text-overflow 与 word-wrap text-overflow用来设置是否使用一个省略标记(...)标示对象内文本的溢出. 语法: 但是text-overflow只是用来说明文字溢出时 ...
- PHP错误The server encountered an internal error or misconfiguration and was unable to complete your re
我的笔记本电脑上的环境安装了很多次,但是运行项目时总是会报The server encountered an internal error or misconfiguration and was un ...
- svn 检出 Check out 请求的名称有效,但是找不到请求的类型的数据。
根据问题不同有不同的解决方案,可按照以下方法进行解决1.取消TortoiseSVN-网络-代理2.确认SVN目录地址是否正确,可在浏览器中直接打开测试.如地址是由计算机名组成请改成Ip地址进行测试
- Jquery方法的应用
<body> <div id="one"><span>one</span></div><div class=&qu ...