poj 1330 Nearest Common Ancestors lca rmq在线算法

原创 2016年06月01日 14:33:53

题意:裸的lca


思路:直接上rmq在线模板


题目链接:http://poj.org/problem?id=1330


#include <cstdio>
#include <cstring>
#include <algorithm>

#pragma warning (disable: 4996)

using namespace std;

const int maxn = 10005;

struct edge
{
	int u, v, next;
}e[maxn];
int head[maxn], cnt;
int n;
int vs[maxn << 1];
int dep[maxn << 1];
int id[maxn];
int dp[maxn << 1][20];
bool in[maxn];
int root;

void init()
{
	cnt = 0;
	memset(in, false, sizeof(in));
	memset(head, -1, sizeof(head));
}

int Min(int x, int y)
{
	return dep[x] <= dep[y] ? x : y;
}

void addedge(int u, int v)
{
	e[cnt].u = u, e[cnt].v = v, e[cnt].next = head[u], head[u] = cnt++;
}

void rmq_init(int n)
{
	for (int i = 1; i <= n; i++)
		dp[i][0] = i;
	for (int j = 1; (1 << j) <= n; j++)
	{
		for (int i = 1; i + (1 << j) - 1 < n; i++)
			dp[i][j] = Min(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
	}
}

int rmq(int l, int r)
{
	int k = 0;
	while ((1 << (k + 1)) <= (r - l + 1)) k++;
	return Min(dp[l][k], dp[r - (1 << k) + 1][k]);
}

void dfs(int u, int p, int d, int &k)
{
	id[u] = k;
	vs[k] = u;
	dep[k++] = d;
	int v;
	for (int i = head[u]; i != -1; i = e[i].next)
	{
		v = e[i].v;
		if (v != p)
		{
			dfs(v, u, d + 1, k);
			vs[k] = u;
			dep[k++] = d;
		}
	}
}


void build()
{
	scanf("%d", &n);
	for (int i = 0; i < n - 1; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		addedge(u, v);
		in[v] = true;
	}
	for (int i = 1; i <= n; i++)
	{
		if (!in[i])
		{
			root = i;
			break;
		}
	}
}

int lca(int u, int v)
{
	return vs[rmq(min(id[u], id[v]), max(id[u], id[v]))];
}

void solve()
{
	int u, v, k = 0;
	scanf("%d%d", &u, &v);
	dfs(root, -1, 0, k);
	rmq_init(n * 2);
	int ans = lca(u, v);
	printf("%d\n", ans);
}

int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		init();
		build();
		solve();
	}
	return 0;
}



版权声明:转载务必请标明出处,谢谢

相关文章推荐

POJ 1330 Nearest Common Ancestors (在线LCA转RMQ)

题目地址:POJ 1330 在线LCA转RMQ第一发。所谓在线LCA,就是先DFS一次,求出遍历路径和各个点深度,那么求最近公共祖先的时候就可以转化成求从u到v经过的点中深度最小的那个。 纯模板题...

POJ 1330 Nearest Common Ancestors [LCA+RMQ]

LCA的入门题,我用的是ST在线算法和Tarjan离线算法。 ST:#include #include #include #include #include #include usin...

POJ 1330 Nearest Common Ancestors LCA--》RMQ or 纯DFS

Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22446   ...

POJ 1330 Nearest Common Ancestors(LCA,在线处理三种方式)

题目链接: POJ 1330 Nearest Common Ancestors 题意: 给一个nn点和n−1n-1条边的树,第nn行是要查询的两个节点的最近公共祖先,输出要查询的最近公共祖先。...

【POJ】1330 Nearest Common Ancestors 在线LCA,倍增思想

Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions...

poj1330 Nearest Common Ancestors LCA倍增法 或 LCA转RMQ

Language: Default Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K ...

poj1330 Nearest Common Ancestors(LCA离线算法)

http://poj.org/problem?id=1330 题意:给你n个节点和n-1个关系,通过关系建起一棵树,然后给你两个节点,求他们的最小公共祖先。 ps:这里的Tarjan离线算法貌似...

poj 1330 Nearest Common Ancestors 朴素LCA算法

Nearest Common AncestorsTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 9213 Accepted: 488...

POJ1330 Nearest Common Ancestors(最近公共祖先LCA 并查集+DFS)

Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions...

LCA-倍增思想 POJ1330 Nearest Common Ancestors

LCA 倍增
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)