POJ 1330 最近公共祖先LCA_Tarjan 【水】
来源:互联网 发布:单片机嵌入式培训 编辑:程序博客网 时间:2024/06/06 03:07
最近做题做到,顺便复习一下。。。23333
#include <cstdio>#include <iostream>#include <cstring>using namespace std;#define maxn 10010int head[maxn],fa[maxn],vis[maxn],is[maxn];int que[2],ans,top;struct node{ int h,next;}line[maxn];void add(int x,int y){ line[top].h=y;line[top].next=head[x];head[x]=top++;}int fint(int x){ return x==fa[x]?x:fa[x]=fint(fa[x]);}void lca(int x){ fa[x]=x; for(int i=head[x];i!=-1;i=line[i].next) { int v=line[i].h; lca(v); fa[v]=x; } int flag=0; if(x==que[flag]||x==que[flag=!flag]) { if(vis[que[!flag]]) { ans=fint(que[!flag]); } } vis[x]=1;}int main(){ int T; cin>>T; while(T--) { memset(head,-1,sizeof(head)); memset(vis,0,sizeof(vis)); memset(is,0,sizeof(is)); top=0; int n; cin>>n; int x,y; for(int i=0;i<n-1;i++) { cin>>x>>y; add(x,y); is[y]++; } int root=1; for(int i=1;i<=n;i++) if(!is[i]) {root=i;break;} cin>>que[0]>>que[1]; lca(root); cout<<ans<<endl; } return 0;}
0 0
- POJ 1330 最近公共祖先LCA_Tarjan 【水】
- POJ 1330 最近公共祖先LCA_Tarjan 【水】
- poj 1330 最近公共祖先
- poj 1330 最近公共祖先
- poj 1330 最近公共祖先
- poj 1330 LCA最近公共祖先
- poj 1330 LCA 最近公共祖先
- poj 1330 Defragment (最近公共祖先)
- poj 1330(最近公共祖先)
- POJ 1330 LCA+并查集求最近公共祖先
- LCA 算法学习 (最近公共祖先)poj 1330
- [POJ 1330]Nearest Common Ancestors(LCA最近公共祖先)
- POJ 1330 最近公共祖先LCA(Tarjan离线做法)
- POJ 1330 Nearest Common Ancestors(最近公共祖先)
- POJ 1330 Nearest Common Ancestors 【最近公共祖先】
- 【POJ 1986】Distance Queries 最近公共祖先
- poj 3237 Tree(最近公共祖先)
- Closest Common Ancestors+poj+最近公共祖先
- char*,const char*和string 三者转换
- 基于key-value的存储系统Redis
- Effective C++读书笔记版-条款01,02
- Android 裁剪图片 aspectX aspectY outputX outputY的设置规则纠正
- struts2 action动态方法调用
- POJ 1330 最近公共祖先LCA_Tarjan 【水】
- Ubuntu 安装mysql和简单操作
- 原型:char *strrchr(const char *str, char c); http://blog.csdn.net/hgj125073/article/details/8443912
- 数据结构之---C语言实现归并排序
- plsql链接oracle Could not load "……\bin\oci.dll"异常
- Matlab cell函数
- Linux下查看和添加PATH环境变量
- C++中未解之谜
- UML类图几种关系的总结