zoukankan      html  css  js  c++  java
  • BNUOJ 1589 Closest Common Ancestors

    Closest Common Ancestors

    Time Limit: 2000ms
    Memory Limit: 10000KB
    This problem will be judged on PKU. Original ID: 1470
    64-bit integer IO format: %lld      Java class name: Main
     
     
    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

     
    解题:LCA。。。。
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <vector>
     6 #include <climits>
     7 #include <algorithm>
     8 #include <cmath>
     9 #define LL long long
    10 #define INF 0x3f3f3f
    11 using namespace std;
    12 const int maxn = 1000;
    13 vector<int>g[maxn];
    14 vector<int>q[maxn];
    15 int n,m,cnt[maxn],uf[maxn];
    16 bool vis[maxn],indeg[maxn];
    17 int Find(int x) {
    18     if(x != uf[x])
    19         uf[x] = Find(uf[x]);
    20     return uf[x];
    21 }
    22 void tarjan(int u) {
    23     int i;
    24     uf[u] = u;
    25     for(i = 0; i < g[u].size(); i++) {
    26         if(!vis[g[u][i]] && g[u][i] != u) {
    27             tarjan(g[u][i]);
    28             uf[g[u][i]] = u;
    29         }
    30     }
    31     vis[u] = true;
    32     for(i = 0; i < q[u].size(); i++) {
    33         if(vis[q[u][i]]) cnt[Find(q[u][i])]++;
    34     }
    35 }
    36 int main() {
    37     int i,j,u,v,k;
    38     while(~scanf("%d",&n)) {
    39         for(i = 0; i <= n; i++) {
    40             g[i].clear();
    41             q[i].clear();
    42             cnt[i] = 0;
    43             indeg[i] = false;
    44         }
    45         for(i = 0; i < n; i++) {
    46             scanf("%d:(%d)",&u,&k);
    47             for(j = 0; j < k; j++) {
    48                 scanf("%d",&v);
    49                 g[u].push_back(v);
    50                 indeg[v] = true;
    51             }
    52         }
    53         scanf("%d",&m);
    54         while(m--) {
    55             scanf(" (%d %d)",&u,&v);
    56             q[u].push_back(v);
    57             q[v].push_back(u);
    58         }
    59         memset(vis,false,sizeof(vis));
    60         memset(cnt,0,sizeof(cnt));
    61         for(i = 1; i <= n; i++)
    62             if(!indeg[i]) {
    63                 tarjan(i);
    64                 break;
    65             }
    66         for(i = 1; i <= n; i++)
    67             if(cnt[i]) printf("%d:%d
    ",i,cnt[i]);
    68     }
    69     return 0;
    70 }
    View Code
  • 相关阅读:
    RxJava+okhttp3
    RetrofitOkHttp网络请求
    布局111
    网络请求展示数据
    一级列表展示购物车
    终极MVP二级购物车
    将博客搬至CSDN
    nyoj-开灯问题
    nyoj-数乌龟
    nyoj 正三角形的外接圆面积
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3897190.html
Copyright © 2011-2022 走看看