poj 1330lca模板题离线算法
来源:互联网 发布:嵌入式和java哪个难学 编辑:程序博客网 时间:2024/05/16 04:46
#include<iostream>
#include<vector>
using namespace std;
const int MAX=10001;
int pre[MAX],visit[MAX],indegree[MAX];
vector<int>qury[MAX],tree[MAX];
int ancestor[MAX];
void init(int n) {//初始化
int i;
for(i=1;i<=n;i++) {
visit[i]=0;
pre[i]=i;
ancestor[i]=i;
indegree[i]=0;
qury[i].clear();
tree[i].clear();
}
}
int find(int x) {
if(x!=pre[x])
pre[x]=find(pre[x]);//路径压缩
return pre[x];
}
void infind(int a,int b) {//合并
int f1=find(a);
int f2=find(b);
if(f1==f2)
return ;
pre[f1]=f2;
return ;
}
void lca(int u) {
int i,len;
len=tree[u].size();
for(i=0;i<len;i++) {
lca(tree[u][i]);
infind(u,tree[u][i]);//将u和其子节点联系起来
ancestor[find(u)]=u;//将所有u的子节点的点都指向u,即u为根
}
visit[u]=1;
len=qury[u].size();
for(i=0;i<len;i++)//
if(visit[qury[u][i]]==1) {//如果找到直接输出
printf("%d\n",ancestor[find(qury[u][i])]);
return ;
}
}
int main() {
int n,t,a,b,i;
scanf("%d",&t);
while(t--) {
scanf("%d",&n);
init(n);
for(i=1;i<n;i++) {
scanf("%d%d",&a,&b);
tree[a].push_back(b);
indegree[b]++;
}
scanf("%d%d",&a,&b);
qury[a].push_back(b);
qury[b].push_back(a);
for(i=1;i<=n;i++)
if(indegree[i]==0) {
lca(i);
break;
}
}
return 0;
}
#include<vector>
using namespace std;
const int MAX=10001;
int pre[MAX],visit[MAX],indegree[MAX];
vector<int>qury[MAX],tree[MAX];
int ancestor[MAX];
void init(int n) {//初始化
int i;
for(i=1;i<=n;i++) {
visit[i]=0;
pre[i]=i;
ancestor[i]=i;
indegree[i]=0;
qury[i].clear();
tree[i].clear();
}
}
int find(int x) {
if(x!=pre[x])
pre[x]=find(pre[x]);//路径压缩
return pre[x];
}
void infind(int a,int b) {//合并
int f1=find(a);
int f2=find(b);
if(f1==f2)
return ;
pre[f1]=f2;
return ;
}
void lca(int u) {
int i,len;
len=tree[u].size();
for(i=0;i<len;i++) {
lca(tree[u][i]);
infind(u,tree[u][i]);//将u和其子节点联系起来
ancestor[find(u)]=u;//将所有u的子节点的点都指向u,即u为根
}
visit[u]=1;
len=qury[u].size();
for(i=0;i<len;i++)//
if(visit[qury[u][i]]==1) {//如果找到直接输出
printf("%d\n",ancestor[find(qury[u][i])]);
return ;
}
}
int main() {
int n,t,a,b,i;
scanf("%d",&t);
while(t--) {
scanf("%d",&n);
init(n);
for(i=1;i<n;i++) {
scanf("%d%d",&a,&b);
tree[a].push_back(b);
indegree[b]++;
}
scanf("%d%d",&a,&b);
qury[a].push_back(b);
qury[b].push_back(a);
for(i=1;i<=n;i++)
if(indegree[i]==0) {
lca(i);
break;
}
}
return 0;
}
- poj 1330lca模板题离线算法
- poj 1330 lca-targan离线算法
- LCA离线tarjan算法模板
- LCA离线tarjan算法模板
- poj 1470 LCA 离线算法
- poj 1330 (LCA模板题)
- 【LCA模板题】POJ 1330
- POJ 1330 Nearest Common Ancestors (模板水题离线LCA,3级)
- LCA 离线算法 tarjan 总结 与模板题
- poj 1330 Nearest Common Ancestors (LCA) tarjan离线算法
- POJ 1330 Nearest Common ancesters(LCA,Tarjan离线算法)
- POJ 1330 Nearest Common Ancestors 【LCA·Tarjan离线算法】
- POJ 1330 Nearest Common Ancestors(LCA 离线算法)
- lca离线算法模板(poj1330验证)
- POJ 1470 LCA tarjan 离线算法
- poj 1470 lca-targan离线算法
- 【离线Tarjan 模板】LCA tarjan 算法 练习: hdu 2586 + poj 1986
- POJ 1330 LCA转RMQ模板题
- 反转句子中的单词顺序
- Android 个人学习笔记 ①
- 程序员网址大全
- 1069. The Black Hole of Numbers (20)
- 图像处理算法1——区域生长法
- poj 1330lca模板题离线算法
- memset()的效率以及源码分析
- 如何终止impdp/expdp
- Struts2基础知识
- hw 9.15
- 【索引】 Volume 7. Graph Algorithms and Implementation Techniques
- ORA-00119: invalid specification for system parameter LOCAL_LISTENER
- JavaScript中125个经典技巧
- 从零开始Windows Phone开发(一):综述及第一个练习