poj_1470Closest Common Ancestors

Closest Common Ancestors
Time Limit:2000MS Memory Limit:10000K
Total Submissions:12891 Accepted:4207

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

数据读入好奇葩


#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
using namespace std;
int main()
{
	int x, y;
	scanf("(%d %d)", &x, &y);
	cout << x << " " << y << endl;
	return 0;
}

这样进行标准输入的时候,不会忽略前导空白字符。。。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
#pragma warning(disable : 4996)

const int MAX = 10005;
int father[MAX];//表示x的父节点
int ranks[MAX];//表示x的秩
int indegree[MAX];//保存每个节点的入度
bool visited[MAX];//标记访问
vector<int> tree[MAX], Ques[MAX];
int ancestor[MAX];
map<int, int> ans;

void init(int n)
{
	for(int i = 1; i <= n; i++)
	{

		ranks[i] = 1;
		father[i] = i;
		indegree[i] = 0;
		visited[i] = false;
		ancestor[i] = 0;
		tree[i].clear();
		Ques[i].clear();
	}

}

int find(int x)
{
	if(x != father[x])
	{
		father[x] = find(father[x]);
	}
	return father[x];
}//查找函数,并压缩路径

void Union(int x, int y)
{
	x = find(x);
	y = find(y);
	if(x == y)
	{
		return;
	}
	else if(ranks[x] <= ranks[y])
	{
		father[x] = y;
		ranks[y] += ranks[x];
	}
	else
	{
		father[y] = x;
		ranks[x] += ranks[y];
	}

}//合并函数

void LCA(int u)
{
	ancestor[u] = u;
	for(int i = 0; i < tree[u].size(); i++)
	{
		LCA(tree[u][i]);
		Union(u, tree[u][i]);
		ancestor[find(u)] = u;
	}
	visited[u] = true;
	for(int i = 0; i < Ques[u].size(); i++)
	{
		//如果已经访问了问题节点,就可以返回结果了.
		if(visited[Ques[u][i]])
		{
			//printf("%d\n", ancestor[find(Ques[u][i])]);
			ans[ancestor[find(Ques[u][i])]]++;
			//break;
		}
	}
}

int main()
{
	freopen("in.txt", "r", stdin);
	int n, x, y, t, q;
	while(scanf("%d", &n) != EOF)
	{
		ans.clear();
		init(n);
		for(int i = 1; i <= n; i++)
		{
			scanf("%d:(%d)", &x, &t);
			while (t--)
			{
				scanf("%d", &y);
				tree[x].push_back(y);
				indegree[y]++;
			}
		}
		scanf("%d", &q);
		while (q--)
		{
			while (getchar() != '(')
			{
				;
			}
			scanf("%d %d", &x, &y);
			Ques[x].push_back(y);
			Ques[y].push_back(x);
			while (getchar() != ')')
			{
				;
			}
		}
		for(int i = 1; i <= n; i++)
		{
			//寻找根节点
			if(indegree[i] == 0)
			{
				LCA(i);
				break;
			}
		}
		map<int, int>::iterator it;
		for (it = ans.begin(); it != ans.end(); it++)
		{
			printf("%d:%d\n", it->first, it->second);
		}
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ 2182是一道使用树状数组解决的题目,题目要求对给定的n个数进行排序,并且输出每个数在排序后的相对位置。树状数组是一种用来高效处理前缀和问题的数据结构。 根据引用中的描述,我们可以通过遍历数组a,对于每个元素a[i],可以使用二分查找找到a到a[i-1]中小于a[i]的数的个数。这个个数就是它在排序后的相对位置。 代码中的query函数用来求前缀和,add函数用来更新树状数组。在主函数中,我们从后往前遍历数组a,通过二分查找找到每个元素在排序后的相对位置,并将结果存入ans数组中。 最后,我们按顺序输出ans数组的元素即可得到排序后的相对位置。 参考代码如下: ```C++ #include <iostream> #include <cstdio> using namespace std; int n, a += y; } } int main() { scanf("%d", &n); f = 1; for (int i = 2; i <= n; i++) { scanf("%d", &a[i]); f[i = i & -i; } for (int i = n; i >= 1; i--) { int l = 1, r = n; while (l <= r) { int mid = (l + r) / 2; int k = query(mid - 1); if (a[i > k) { l = mid + 1; } else if (a[i < k) { r = mid - 1; } else { while (b[mid]) { mid++; } ans[i = mid; b[mid = true; add(mid, -1); break; } } } for (int i = 1; i <= n; i++) { printf("%d\n", ans[i]); } return 0; } ``` 这段代码使用了树状数组来完成题目要求的排序功能,其中query函数用来求前缀和,add函数用来更新树状数组。在主函数中,我们从后往前遍历数组a,通过二分查找找到每个元素在排序后的相对位置,并将结果存入ans数组中。最后,我们按顺序输出ans数组的元素即可得到排序后的相对位置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [poj2182Lost Cows——树状数组快速查找](https://blog.csdn.net/aodan5477/article/details/102045839)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [poj_2182 线段树/树状数组](https://blog.csdn.net/weixin_34138139/article/details/86389799)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值