zoukankan      html  css  js  c++  java
  • ClosestCommon Ancestors·POJ1470

    传送门:http://poj.org/problem?id=1470

    Closest Common Ancestors
    Time Limit: 2000MS   Memory Limit: 10000K
         

    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.

    Source

     
    第一个Tarjan LCA。。WA了N次的原因居然是。。。。。。。。。。。。。。。。。。。。。。输出没带 “  :” 。。。视力拙计
     
    Codes:
     1 #include<set>
     2 #include<queue>
     3 #include<vector>
     4 #include<cstdio>
     5 #include<cstring>
     6 #include<iostream>
     7 #include<algorithm>
     8 using namespace std;
     9 const int N = 910;
    10 #define For(i,n) for(int i=1;i<=n;i++)
    11 #define Rep(i,l,r) for(int i=l;i<=r;i++)
    12 
    13 struct edge{
    14     int t,next;
    15 }E[N*2];
    16 int head[N];
    17 vector<int> Q[N];
    18 int indeg[N],fa[N],anc[N],Ans[N],Hash[N][N];
    19 int Es,n,m,x,y,tot,root;char ch;
    20 bool vis[N];
    21 void makelist(int s,int t){
    22     E[Es].t = t; E[Es].next = head[s];
    23     head[s] = Es++;
    24 }
    25 
    26 int read(){
    27     ch = getchar();int num = 0;
    28     while(ch>'9'||ch<'0') ch = getchar();
    29     while(ch>='0'&&ch<='9'){
    30         num = num * 10 + ch - '0';
    31         ch = getchar();
    32     }
    33     return num;
    34 }
    35 
    36 int find(int i){
    37     return (fa[i]==i)?(i):(find(fa[i]));
    38 }
    39 
    40 void LCA(int i){
    41     anc[i] = i;
    42     for(int p = head[i];p!=-1;p=E[p].next){
    43         if(vis[E[p].t]) continue;
    44         LCA(E[p].t);
    45         fa[find(E[p].t)] = find(i);
    46         anc[find(i)] = i;
    47     }
    48     vis[i] = true;
    49     for(int j=0;j<Q[i].size();j++)
    50         if(vis[Q[i][j]]&&(Hash[i][Q[i][j]]))    {
    51             Ans[anc[find(Q[i][j])]]+=Hash[i][Q[i][j]];
    52             Hash[i][Q[i][j]] = Hash[Q[i][j]][i] = 0;
    53         }
    54 }
    55 
    56 int main(){
    57     while(scanf("%d",&n)!=EOF){
    58         memset(Ans,0,sizeof(Ans));Es = 0;
    59         memset(Hash,false,sizeof(Hash));
    60         memset(head,-1,sizeof(head));
    61         memset(indeg,0,sizeof(indeg));memset(anc,0,sizeof(anc));
    62         memset(vis,false,sizeof(vis));
    63         For(i,n){
    64             Q[i].clear();
    65             x = read(); tot = read();
    66             For(j,tot){
    67                 y = read();
    68                 makelist(x,y);
    69                 indeg[y]++;
    70             }
    71         }
    72         m = read();
    73         For(i,m){
    74             int x = read() , y = read();
    75             Hash[x][y]++;Hash[y][x]++;
    76             Q[x].push_back(y);Q[y].push_back(x);
    77         }
    78         For(i,n) {
    79             root = (!indeg[i]) ? i : root;
    80             fa[i] = i;
    81         }
    82         LCA(root);
    83         For(i,n) 
    84           if(Ans[i]) printf("%d:%d
    ",i,Ans[i]);
    85     }
    86     return 0;
    87 }
  • 相关阅读:
    spark的map和mapPartitions
    scala 语法速查
    spark 在yarn模式下提交作业
    spark的wordcount
    spark安装
    hadoop mapreduce求解有序TopN(高效模式)
    hadoop 自定义OutputFormat
    ONI无法启动: Uh oh! Unable to launch Neovim...
    Macbook外接显示器模糊解决方法
    macOS: sudo : Operation not permitted
  • 原文地址:https://www.cnblogs.com/zjdx1998/p/3886669.html
Copyright © 2011-2022 走看看