【BZOJ1787】【Ahoi2008】Meet 紧急集合 LCA、双倍经验

#include <stdio.h>
int main()
{
	puts("转载请注明出处谢谢");
	puts("http://blog.csdn.net/vmurder/article/details/43235305");
}

233……

【BZOJ1832】【AHOI2008】聚会
倍增lca

就是这道题。输入输出都没有改。

http://blog.csdn.net/vmurder/article/details/42607739

题解也在以前那篇博客里面。

代码:(直接复制的那篇博客)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 501000
#define LOGN 20
#define inf 0x3f3f3f3f
using namespace std;
struct KSD
{
	int v,next;
}e[N<<1];
int head[N],cnt;
inline void add(int u,int v)
{
	cnt++;
	e[cnt].v=v;
	e[cnt].next=head[u];
	head[u]=cnt;
}
int n,m;
int fa[N][LOGN],deep[N];
void dfs(int x,int p)
{
	int i,v;
	deep[x]=deep[p]+1;
	for(i=head[x];i;i=e[i].next)
	{
		v=e[i].v;
		if(v==p)continue;
		fa[v][0]=x;
		dfs(v,x);
	}
	return ;
}
inline void array()
{
	int i,j;
	for(j=1;j<LOGN;j++)
		for(i=1;i<=n;i++)
			fa[i][j]=fa[fa[i][j-1]][j-1];
}
int lcansx,lcansy;
inline int getlca(int x,int y)
{
	if(deep[x]<deep[y])swap(x,y);
	int i,j,k;
	lcansx=lcansy=0;
	for(i=LOGN-1;i>=0;i--)
		if(deep[fa[x][i]]>=deep[y])x=fa[x][i],lcansx+=(1<<i);
	if(x==y)return x;
	for(i=LOGN-1;i>=0;i--)
		if(fa[x][i]!=fa[y][i])
			x=fa[x][i],y=fa[y][i],lcansy+=(1<<i);
	lcansy+=1,lcansx+=lcansy;
	return fa[x][0];
}
int main()
{
//	freopen("test.in","r",stdin);
	int i,ans,id;
	int a,b,c;
	scanf("%d%d",&n,&m);
	for(i=1;i<n;i++)
	{
		scanf("%d%d",&a,&b);
		add(a,b),add(b,a);
	}
	dfs(1,0);
	array();
	while(m--)
	{
		scanf("%d%d%d",&a,&b,&c);

		int lca=getlca(a,b),temp=lcansx+lcansy;
		getlca(lca,c);
		temp+=lcansx+lcansy;
		ans=temp,id=lca;

		lca=getlca(a,c),temp=lcansx+lcansy;
		getlca(lca,b);
		temp+=lcansx+lcansy;
		if(ans>temp)ans=temp,id=lca;

		lca=getlca(b,c),temp=lcansx+lcansy;
		getlca(lca,a);
		temp+=lcansx+lcansy;
		if(ans>temp)ans=temp,id=lca;

		printf("%d %d\n",id,ans);
	}
	return 0;
}
时间: 2024-07-30 20:31:31

【BZOJ1787】【Ahoi2008】Meet 紧急集合 LCA、双倍经验的相关文章

[bzoj1787][Ahoi2008]Meet 紧急集合(lca)

传送门 可以看出,三个点两两之间的lca会有一对相同,而另一个lca就是聚集点. 然后搞搞就可以求出距离了. ——代码 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #define MAXN 1000001 5 6 using namespace std; 7 8 int n, m, cnt, ans; 9 int head[MAXN], to[MAXN], next[MAXN], de

【块状树】【LCA】bzoj1787 [Ahoi2008]Meet 紧急集合

分块LCA什么的,意外地快呢…… 就是对询问的3个点两两求LCA,若其中两组LCA相等,则答案为第三者. 然后用深度减一减什么的就求出距离了. 1 #include<cstdio> 2 #include<algorithm> 3 #include<cmath> 4 using namespace std; 5 #define maxn 500001 6 struct Graph 7 {int v[maxn<<1],first[maxn<<1],n

BZOJ1787 [Ahoi2008]Meet 紧急集合

水题 求出三个人每两个间的LCA,然后最小花费就是两两点之间的路径长度之和除以2 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 const int maxn = 500005; 6 struct edge{ 7 int v,next; 8 }e[maxn*2]; 9 struct ask{ 10 int v,next,lca,d,u; 11

【BZOJ-1787】Meet紧急集合 倍增LCA

1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 2259  Solved: 1023[Submit][Status][Discuss] Description Input Output Sample Input 6 4 1 2 2 3 2 4 4 5 5 6 4 5 6 6 3 1 2 4 4 6 6 6 Sample Output 5 2 2 5 4 1 6 0 HINT Source Day1

bzoj 1787: [Ahoi2008]Meet 紧急集合

1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 3016  Solved: 1344[Submit][Status][Discuss] Description Input Output Sample Input 6 4 1 2 2 3 2 4 4 5 5 6 4 5 6 6 3 1 2 4 4 6 6 6 Sample Output 5 2 2 5 4 1 6 0 HINT Source Day1

1787: [Ahoi2008]Meet 紧急集合

1787: [Ahoi2008]Meet 紧急集合 Description Input Output Sample Input 6 4 1 2 2 3 2 4 4 5 5 6 4 5 6 6 3 1 2 4 4 6 6 6 Sample Output 5 2 2 5 4 1 6 0 HINT 题解: n-1条边,很明显这是一棵树 那么题目就是求3个点的LCA 我们将3个点x,y,z分别求出x,y的LCA,设为a,x,z的为b,y,z的为c 则a,b,c 3个点中必有两个相等,答案就是另外一个(自

bzoj 1787 [Ahoi2008]Meet 紧急集合(1832 [AHOI2008]聚会)

1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 1841  Solved: 857[Submit][Status][Discuss] Description Input Output Sample Input 6 4 1 2 2 3 2 4 4 5 5 6 4 5 6 6 3 1 2 4 4 6 6 6 Sample Output 5 2 2 5 4 1 6 0 HINT Source Day1

BZOJ 1787: [Ahoi2008]Meet 紧急集合( 树链剖分 )

这道题用 LCA 就可以水过去 , 但是我太弱了 QAQ 倍增写LCA总是写残...于是就写了树链剖分... 其实也不难写 , 线段树也不用用到 , 自己YY一下然后搞一搞就过了...速度还挺快的好像= = #9 ---------------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algori

【Ahoi2008】【lca】【bzoj1787】Meet 紧急集合

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1787 题解: 求出三个点两两之间的lca会发现有两个是一样的,然后我们选那个不一样的就好了. #include<iostream> #include<cstdio> #include<cmath> #include<cstring> using namespace std; struct use{ int st,en; }b[5000001]; in