Poj 1330 nearest common ancestors (simplest LCA)

Source: Internet
Author: User

Question:

Returns the structure of a tree.

Given two dots X and Y, find the two of them.

 

Ideas:

We only need two point-of-concept (LCA). Two methods are used: offline Tarjan and direct.

Check the code.

 

Code:

Method 1: do it directly.

int const maxn = 10005;int T,n,a,b;int fa[maxn];int X,Y;int main(){    cin>>T;    while(T--){        scanf("%d",&n);        mem(fa,-1);        rep(i,1,n-1){            scanf("%d%d",&a,&b);            fa[b]=a;        }        scanf("%d%d",&X,&Y);        map<int,char> mp;        int t1=X;        while(t1!=-1){            mp[t1]=1;            t1=fa[t1];        }        t1=Y;        while(t1!=-1){            if(mp[t1]==1){                printf("%d\n",t1);                break;            }            t1=fa[t1];        }    }}


 

Method 2: offline Tarjan.

Int const maxn = 10005; struct node {int to, W, next, LCA ;}; node edge [maxn * 2]; bool vis [maxn]; int cnt1, cnt2; int head [maxn], Fa [maxn]; bool flag; int X, Y, N; inline void addedge (int u, int V, int W) {edge [++ cnt1]. W = W; edge [cnt1]. to = V; edge [cnt1]. next = head [u]; head [u] = cnt1;} void Init () {MEM (Head,-1); MEM (VIS, false); cnt1 = 0; flag = false; rep (I, 1, n) Fa [I] = I;} int findfa (INT X) {If (Fa [x] = X) return Fa [X]; return Fa [X] = findfa (Fa [x]);} void tarjan_lca (int u) {// offline LCA algorithm if (FLAG) return; Fa [u] = u; vis [u] = true; For (INT I = head [u]; I! =-1; I = edge [I]. Next) {If (! Vis [edge [I]. to]) {tarjan_lca (edge [I]. to); Fa [edge [I]. to] = u ;}} if (U == Y &&! Flag) {If (vis [x]) {printf ("% d \ n", findfa (x); flag = true; return ;}} if (u = x &&! Flag) {If (vis [y]) {printf ("% d \ n", findfa (y); flag = true; return ;}} int T,, b; int main () {CIN> T; while (t --) {scanf ("% d", & N); Init (); bool T1 [maxn] = {0}; rep (I, 1, n-1) {scanf ("% d", & A, & B); addedge (, b, 1); T1 [B] = true;} scanf ("% d", & X, & Y); rep (I, 1, n) if (! T1 [I]) {tarjan_lca (I); // I is the root node break of the tree ;}}}

 

Poj 1330 nearest common ancestors (simplest LCA)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.