您的位置:首页 > 其它

poj1330 Nearest Common Ancestors(LCA离线)

2016-08-05 11:20 381 查看
Nearest Common Ancestors

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 25419Accepted: 13199
Description
A rooted tree is a well-known data 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 8 is 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 node 16. 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 of itself. Nodes 8, 4, 6, and 7 are the ancestors of node 7. A node x is called a common ancestor of two different nodes y and z if node x is an ancestor of node y and an ancestor of node z. Thus, nodes 8 and 4 are the common ancestors of nodes 16 and 7. A node x is called the nearest common ancestor of nodes y and z if x is a common ancestor of y and z and nearest to y and z among their common ancestors. Hence, the nearest common ancestor of nodes 16 and 7 is node 4. Node 4 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 common ancestor of nodes 4 and 12 is node 4. In the last example, if y is an ancestor of 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 in a tree.

InputThe input consists of T test cases. The number of test cases (T) is given in the first 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 are labeled with integers 1, 2,…, N. Each of the next N -1 lines contains a pair of integers that represent an edge –the first integer is the parent node of the 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 common ancestor is to be computed. OutputPrint exactly one line for each test case. The line should contain the integer that is the nearest common ancestor. Sample Input2

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 Output4

3

tarjan模版

只是题目没有给出根,我们需要根据题目的关系自己求解一下。


/**************************
*Create time: Thu Aug 04 20:23:13 2016

*Author: Mymilkbottles

*File name:
**************************/
#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<time.h>

#define pi(x,y) printf("%d%c",(x),(y));
#define pin(x) printf("%d\n",(x));
#define si(x) scanf("%d",&(x))
#define sii(x,y) scanf("%d%d",&(x),&(y))
#define s3(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define rep(x,y,z) for(int (x)=(y);(x)<(z);++(x))
#define dep(x,y,z) for(int (x)=(y)-1;(x)>=(z);--(x))
#define read int TcaseN;scanf("%d",&TcaseN);for(int Tcase=1;Tcase<=TcaseN;++Tcase)
#define cls(x,y) memset((x),(y),sizeof((x)));
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define max3(value_a,value_b,value_c) max(max(value_a,value_b),value_c)
#define min3(value_a,value_b,value_c) min(min(value_a,value_b),value_c)
#define GT(x) (x)=clock();
#define fin(x) freopen(x,"r",stdin);
#define fout(x) freopen(x,"w",stdout);

///In This You Can Define Long Integer Type
#define LONGTYPE long long
typedef LONGTYPE LL;
typedef unsigned LONGTYPE ULL;
const int maxint=((~((unsigned)(0)))>>1);
const LL maxll=((~((unsigned LONGTYPE)(0)))>>1);

const int inf=0x3f3f3f3f;
const double PI=acos(-1.0);
const int maxn=1e4+5;

int top1,top2;
struct note {
int to,next,lca;
} a[maxn<<1],q[maxn<<1];

int deg[maxn];
int in[maxn];
int head1[maxn];
int head2[maxn];
bool used[maxn];
int fa[maxn];

void ADD(int u,int v) {
a[top1].to=v;
a[top1].next=head1[u];
head1[u]=top1++;
a[top1].to=u;
a[top1].next=head1[v];
head1[v]=top1++;
}

void ADD1(int u,int v) {
q[top2].to=v;
q[top2].next=head2[u];
q[top2].lca=-1;
head2[u]=top2++;
q[top2].to=u;
q[top2].next=head2[v];
q[top2].lca=-1;
head2[v]=top2++;
}

int finds(int x) {
if(x^fa[x]) {
fa[x]=finds(fa[x]);
}
return fa[x];
}

void tarjan(int f) {
used[f]=true;
fa[f]=f;
for(int i=head1[f]; ~i; i=a[i].next) {
int to=a[i].to;
if(!used[to]) {
tarjan(to);
fa[to]=f;
}
}
for(int i=head2[f]; ~i; i=q[i].next) {
int to=q[i].to;
if(used[to]&&q[i].lca==-1) {
q[i].lca=q[i^1].lca=finds(to);
}
}
}

int main() {
#ifdef tangge
clock_t tSTART,tEND,t3;
GT(tSTART);
#endif // tangge

/*Input:*/
int u,v;
read{
int n;
si(n);
cls(head1,-1)
top1=top2=0;
cls(deg,0)
rep(i,1,n) {
sii(u,v);
ADD(u,v);
++deg[v];
}
cls(head2,-1)
sii(u,v);
ADD1(u,v);
cls(used,false)
rep(i,1,n+1){
if(!deg[i]){
tarjan(i);break;
}
}
printf("%d\n",q[0].lca);
}
#ifdef tangge
GT(tEND);
printf("%.8lf\n",(tEND-tSTART)/1000.0);
#endif // tangge
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: