zoukankan      html  css  js  c++  java
  • POJ 1470 Tarjan算法

    裸的LCA,读入小坑。Tarjan算法大坑,一开始不知道哪儿错了,后来才发现,是vis数组忘了清零了(⊙﹏⊙)b
    傻傻的用了邻接矩阵。。。很慢啊,1100多ms。

    Closest Common Ancestors

    Description

    Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the program determines the closest common ancestor of u and v in the tree. The closest common ancestor of two nodes u and v is the node w that is an ancestor of both u and v and has the greatest depth in the tree. A node can be its own ancestor (for example in Figure 1 the ancestors of node 2 are 2 and 5)
    Input

    The data set, which is read from a the std input, starts with the tree description, in the form:

    nr_of_vertices
    vertex:(nr_of_successors) successor1 successor2 … successorn

    where vertices are represented as integers from 1 to n ( n <= 900 ). The tree description is followed by a list of pairs of vertices, in the form:
    nr_of_pairs
    (u v) (x y) …

    The input file contents several data sets (at least one).
    Note that white-spaces (tabs, spaces and line breaks) can be used freely in the input.
    Output

    For each common ancestor the program prints the ancestor and the number of pair for which it is an ancestor. The results are printed on the standard output on separate lines, in to the ascending order of the vertices, in the format: ancestor:times
    For example, for the following tree:

    Sample Input

    5
    5:(3) 1 4 2
    1:(0)
    4:(0)
    2:(1) 3
    3:(0)
    6
    (1 5) (1 4) (4 2)
    (2 3)
    (1 3) (4 3)
    Sample Output

    2:1
    5:5
    Hint

    Huge input, scanf is recommended.

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    char jy;
    bool vis[999];
    int q,m,jyy,first[999],ans[999],next[999999],tot,qu[999][999],v[999999],n,k,xx,yy,root,f[999];
    int find(int x)
    {
        return x==f[x]?x:f[x]=find(f[x]);
    }
    void Tarjan(int x)
    {
        f[x]=x;
        for(int i=first[x];~i;i=next[i])
        {
            Tarjan(v[i]);
            f[find(v[i])]=x;
        }
        vis[x]=1;
        for(int i=1;i<=n;i++)
        {
            if(vis[i]&&qu[x][i])
            {
                ans[find(i)]+=qu[x][i];
                qu[x][i]=qu[i][x]=0;
            }
        }
    }
    int main()
    {
        while(scanf("%d",&n)!=EOF)
        {
            memset(qu,0,sizeof(qu));
            memset(ans,0,sizeof(ans));
            memset(vis,0,sizeof(vis));
            memset(first,-1,sizeof(first));
            tot=1;
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&q);
                while(scanf("%c",&jy)&&jy!='(');
                scanf("%d",&m);
                while(scanf("%c",&jy)&&jy!=')');
                for(int i=1;i<=m;i++)
                {
                    scanf("%d",&v[tot]);
                    vis[v[tot]]=1;
                    next[tot]=first[q];
                    first[q]=tot++;
                }
            }
            for(int i=1;i<=n;i++)
                if(!vis[i])
                {
                    root=i;
                    break;
                }
            memset(vis,0,sizeof(vis));
            scanf("%d",&k);
            for(int i=1;i<=k;i++)
            {
                while(scanf("%c",&jy)&&jy!='(');
                scanf("%d%d",&xx,&yy);
                qu[xx][yy]++;
                qu[yy][xx]=qu[xx][yy];
                while(scanf("%c",&jy)&&jy!=')');
            }
            Tarjan(root);
            for(int i=1;i<=n;i++)
                if(ans[i])
                    printf("%d:%d
    ",i,ans[i]);
        }
    } 

    这里写图片描述

  • 相关阅读:
    网址收藏
    Linux创建swap文件
    vim命令大全
    char * 和字符数组
    JSR 203终于要出来啦
    对象关系技术的探讨
    最近编码更流畅了
    孤独终止的地方,就是广场开始的地方......
    不要奢望.NET能够跨平台
    实现了HTTP多线程下载
  • 原文地址:https://www.cnblogs.com/SiriusRen/p/6532488.html
Copyright © 2011-2022 走看看