YunYan

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
Language:
Closest Common Ancestors
Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 24653   Accepted: 7648

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<iostream>
#include<cstdio>
#include<map>
#include<vector>
#include<cstring>
using namespace std;
const int N=1E4+7;
typedef long long ll;
ll bits[30];
bool pre[N];
int fa[N][30],depth[N];
vector<int >ve[N];
map<int ,int >mp;
map<int ,int >::iterator it;
void inint(){
    bits[0]=1;
    for(int i=1;i<30;i++){
        bits[i]=bits[i-1]<<1;
    }
}
void dfs(int x,int y){
    depth[x]=depth[y]+1;
    fa[x][0]=y;
    for(int i=1;i<30;i++) fa[x][i]=fa[fa[x][i-1]][i-1];
    
    for(int i=0;i<ve[x].size();i++){
        int dx=ve[x][i];
        if(dx!=y){
            dfs(dx,x);
        }
    }
}
int lca(int x,int y){
    if(depth[x]<depth[y]) swap(x,y);
    int dif=depth[x]-depth[y];
    for(int i=29;i>=0;i--){
        if(dif>=bits[i]){
            x=fa[x][i];
            dif-=bits[i];
        }
    }
    if(x==y) return x;
    for(int i=29;i>=0;i--){
        if(depth[x]>=bits[i]&&fa[x][i]!=fa[y][i]){
            x=fa[x][i];
            y=fa[y][i];
        }
    }
    return fa[x][0];
}
int main(){
    int t;
    inint();
    while(~scanf("%d",&t)){
    memset(pre,0,sizeof(pre));
    memset(depth,0,sizeof(depth));
    memset(fa,0,sizeof(fa));
    for(int i=1;i<=t;i++){
        ve[i].clear();
    }
    for(int i=1;i<=t;i++){
        int x,y;
        scanf("%d:(%d)",&x,&y);
        while(y--){
            int x1;
            scanf("%d",&x1);
            pre[x1]=1;
            ve[x].push_back(x1);
            ve[x1].push_back(x);
        }
    }
    int ancestor;
    for(int i=1;i<=t;i++){
        if(pre[i]==0){
            ancestor=i;
            break;
        
        }
    }
    dfs(ancestor,0);
    int n;
    scanf("%d",&n);
    
    for(int i=1;i<=n;i++){
        int x,y;
        char xx;
        while(xx=getchar()){
            if(xx=='(')
            break;
        }
        scanf("%d%d)",&x,&y);
        mp[lca(x,y)]++;
    }
    for(it=mp.begin();it!=mp.end();it++){
        if(it->second!=0){
            printf("%d:%d\n",it->first,it->second);
        }
    }
    mp.clear();
}
    return 0;
} 

 

posted on 2019-08-08 17:39  Target--fly  阅读(115)  评论(0编辑  收藏  举报