POJ1470 (Closest Common Ancestors)

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.

题目源于网络

题意

输入n,有1~n的节点,接下来n行输入 x:(k) y1, y2, …,yk.表示x节点有k个子节点,分别是y1, y2, …,yk. 输入m,接下来m行输入m个(u v),计算这些(u v)的最近祖先结点,结果将1~n的节点中,曾作为m个(u v)的最近祖先结点的节点输出,并输出有多少个(u v)的最近祖先节点是该节点。

解析

和D题一样,还是求最近祖先节点,只不过(u v)数量增多,结果需要计算个数。在D题基础上加一个计数数组即可。我这里方法和自己写的D题略有不同,我就再说一下我的方法。定义一个bool vis[]数组,每输入一次(u v)就置0,先搜索u的祖先节点,从u先搜其父亲节点,再令u为u的父亲节点,这样再搜,就得到u的爷爷节点,同理直到根节点,即father[u] = 0,这样逆序求解保证距离最近,因为每往下一步得到的父亲节点距离都是+1,在搜索同时将每一个搜索到的vis[u] = 1,这样再搜索v的祖先节点,方法同v如果vis[u] == 1,就说明u有这个祖先节点,那即找到二者的最近祖先。这题输入会比较麻烦些,但也还好。重点解题步骤我会在代码中标注。(说的详细点,方便别人看,以后自己看起来也容易懂)

/*
一开始担心会超时,没想到还跑得挺快的
Time  547ms
Memory  124kB
Length  1139
Time limit  2000 ms
Memory limit  10000 kB
*/
#include<iostream>
#include<math.h>
#include <algorithm>
#include<stdio.h>
#include<string.h>
#include<stack> 
#include<queue>
using namespace std;
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define req(i, a, b) for(int i=(a); i<=(b); i++)
#define ull unsigned __int64
#define sc(t) scanf("%d",&(t))
#define sc2(t,x) scanf("%d%d",&(t),&(x))
#define pr(t) printf("%d\n",(t))
#define pf printf
#define prk printf("\n")
#define pi acos(-1.0)
#define ms(a,b) memset((a),(b),sizeof((a)))
#define mc(a,b) memcpy((a),(b),sizeof((a))) 
#define w while
#define vr vector<int>
typedef long long ll;


int ans[905]; //计数数组
int father[905]; //记录父亲节点
bool vis[905]; //记录数组
int n, m;

int main()
{
    int u, v, k;
    int x, y;
    char c;
    w(~sc(n))
    {
        ms(ans, 0);
        ms(father, 0);
        req(i, 1, n)
        {
            getchar(); // 一定要
            scanf("%d:(%d)",&x,&k);
            req(j, 1, k)
            {
                sc(y);
                father[y] = x;
            }
        }
        sc(m);
        req(i, 1, m)
        {
            scanf("%*1s%d%d%*1s",&u,&v); //%*1s缓冲换行空格之类的
            ms(vis, false);
            w(u) //关键代码标记祖先节点
            {
                vis[u] = true;
                u = father[u];
            }
            w(vis[v] == false) //关键代码,逆序求最近
            {
                v = father[v];
            }
            ans[v]++;
        }
        req(i, 1, n)
        if(ans[i])
        pf("%d:%d\n",i,ans[i]);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值