poj1330Nearest Common Ancestors 1470 Closest Common Ancestors(LCA算法)
LCA思想:http://www.cnblogs.com/hujunzheng/p/3945885.html
在求解最近公共祖先为问题上,用到的是Tarjan的思想,从根结点开始形成一棵深搜树,非常好的处理技巧就是在回溯到结点u的时候,u的子树已经遍历,这时候才把u结点放入合并集合中,
这样u结点和所有u的子树中的结点的最近公共祖先就是u了,u和还未遍历的所有u的兄弟结点及子树中的最近公共祖先就是u的父亲结点。以此类推。。这样我们在对树深度遍历的时候就很自然的将树中的结点分成若干的集合,两个集合中的所属不同集合的任意一对顶点的公共祖先都是相同的,也就是说这两个集合的最近公共最先只有一个。对于每个集合而言可以用并查集来优化,时间复杂度就大大降低了,为O(n + q),n为总结点数,q为询问结点对数。
/*
题意很明显,就是求LCA!
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define N 10005
using namespace std; int n;
int x, y;
vector<int>g[N];
int f[N];
int vis[N], cnt[N];
int ret; int getFather(int x){
return x==f[x] ? x : f[x]=getFather(f[x]);
} bool LCA(int u){
vis[u]=;
f[u]=u;
int len=g[u].size(); if(u==x && vis[y]){
ret=getFather(y);
return true;
} else if(u==y && vis[x]){
ret=getFather(x);
return true;
} for(int i=; i<len; ++i){
int v=g[u][i];
if(!vis[v]){
if(LCA(v)) return true;
f[v]=u;
}
}
return false;
} int main(){
int t;
scanf("%d", &t);
while(t--){
scanf("%d", &n);
memset(cnt, , sizeof(cnt));
for(int i=; i<n; ++i){
int u, v;
scanf("%d%d", &u, &v);
g[u].push_back(v);
++cnt[v];
}
memset(vis, , sizeof(vis));
scanf("%d%d", &x, &y);
for(int i=; i<=n; ++i)
if(cnt[i]==){
LCA(i);
break;
}
printf("%d\n", ret);
for(int i=; i<=n; ++i)
g[i].clear();
}
return ;
}
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#define N 905
#define M 25000
using namespace std; vector<int>g[N];
vector<int>p[M];
int cnt[N];
int f[N];
int vis[N];
int ans[N]; int n, m; int getFather(int x){
return x==f[x] ? x : f[x]=getFather(f[x]);
} void LCA(int u){
f[u]=u;
vis[u]=;
int len; len=p[u].size();
for(int i=; i<len; ++i){
int v=p[u][i];
if(vis[v])
++ans[getFather(v)];
} len=g[u].size();
for(int i=; i<len; ++i){
int v=g[u][i];
if(!vis[v]){
LCA(v);
f[v]=u;
}
}
} int main(){
while(scanf("%d", &n)!=EOF){
memset(cnt, , sizeof(cnt));
for(int i=; i<=n; ++i){
vis[i]=;
ans[i]=;
int a, d, b;
char ch;
scanf("%d", &a);
while(scanf("%c", &ch) && ch!='(');
scanf("%d", &d);
while(scanf("%c", &ch) && ch!=')');
while(d--){
scanf("%d", &b);
++cnt[b];
g[a].push_back(b);
}
}
scanf("%d", &m);
while(m--){
char ch;
while(scanf("%c", &ch) && ch!='(');
int u, v;
scanf("%d%d", &u, &v);
p[u].push_back(v);
p[v].push_back(u);
while(scanf("%c", &ch) && ch!=')');
} for(int i=; i<=n; ++i)
if(cnt[i]==){
LCA(i);
break;
}
for(int i=; i<=n; ++i)
if(ans[i]!=)
printf("%d:%d\n", i, ans[i]); for(int i=; i<=n; ++i)
g[i].clear(), p[i].clear();
}
return ;
}
poj1330Nearest Common Ancestors 1470 Closest Common Ancestors(LCA算法)的更多相关文章
- 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
题目链接: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,离线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——T 1470 Closest Common Ancestors
http://poj.org/problem?id=1470 Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 20830 ...
- 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 ...
- POJ 1470 Closest Common Ancestors【近期公共祖先LCA】
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013912596/article/details/35311489 题目链接:http://poj ...
随机推荐
- Android服务开机自启动
新任务需要Android程序开机跑一个服务,查找资料得出如下方法: 用广播的方法监听系统启动事件:android.intent.action.BOOT_COMPLETED 并在AndroidManif ...
- 【整理】--linux指令
1.压缩 解压 .tar 解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)———————————————.g ...
- IOS 通过苹果开放API检测更新
#define APPID @"1067207206" -(void)onCheckVersion { NSDictionary *infoDic = [[NSBundle mai ...
- dlmalloc(Android bionic C库的malloc实现)简介
欢迎转载opendevkit文章, 文章原始地址: http://www.opendevkit.com/?e=56 Dlmalloc是目前一个十分流行的内存分配器,其由Doug Lea从1987年开始 ...
- step by step设置postgresql用户密码并配置远程连接
设置pgsql默认用户posgres的密码 1.确认pgsql是开启的 /etc/init.d/postgresql status 2.使用默认用户登录 sudo su postgres 3.进入ps ...
- SSH 动态端口forwarding是如何工作的
好久没有来了,实在是太懒. 经常用SSH的动态端口forwarding 来FQ,使用像这样的命令: ssh -D 9999 -f -C -q -N sshHost.somewhere.com 这个命令 ...
- SQL入门经典(四)之创建和修改数据表
本章主要讲如何使用脚本创建数据库:如何使用脚本创建表:如何删除对象和修改对象. CREATE 语句:CREATE <object type> <onject name> 创建数 ...
- Wireshark插件编写
Wireshark插件编写 在抓包的过程中学习了使用wireshark,同时发现wireshark可以进行加载插件,便在网上学习了一下相应的插件开发技术. 需求编写一个私有协议名为SYC,使用UDP端 ...
- Linux修改环境变量的方法
在Linux操作系统中,有时候跟着教程安装了一些软件,安装成功后,很高兴的准备运行该软件相应命令,但是偶尔会遇到”Command not found…“的提示.原因是因为你安装的软件需要设置环境变量才 ...
- Nim编码风格
介绍 Nim语言不限制开发人员使用哪种具体的编码风格, 但为了社区的发展,在编写一些标准库的时候还是应该遵从统一的编码风格 这篇文章会列出一系列的编码风格准则,供大家参考. 但值得注意的是,有很多例外 ...