---
title: "POJ 1470 zz"
author: "glintstar"
date: "2009-05-11T23:45:42Z"
tags: []
slug: "poj-1470-zz"
draft: false
lastmod: "2009-05-11T23:49:46Z"
id: 383937
url: "https://glintstar.iteye.com/blog/383937"
---

http://blog.csdn.net/apachecq/archive/2009/01/13/3769689.aspx

比较简单的LCA。我先用的是邻接表来存放树结构和查询，结果要超时。但程序应该可以在ZOJ上AC。我自己判断超时的原因可能是对于本题的多组数据输入格式，每做完一组数据后，需要对各个数据结构清0，对于动态申请的邻接表结点要逐一释放空间。这些操作对于一组输入数据可能花不了多长时间，但对于多组数据，就会花很长时间。

然后我计算了一下空间限制，对于N=900的上限，开两个二维数组来存放树和查询，采用“假指针”方式存放，空间大约占用6M左右，完全过得了空间限制。于是就改了。再次提交，AC。空间：6816K， 时间：672 MS

 

版本1：邻接表方式。

#include <iostream>
using namespace std;
const int MAXN = 910;
int fa, rank, ancestor, ans;
bool visit, rt;
struct node {
    int v;
    node *next;
};

node *tree, *query;
void Make_Set(int x)
{
    fa = x;
    rank = 0;
}
int Find_Set(int x)
{
   if (fa!=x)
     fa = Find_Set(fa);
   return fa;
}
void Union_Set(int x, int y)
{
    int xRoot, yRoot;
    xRoot = Find_Set(x);
    yRoot = Find_Set(y);
    if (rank > rank)
      fa = xRoot;
    else if (rank > rank)
      fa = yRoot;
    else if (xRoot != yRoot)
    {
        fa = xRoot;
        rank++;
    }
}
void Tarjan(int u)
{
    node *p;

    Make_Set(u);
    ancestor<u> = u;

    for (p = tree<u>; p ; p=p->next)
    {
        Tarjan(p->v);
        Union_Set(u, p->v);
        ancestor = u;
    }
    visit<u> = true;
    for (p = query<u>; p ; p=p->next)
      if (visit)
        ans]++;

}

int main()
{
    int n, n1, m,a, b, root, i, j;
    char c1, c2, c3;
    node *p, *q;
    while(scanf("%d", &n)!=EOF)
    {
        memset(rt, true, sizeof(rt));
        memset(visit, false, sizeof(visit));
        memset(ans, 0, sizeof(visit));
        memset(tree, 0, sizeof(tree));
        memset(query, 0, sizeof(query));
        for (i=1; i<=n; i++)
        {
            scanf("%d%c%c%d%c", &m, &c1, &c2, &n1, &c3);
            if (n1 > 0)
                for (j=1; j<=n1; j++)
                {
                    scanf("%d", &a);
                    rt = false;
                    p = new node;
                    p->v = a;
                    p->next = tree;
                    tree = p;
                }
        }
        for (i=1; i<=n; i++)
            if (rt*)
            {
                root = i;
                break;
            }
        scanf("%d\n", &n1);
        for (i=1; i<=n1;i++)
        {
            while (getchar()!='(') ;

            scanf("%d %d)%c", &a, &b, &c3);
            p = new node;
            p->v = b;
            p->next= query;
            query = p;

            p = new node;
            p->v = a;
            p->next = query**;
            query** = p;
        }
        Tarjan(root);
        for (i=1; i<=n; i++)
          if (ans* > 0)
            printf("%d:%d\n", i, ans*);
        for (i=1; i<=n; i++)
        {
            p=tree*;
            while (p!=NULL)
            {
                q = p;
                p = p->next;
                delete q;
            }
            p=query*;
            while (p!=NULL)
            {
                q = p;
                p = p->next;
                delete q;
            }
        }


    }

    return 0;
}

版本2：二维数组

#include <iostream>
using namespace std;
const int MAXN = 910;
int fa, rank, ancestor, ans;
bool visit, rt;
int tree, query;
void Make_Set(int x)
{
    fa = x;
    rank = 0;
}
int Find_Set(int x)
{
   if (fa!=x)
     fa = Find_Set(fa);
   return fa;
}
void Union_Set(int x, int y)
{
    int xRoot, yRoot;
    xRoot = Find_Set(x);
    yRoot = Find_Set(y);
    if (rank > rank)
      fa = xRoot;
    else if (rank > rank)
      fa = yRoot;
    else if (xRoot != yRoot)
    {
        fa = xRoot;
        rank++;
    }
}
void Tarjan(int u)
{
    int i;

    Make_Set(u);
    ancestor<u> = u;

    for (i = 1; i<=tree<u>; i++)
    {
        Tarjan(tree<u>*);
        Union_Set(u, tree<u>*);
        ancestor = u;
    }
    visit<u> = true;
    for (i=1; i<=query<u>; i++)
      if (visit*])
        ans*)]]++;

}

int main()
{
    int n, n1, m,a, b, root, i, j;
    char c1, c2, c3;
    while(scanf("%d", &n)!=EOF)
    {
        memset(rt, true, sizeof(rt));
        memset(visit, false, sizeof(visit));
        memset(ans, 0, sizeof(visit));
        for (i=1; i<=n; i++)
        {
            tree* = 0;
            query* = 0;
        }

        for (i=1; i<=n; i++)
        {
            scanf("%d%c%c%d%c", &m, &c1, &c2, &n1, &c3);
            if (n1 > 0)
                for (j=1; j<=n1; j++)
                {
                    scanf("%d", &a);
                    rt = false;
                    tree++;
                    tree] = a;
                }
        }
        for (i=1; i<=n; i++)
            if (rt*)
            {
                root = i;
                break;
            }
        scanf("%d\n", &n1);
        for (i=1; i<=n1;i++)
        {
            while (getchar()!='(') ;

            scanf("%d %d)%c", &a, &b, &c3);
            query++;
            query] = b;

            query**++;
            query**] = a;

        }
        Tarjan(root);
        for (i=1; i<=n; i++)
          if (ans* > 0)
            printf("%d:%d\n", i, ans*);
    }

    return 0;
}