POJ 1470 Closest Common Ancestors 离线LCA

实测查询量大概是25W左右,离线搞比较快.

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#include <stack>
#include <string>
#include <iostream>
#include <cmath>
#include <climits>

using namespace std;
const int maxn = 1000;
struct Edge {
	int u, v, id;
	Edge(int u, int v, int id = 0): u(u), v(v), id(id) {}
};
vector<Edge> e[maxn], q[maxn];
int n, fa[maxn], ans[260000], col[maxn], pfa[maxn];
map<int, int> mp;

int getfa(int u) {
	return fa[u] == u ? u : fa[u] = getfa(fa[u]);
}

void dfs(int now) {
	int qsize = q[now].size();
	col[now] = 1;
	for(int i = 0; i < qsize; i++) if(ans[q[now][i].id] == -1) {
		int v = q[now][i].v;
		if(col[v] != 0) ans[q[now][i].id] = getfa(v);
	}
	int esize = e[now].size();
	for(int i = 0; i < esize; i++) {
		int v = e[now][i].v;
		dfs(v);
		fa[v] = now;
	}
}

int main() {
	while(scanf("%d", &n) != EOF) {
		for(int i = 1; i <= n; i++) {
			e[i].clear(); q[i].clear();
			fa[i] = i; col[i] = 0;
		}
		memset(pfa, -1, sizeof(pfa));
		for(int i = 1; i <= n; i++) {
			int now, cnt; scanf("%d:(%d)", &now, &cnt);
			for(int j = 0; j < cnt; j++) {
				int tmp; scanf("%d", &tmp);
				e[now].push_back(Edge(now, tmp));
				pfa[tmp] = now;
			}
		}
		int qcnt; scanf("%d", &qcnt);
		char kk;
		memset(ans, -1, sizeof(ans));
		for(int i = 1; i <= qcnt; i++) {
			int a, b;
			scanf(" %c%d %d%c", &kk, &a, &b, &kk);
			q[a].push_back(Edge(a, b, i));
			q[b].push_back(Edge(b, a, i));
		}
		int root;
		for(int i = 1; i <= n; i++) if(pfa[i] == -1) root = i;
		dfs(root);
		mp.clear();
		for(int i = 1; i <= qcnt; i++) mp[ans[i]]++;
		for(map<int, int>::iterator i = mp.begin(); i != mp.end(); i++) {
			cout << i->first << ":" << i->second << endl;
		}
	}
	return 0;
}

  

时间: 2024-10-16 20:39:05

POJ 1470 Closest Common Ancestors 离线LCA的相关文章

POJ 1470 Closest Common Ancestors 【LCA】

任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 22519   Accepted: 7137 Description Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pa

POJ 1470 Closest Common Ancestors (在线LCA转RMQ)

题目地址:POJ 1470 LCA模板题..输入有点坑,还有输入的第一个结点不一定是根节点. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #include

poj 1470 Closest Common Ancestors 【Tarjan 离线 LCA】

题目:poj 1470 Closest Common Ancestors 题意:给出一个树,一些询问.求LCA的个数. 分析:很简单的模板题目,但是模板不够优秀,一直wa...RE,各种错误一下午,终于发现自己模板的漏洞了. AC代码: #include <iostream> #include <cstdio> #include <cstring> #include <vector> using namespace std; #define N 1010 #

POJ 1470 Closest Common Ancestors LCA题解

本题也是找LCA的题目,不过要求多次查询,一般的暴力查询就必然超时了,故此必须使用更高级的方法,这里使用Tarjan算法. 本题处理Tarjan算法,似乎输入处理也挺麻烦的. 注意: 因为查询的数据会极大,故此使用一个数组记录所有查询数据就会超时的.我就载在这里了.查了好久才想到这点.因为我使用了一个vector容器记录了查询数据,故此每次都循环这组这么大的数据,就超时了.----解决办法:使用一个vector<int> quest来记录查询数组,这样每次都只需要循环某节点的邻接查询点就可以了

POJ 1470 Closest Common Ancestors【最近公共祖先LCA】

题目链接:http://poj.org/problem?id=1470 题目大意:给出一棵树,再给出若干组数(a,b),输出节点a和节点b的最近公共祖先(LCA) 就是很裸的LCA,但是我用的是<挑战程序设计竞赛>上的"基于二分搜索的算法求LCA",我看网上用的都是tarjan算法.但是我的代码不知道为什么提交上去 wrong answer,自己想的很多测试数据也都和题解结果一样,不知道错在哪里,所以把代码保存一下,留待以后解决...... 如果读者有什么建议,希望提出来,

POJ 题目1470 Closest Common Ancestors(LCA)

Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 16671   Accepted: 5319 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

POJ 1470 Closest Common Ancestors

传送门 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 17306   Accepted: 5549 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

POJ 1470 Closest Common Ancestors 采用树结构的非线性表编程

A - Closest Common Ancestors(8.4.9) Time Limit:2000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Description Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the prog

poj 1470 Closest Common Ancestors LCA

题目链接:http://poj.org/problem?id=1470 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 n