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
  • 相关阅读:
    解决 'mvn' 不是内部或外部命令,也不是可运行的程序 或批处理文件。
    关于IDEA的Maven打jar包springboot项目问题,打成可执行jar包,IDEA创建的maven项目和spring initializr项目
    Flink接收RabbitMQ数据写入到Oracle
    操作MongoDB好用的图形化工具,Robomongo -> 下载 -> 安装
    PL/SQL Developer -> 下载 -> 安装 ->执行SQL -> 设置本地/远程连接
    MongoDB学习笔记,基础+增删改查+索引+聚合...
    SpringBoot整合MongoDB JPA,测试MongoRepository与MongoTemplate用法,简单增删改查+高级聚合
    Elasticsearch没看文档之前,整理的一些知识
    Elasticsearch中文文档,内容不全
    Elasticsearch 7.4.0官方文档操作
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3897190.html
Copyright © 2011-2022 走看看