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算法)的更多相关文章

  1. POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)

    POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...

  2. POJ 1470 Closest Common Ancestors 【LCA】

    任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000 ...

  3. 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 ...

  4. POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13372   Accept ...

  5. POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13370   Accept ...

  6. POJ——T 1470 Closest Common Ancestors

    http://poj.org/problem?id=1470 Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 20830   ...

  7. POJ 1470 Closest Common Ancestors

    传送门 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 17306   Ac ...

  8. poj——1470 Closest Common Ancestors

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 20804   Accept ...

  9. POJ 1470 Closest Common Ancestors【近期公共祖先LCA】

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013912596/article/details/35311489 题目链接:http://poj ...

随机推荐

  1. 更改Xampp-sql的默认密码-配置appche运行环境

    用php编写的web应用程序,需运行在php的web容器中,其中apache server是一个针对php web容器,它是apache下的开源项目.通常要运行一个web程序,我们还需要安装数据库软件 ...

  2. 关于 某编译错误: This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.

    每次当八月在VS2013里使用strcat的时候,基本上都会出现这个问题╮(╯▽╰)╭ 原因貌似是因为安全问题(⊙o⊙) 于是,解决方法如下: ①更改预处理定义: (这也是八月最常用的方法了,虽然貌似 ...

  3. loadRunner 负载机连接错误分析

    错误信息: Error: Process "lr_bridge.exe" was not created on remote host "192.168.86.17&qu ...

  4. c#后台进行窗体切换的方法

    Response.Redirect("http://localhost:60896/WebForm2.aspx");

  5. winform 子报表

    public void BindReport(string _invno,string _type)         {             if (!Is_Has_Express_No(_inv ...

  6. [leetcode 24] Swap Nodes in k-Group

    1 题目: 目前被墙,看不了. 2 思路: 比较简单,注意处理边界点就好 3 代码: public ListNode swapPairs(ListNode head) { int temp; if(h ...

  7. Swift语言的基础详解

    1.1.关于swift · Swift 是一种适用于 iOS 和 OS X 应用的全新编程语言,它建立在最好的 C 和 Objective-C 语言之上,并且没有 C 语言的兼容性限制.Swift 采 ...

  8. 分享我开发的网络电话Android手机APP正式版,图文详解及下载

    分享我开发的网络电话Android手机APP正式版,图文详解及下载 分享我开发的网络电话Android手机APP正式版 实时语音通讯,可广域网实时通讯,音质清晰流畅! 安装之后的运行效果: 第一次安装 ...

  9. TCP字节流和UDP数据报区别

    两者的区别在于TCP接收的是一堆数据,而每次取多少由主机决定;而UDP发的是数据报,客户发送多少就接收多少. 拥有这些区别的原因是由于TCP和UDP的特性不同而决定的.TCP是面向连接的,也就是说,在 ...

  10. Win10 UWP开发中的重复性静态UI绘制小技巧 2

    小技巧1 地址:http://www.cnblogs.com/ms-uap/p/4641419.html 介绍 我们在上一篇博文中展示了通过Shape.Stroke族属性实现静态重复性UI绘制,使得U ...