现在的位置: 首页 > 综合 > 正文

POJ 1470 LCA问题,一次AC的,很鸡冻!

2017年11月23日 ⁄ 综合 ⁄ 共 2489字 ⁄ 字号 评论关闭
Closest Common Ancestors
Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 7415   Accepted: 2360

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

输入的部分需要做特殊处理,很难遇到如此变态要求的题目!

Source Code

Problem: 1470   User: bingshen
Memory: 240K   Time: 813MS
Language: C++   Result: Accepted
  • Source Code

    #include<stdio.h>
    #include<algorithm>
    #include<vector>
    #include<string.h>
    #define inf 99999999
    
    using namespace std;
    
    vector<int>tree[1000];
    int n,num;
    int eular[2000];
    int first[1000];
    int deep[2000];
    bool used[1000];
    int ans[1000];
    
    void init()
    {
    	int i;
    	num=0;
    	memset(ans,0,sizeof(ans));
    	for(i=0;i<1000;i++)
    	{
    		tree[i].clear();
    		memset(used,0,sizeof(used));
    	}
    }
    
    void dfs(int root,int dep)
    {
    	int i;
    	eular[num]=root;
    	deep[num]=dep;
    	num++;
    	for(i=0;i<tree[root].size();i++)
    	{
    		dfs(tree[root][i],dep+1);
    		eular[num]=root;
    		deep[num]=dep;
    		num++;
    	}
    }
    
    int find(int u,int v)
    {
    	int i,min,ret;
    	min=inf;
    	for(i=first[u];i<first[v];i++)
    	{
    		if(deep[i]<min)
    		{
    			min=deep[i];
    			ret=i;
    		}
    	}
    	return ret;
    }
    
    int main()
    {
    	int i,a,b,root,j,m,q,u,v;
    	while(scanf("%d",&n)!=EOF)
    	{
    		init();
    		for(i=0;i<n;i++)
    		{
    			scanf("%d:(%d)",&a,&m);
    			for(j=0;j<m;j++)
    			{
    				scanf("%d",&b);
    				tree[a].push_back(b);
    				used[b]=true;
    			}
    		}
    		for(i=1;i<=n;i++)
    		{
    			if(used[i]==false)
    			{
    				root=i;
    				break;
    			}
    		}
    		dfs(root,0);
    		memset(used,0,sizeof(used));
    		for(i=0;i<num;i++)
    		{
    			if(!used[eular[i]])
    			{
    				used[eular[i]]=true;
    				first[eular[i]]=i;
    			}
    		}
    		scanf("%d",&q);
    		while(q--)
    		{
    			scanf("%*[^(](%d %d)",&u,&v);
    			if(first[u]>first[v])
    			{
    				swap(u,v);
    			}
    			int k=find(u,v);
    			ans[eular[k]]++;
    		}
    		for(i=1;i<=n;i++)
    		{
    			if(ans[i])
    			{
    				printf("%d:%d/n",i,ans[i]);
    			}
    		}
    	}
    	return 0;
    }

抱歉!评论已关闭.