POJ 1330 Nearest Common Ancestors LCA
来源:互联网 发布:化妆品真伪查询软件 编辑:程序博客网 时间:2024/05/16 05:22
给一棵树,查询两个点的最近公共祖先。还是模板题,不过数据给的时候是按顺序给的,所以要直接存成从根节点到叶子节点的有向图,同时存一下每个点的入度用来找根节点,然后从根节点开始做一遍tarjan就行..
#include <iostream>#include <cstdio>#include <algorithm>#include <memory.h>#include <cmath>using namespace std;typedef long long ll;const int maxn=40000+40;struct node{ int w,v; int next;}edge[maxn<<1],query[500];bool vis[maxn];int g[maxn],gg[maxn];int res[maxn][3];int m,n,k,p,q;int en,eq;int test;int fa[maxn];int dis[maxn];int find(int x){ if (x!=fa[x]) return fa[x]=find(fa[x]); return x;}void tarjan(int u){ vis[u]=true; fa[u]=u; for (int j=gg[u]; j!=-1; j=query[j].next) { if (vis[query[j].v]) { res[query[j].w][2]=find(query[j].v); } } for (int j=g[u]; j!=-1; j=edge[j].next) { if (!vis[edge[j].v]) { dis[edge[j].v]=dis[u]+edge[j].w; tarjan(edge[j].v); fa[edge[j].v]=u; } }}int ideg[maxn];int main(){// freopen("in.txt","r",stdin); scanf("%d",&test); while (test--) { memset(vis,false,sizeof vis); memset(dis,0,sizeof dis); memset(ideg,0,sizeof ideg); scanf("%d",&n);// scanf("%d",&m); m=1; memset(g,-1,sizeof g); memset(gg,-1,sizeof gg); en=eq=0; int x,y,z; int root=-1; for (int i=1; i<n; i++) { scanf("%d%d",&x,&y); ideg[y]++; if (root==-1) root=x; z=1; edge[en].w=z; edge[en].v=y; edge[en].next=g[x]; g[x]=en; en++; } for (int i=1; i<=m; i++) { scanf("%d%d",&x,&y); query[eq].v=y; query[eq].w=i; query[eq].next=gg[x]; gg[x]=eq; eq++; query[eq].v=x; query[eq].w=i; query[eq].next=gg[y]; gg[y]=eq; eq++; res[i][0]=x; res[i][1]=y; } for (int i=1; i<=n; i++) if (ideg[i]==0) { root=i; break; } dis[root]=0; tarjan(root); cout<<res[1][2]<<endl; } return 0;}
0 0
- POJ-1330 Nearest Common Ancestors【LCA】
- POJ 1330 Nearest Common Ancestors [LCA+RMQ]
- POJ 1330Nearest Common Ancestors LCA模板
- poj 1330 Nearest Common Ancestors(LCA)
- POJ 1330 Nearest Common Ancestors (LCA)
- poj 1330 Nearest Common Ancestors (LCA)
- poj 1330 Nearest Common Ancestors(LCA模板)
- POJ 1330 Nearest Common Ancestors LCA
- POJ 1330 Nearest Common Ancestors LCA
- poj 1330 Nearest Common Ancestors LCA targin
- POJ 1330 Nearest Common Ancestors LCA题解
- poj 1330 Nearest Common Ancestors (LCA)
- Nearest Common Ancestors - POJ 1330 LCA
- 【LCA】 POJ 1330 Nearest Common Ancestors
- POJ 1330 Nearest Common Ancestors LCA
- POJ 1330 Nearest Common Ancestors (LCA)
- POJ 1330 Nearest Common Ancestors (LCA)
- Poj-1330-Nearest Common Ancestors-倍增LCA
- ural - 1470 - UFOs(树状数组)
- ucos创建任务
- windows核心编程笔记(一)
- 253 - Cube painting UVA
- MySql 查询一周内最近7天记录
- POJ 1330 Nearest Common Ancestors LCA
- Python日志输出——logging模块
- mysql获取按日期排序获取最新的记录
- hdu 3336 Count the string(KMP)
- Learn Python The Hard Way-Ecercise26~30
- 没有RunInstallerAttribute.Yes的公共安装程序
- ubuntu(linux)系统根目录文件分类代表的含义
- javaBean的4种scope属性总结
- select . checkbox 选中,取消 js操作大全