poj1330
来源:互联网 发布:淘宝访客只有二三十 编辑:程序博客网 时间:2024/06/07 11:31
# include <iostream># include <cstdio># include <cstring># include <string># include <cmath># include <vector># include <map># include <queue># include <cstdlib># define MAXN 500001using namespace std;int x,y,t,n,s,ans;int vis[MAXN],fa[MAXN];vector <int> vec[MAXN];int find(int x){ return (x == fa[x]) ? x : fa[x] = find(fa[x]);}void dfs(int u){ int i, v; for(i = 0; i < vec[u].size(); i++) { v = vec[u][i]; dfs(v); vis[v]=1; fa[v] = u; } if(u==x && vis[y]) ans=find(y); if(u==y && vis[x]) ans=find(x);}int main(){ cin>>t; while(t--) { cin>>n; memset(vis,0,sizeof vis); for(int i = 1; i < n; i++) { cin>>x>>y; vis[y]=1; vec[x].push_back(y); } cin>>x>>y; for(int i=1; i<=n; i++) if(vis[i]==0) { s=i; fa[s]=s; break; } memset(vis,0,sizeof vis); for(int i=1;i<=n;i++) fa[i]=i; dfs(s); cout<<ans<<endl; for(int i=1;i<=n;i++) vec[i].clear(); } return 0;}/*152 33 43 11 53 5*/
阅读全文
0 0
- poj1330
- poj1330
- poj1330
- poj1330
- poj1330
- poj1330
- POJ1330 TarjanLCA
- POJ1330 倍增
- POJ1330 Nearest Common Ancestors
- LCA问题 poj1330 / poj1470
- poj1330 LCA离线算法
- poj1330 解题报告
- POJ1330(Nearest Common Ancestors)
- poj1330 lca转rmq
- POJ1330--Nearest Common Ancestors
- POJ1330(最近公共祖先)
- POJ1330(LCA-离线tarjan)
- poj1330 倍增LCA
- Linux libcurl安装及注意事项
- Java中常用的四种排序算法
- CodeForces
- wait、notify、notifyAll的使用方法
- 队列的链式存储的实现及操作
- poj1330
- [架构师之路] 深入浅出搜索引擎 系列
- IO流
- 利用带花树算法解决一般图的最大匹配
- WebSocket实例教程
- 利用python进行数据分析学习笔记—python基础知识
- 把Hive操作的spark代码丢到yarn上面运行找不到数据库
- KDD1999 数据集
- 蒟蒻的网络流24题解题记