码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ 3251 树上三角形 暴力

时间:2015-01-26 22:57:17      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:bzoj   bzoj3251   暴力   

题目大意:给定一棵树,每个点上有点权,多次修改点权,以及查询两点间路径上所有点权之间能否找出三个值构成三角形的三边长

被逗了- -

首先考虑如果一些数不能构成三角形的三边长,那么这些数最多有多少个?

显然当这些数构成斐波那契数列的时候数值的个数最多- -

那么2^31以内共有多少个斐波那契数?46!

也就是说当两点间路径上的点>=47时答案一定是YES!

那么小于47时只要暴力就行- - 时间复杂度O(mlogk) 其中k是最大的数的大小- -

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define M 100100
using namespace std;
struct abcd{
	int to,next;
}table[M<<1];
int head[M],tot;
int n,m,a[M],fa[M],dpt[M];
int stack[60],top;
void Add(int x,int y)
{
	table[++tot].to=y;
	table[tot].next=head[x];
	head[x]=tot;
}
void DFS(int x)
{
	int i;
	dpt[x]=dpt[fa[x]]+1;
	for(i=head[x];i;i=table[i].next)
		if(table[i].to!=fa[x])
		{
			fa[table[i].to]=x;
			DFS(table[i].to);
		}
}
bool Check(int x,int y)
{
	top=0;
	if(dpt[x]<dpt[y])
		swap(x,y);
	while(dpt[x]>dpt[y])
	{
		stack[++top]=a[x];
		if(top>=47) return true;
		x=fa[x];
	}
	while(x!=y)
	{
		stack[++top]=a[x];
		stack[++top]=a[y];
		if(top>=47) return true;
		x=fa[x];y=fa[y];
	}
	stack[++top]=a[x];
	if(top>=47) return true;
	sort(stack+1,stack+top+1);
	for(int i=3;i<=top;i++)
		if(stack[i]-stack[i-1]<stack[i-2])
			return true;
	return false;
}
int main()
{
	int i,x,y,p;
	cin>>n>>m;
	for(i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(i=1;i<n;i++)
	{
		scanf("%d%d",&x,&y);
		Add(x,y);Add(y,x);
	}
	DFS(1);
	for(i=1;i<=m;i++)
	{
		scanf("%d%d%d",&p,&x,&y);
		if(!p)
			puts(Check(x,y)?"Y":"N");
		else
			a[x]=y;
	}
	return 0;
}


BZOJ 3251 树上三角形 暴力

标签:bzoj   bzoj3251   暴力   

原文地址:http://blog.csdn.net/popoqqq/article/details/43156247

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!