POJ 1330 Nearest Common Ancestors 【Tarjan+倍增+RMQ】
Nearest Common Ancestors
Time Limit: 1000MS |
Memory Limit: 10000K |
Total Submissions: 28591 |
Accepted: 14626 |
Description
A rooted tree is a well-knowndata structure in computer science and engineering. An example is shown below:
In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8is the root of the tree. Node x is an ancestor of node y if node x is in the path between the root and node y. For example, node 4 is an ancestor of node16. Node 10 is also an ancestor
of node 16. As a matter of fact, nodes 8, 4,10, and 16 are the ancestors of node 16. Remember that a node is an ancestor ofitself. Nodes 8, 4, 6, and 7 are the ancestors of node 7. A node x is called acommon ancestor of two different nodes y and z if node
x is an ancestor of nodey and an ancestor of node z. Thus, nodes 8 and 4 are the common ancestors ofnodes 16 and 7. A node x is called the nearest common ancestor of nodes y and zif x is a common ancestor of y and z and nearest to y and z among their commonancestors.
Hence, the nearest common ancestor of nodes 16 and 7 is node 4. Node4 is nearer to nodes 16 and 7 than node 8 is.
For other examples, the nearest common ancestor of nodes 2 and 3 is node 10,the nearest common ancestor of nodes 6 and 13 is node 8, and the nearest commonancestor of nodes 4 and 12 is node 4. In the last example, if y is an ancestorof z, then the nearest common
ancestor of y and z is y.
Write a program that finds the nearest common ancestor of two distinct nodes ina tree.
Input
The input consists of T test cases. The number of test cases (T) is given in thefirst line of the input file. Each test case starts with a line containing an integer N , the number of nodes in a tree, 2<=N<=10,000. The nodes arelabeled with integers 1, 2,..., N. Each of the next N -1 lines contains a pairof integers that represent an edge --the first integer is the parent node ofthe second integer. Note that a tree with N nodes has exactly N - 1 edges. The last line of each test case contains two distinct integers whose nearest commonancestor is to be computed.
Output
Print exactly one line for each test case. The line should contain the integer thatis the nearest common ancestor.
Sample Input
2
16
1 14
8 5
10 16
5 9
4 6
8 4
4 10
1 13
6 15
10 11
6 7
10 2
16 3
8 1
16 12
16 7
5
2 3
3 4
3 1
1 5
3 5
Sample Output
4
3
LCA演算法簡介:傳送門
Tarjan:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define f(i,a,b) for(int i=(a);i<(b);++i)
typedef long long ll;
const int maxn= 10005;
const int mod = 475;
const ll INF = 0x3f3f3f3f;
const double eps = 1e-6;
#define rush() int T;scanf("%d",&T);while(T--)
int n;
int u,v;
int flag;
int in[maxn];
int vis[maxn];
int pre[maxn];
vector<int>arr[maxn];
void init()
{
mst(in,0);
mst(vis,0);
for(int i=1;i<=n;i++)
{
pre[i]=i;
arr[i].clear();
}
flag=0;
}
int find(int x)
{
int t,r=x;
while(x!=pre[x])
{
x=pre[x];
}
while(r!=x)
{
t=pre[r];
pre[r]=x;
r=t;
}
return x;
}
void join(int a,int b)
{
int A,B;
A=find(a);
B=find(b);
if(A!=B)
pre[B]=A;
}
void Tarjan(int now)
{
if(flag) return;
vis[now]=1;
for(unsigned int i=0;i<arr[now].size();i++)
{
int t=arr[now][i];
//if(vis[t]) continue; //對於一棵樹來說這條語句沒必要
Tarjan(t);
join(now,t);
}
if(flag) return;
if(now==u&&vis[v]==1)
{
flag=1;
printf("%d\n",find(v));
return;
}
if(now==v&&vis[u]==1)
{
flag=1;
printf("%d\n",find(u));
return;
}
}
int main()
{
int x,y;
rush()
{
scanf("%d",&n);
init();
for(int i=1;i<n;i++)
{
scanf("%d%d",&x,&y);
in[y]++;
arr[x].push_back(y);
}
scanf("%d%d",&u,&v);
for(int i=1;i<=n;i++)
{
if(in[i]==0)
{
Tarjan(i);
break;
}
}
}
return 0;
}
倍增演算法:
#include <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)
typedef long long ll;
const int maxn = 10005;
const ll mod = 1e9+7;
const int INF = 0x3f3f3f;
const double eps = 1e-9;
vector<int>vec[maxn];
int n,root;
int fa[maxn][20];
int depth[maxn];
int degree[maxn];
void dfs(int u,int pre,int d)
{
fa[u][0]=pre;
depth[u]=d;
for(int i=0;i<vec[u].size();i++)
{
int v=vec[u][i];
if(v!=pre)
{
dfs(v,u,d+1);
}
}
}
void init()
{
dfs(root,-1,0);
for(int j=0;(1<<(j+1))<n;j++)
for(int i=1;i<=n;i++)
{
if(fa[i][j]<0) fa[i][j+1]=-1;
else fa[i][j+1]=fa[fa[i][j]][j];
}
}
int LCA(int u,int v)
{
if(depth[u]>depth[v]) swap(u,v);
int temp=depth[v]-depth[u];
for(int i=0;(1<<i)<=temp;i++)
{
if((1<<i)&temp)
v=fa[v][i];
}
if(v==u) return u;
for(int i=(int)(log(n*1.0)/log(2.0));i>=0;i--)
{
if(fa[u][i]!=fa[v][i])
{
u=fa[u][i];
v=fa[v][i];
}
}
return fa[u][0];
}
int main()
{
int u,v;
rush()
{
mst(fa,0);
mst(degree,0);
scanf("%d",&n);
for(int i=0;i<=n;i++)
{
vec[i].clear();
}
for(int i=0;i<n-1;i++)
{
scanf("%d%d",&u,&v);
vec[u].push_back(v);
vec[v].push_back(u);
degree[v]++;
}
for(int i=1;i<=n;i++)
{
if(degree[i]==0)
{
root=i;
break;
}
}
init();
scanf("%d%d",&u,&v);
int ans=LCA(u,v);
printf("%d\n",ans);
}
return 0;
}
RMQ演算法:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)
typedef long long ll;
const int maxn = 10005;
const ll mod = 1e9+7;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
struct node
{
int u,v,next;
}e[maxn*2];
int n,root;
int tot,cnt;
int id[2*maxn];
int depth[2*maxn];
int head[2*maxn];
int ss[maxn];
int dp[2*maxn][30];
int degree[maxn];
void add(int u,int v)
{
e[cnt].u=u;
e[cnt].v=v;
e[cnt].next=head[u];
head[u]=cnt++;
}
void dfs(int u,int pre,int dep)
{
id[++tot]=u;
ss[u]=tot;
depth[tot]=dep;
for(int i=head[u];~i;i=e[i].next)
{
int v=e[i].v;
if(v==pre) continue;
dfs(v,u,dep+1);
id[++tot]=u;
depth[tot]=dep;
}
}
void ST(int n)
{
int k=(int)(log2(1.0*n));
for(int i=1;i<=n;i++) dp[i][0]=i;
for(int j=1;j<=k;j++)
for(int i=1;i+(1<<j)-1<=n;i++)
{
int a=dp[i][j-1];
int b=dp[i+(1<<(j-1))][j-1];
if(depth[a]<depth[b]) dp[i][j]=a;
else dp[i][j]=b;
}
}
int RMQ(int l,int r)
{
int k=(int)(log2(1.0*r-l+1));
int a=dp[l][k];
int b=dp[r-(1<<k)+1][k];
if(depth[a]<depth[b]) return a;
else return b;
}
int LCA(int x,int y)
{
int l=ss[x];
int r=ss[y];
if(l>r) swap(l,r);
return id[RMQ(l,r)];
}
int main()
{
int u,v;
rush()
{
mst(degree,0);
mst(head,-1);
cnt=tot=0;
scanf("%d",&n);
for(int i=0;i<n-1;i++)
{
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
degree[v]++;
}
for(int i=1;i<=n;i++)
{
if(degree[i]==0)
{
root=i;
break;
}
}
dfs(root,-1,1);
ST(2*n-1);
scanf("%d%d",&u,&v);
int ans=LCA(u,v);
printf("%d\n",ans);
}
return 0;
}