POJ2114-Boatherds-树分治

题目描述

Boatherds Inc. is a sailing company operating in the country of Trabantustan and offering boat trips on Trabantian rivers. All the rivers originate somewhere in the mountains and on their way down to the lowlands they gradually join and finally the single resulting river flows to the sea. Moreover, the Trabantian villages are exactly at the rivers’ springs, junctions and at the mouth of the largest river. Please note that more than 2 rivers can join at a junction. However, the rivers always form a tree (with villages as vertices).
The pricing policy of the Boatherds is very simple: each segment of each river between two villages is assigned a price (the price is same in both directions), so if a tourist requests a journey between any two villages, the ticket office clerks just add the prices of the segments along the only path between the villages.
One day, a very strange tourist appeared. She told the clerks that she returns to her country on the next day and she wants to spend all the remaining money on a boat trip, so they should find a route with exactly this cost. Being just poor (ahem) businessmen, they have asked the Abacus Calculator Makers for help.
You are given a description of the river network with costs of river segments and a sequence of integers x1,…, xk. For each xi, you should determine if there is a pair of cities (a, b) in the river network such that the cost of the trip between a and b is exactly xi.
Input
The input consists of several instances. Each instance is described by (in the following order):
A single line containing a single integer: the number of villages N (1 <= N <= 10 000).
N lines describing the villages. The i-th of these lines (1 <= i <= N) describes the village with number i. It contains space separated integers d1, c1, d2, c2, , dki, cki, 0. The dj’s are numbers of villages from which the rivers flow directly to the village i (with no other villages in between), each cj is the price of the journey between villages i and dj. Moreover, 2 <= dj <= N and 0 <= cj <= 1 000. Village 1 always corresponds to the mouth of the largest river, therefore no di can ever be equal to 1.
M <= 100 lines describing the queries. The i-th of these lines corresponds to the i-th query and contains a single integer xi (1 <= xi <= 10 000 000).
The instance is finished by a single line containing the number 0.
The whole input is ended by a single line containing the number 0.
Output
For each instance you should produce a sequence of M lines (where M is the number of queries in the particular instance). The i-th of these lines contains the word “AYE” if there exists a pair of cities in the river network which is connected by a path of cost xi, or the word “NAY” otherwise.
Output for each instance must be followed by a single line containing just the dot character.
Sample Input
6
2 5 3 7 4 1 0
0
5 2 6 3 0
0
0
0
1
8
13
14
0
0
Sample Output
AYE
AYE
NAY
AYE

树分治的板题,但是调了好久。原本想要用一个map记录所有路径,然后一次性回答所有的询问。可是写出来以后一直RE,没有办法写了一个一个回答的。可是也一直错。看了半天发现自己没有正确初始化,还有把路径长度和点搞混了。。。

AC代码

先贴一个一个算的。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<climits>
#include<algorithm>
#include<ctime>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<cmath>

using namespace std;
typedef long long ll;
const int MAXN=1e4+5;
struct edge
{
	int to,len,last;
}Edge[MAXN<<2]; int Last[MAXN],tot;
int n,kk,SonNum[MAXN],MaxNum[MAXN],Vis[MAXN],Dis[MAXN<<2];
int root,rootx,dlen,ss;
ll ans;

int getint()
{
	int x=0,sign=1; char c=getchar();
	while(c<'0' || c>'9')
	{
		if(c=='-') sign=-1; c=getchar();
	}
	while(c>='0' && c<='9')
	{
		x=x*10+c-'0'; c=getchar();
	}
	return x*sign;
}

void Init()
{
	for(int i=0;i<=n;++i) Last[i]=0;
	tot=0; 
}

void AddEdge(int u,int v,int w)
{
	Edge[++tot].to=v; Edge[tot].len=w; 
	Edge[tot].last=Last[u]; Last[u]=tot;
}

void GetRoot(int x,int father)
{
	int v;
	SonNum[x]=1; MaxNum[x]=1;
	for(int i=Last[x];i;i=Edge[i].last)
	{
		v=Edge[i].to; if(v==father || Vis[v]) continue;
		GetRoot(v,x);
		SonNum[x]+=SonNum[v];
		if(SonNum[v]>MaxNum[x]) MaxNum[x]=SonNum[x];
	}
	MaxNum[x]=max(MaxNum[x],ss-SonNum[x]);
	if(rootx>MaxNum[x]) root=x,rootx=MaxNum[x];
}

void GetDis(int x,int father,int dis)
{
	int v;
	Dis[++dlen]=dis;
	for(int i=Last[x];i;i=Edge[i].last)
	{
		v=Edge[i].to; if(v==father|| Vis[v]) continue;
		GetDis(v,x,dis+Edge[i].len);
	}
}

ll Count(int x,int dis)
{
	for(int i=0;i<=dlen;++i) Dis[i]=0;
	dlen=0;
	GetDis(x,0,dis);
	sort(Dis+1,Dis+1+dlen);
	ll ret=0;
	int i=1;
	while(i<=dlen && Dis[i]<=kk-Dis[i])
	{
		ll x=upper_bound(Dis+1,Dis+dlen+1,Dis[i])-lower_bound(Dis+1,Dis+dlen+1,Dis[i]);
		ll y=upper_bound(Dis+1,Dis+dlen+1,kk-Dis[i])-lower_bound(Dis+1,Dis+dlen+1,kk-Dis[i]);
		if(Dis[i]!=kk-Dis[i])
		ret+=x*y;
		else
		ret+=x*(x-1);
		i+=x;
	}
	return ret;
}

void Solve(int x)
{
	int v;
	ans+=Count(x,0);
	Vis[x]=true;
	for(int i=Last[x];i;i=Edge[i].last)
	{
		v=Edge[i].to; if(Vis[v]) continue;
		ans-=Count(v,Edge[i].len);
		ss=SonNum[v]; rootx=INT_MAX; root=0;
		GetRoot(v,x);
		Solve(root);
	}
}

void Read()
{
	int u,w;
	for(int i=1;i<=n;++i)
	{
		u=getint();
		while(u!=0)
		{
			w=getint(); AddEdge(i,u,w); AddEdge(u,i,w);
			u=getint();
		}
	}
}

void Work()
{
	rootx=INT_MAX; ss=n; root=0; ans=0;
	GetRoot(1,0); 
	Solve(root);
}

void Query()
{
	int x=getint();
	while(x!=0)
	{
		kk=x;
		Work();
		for(int i=1;i<=n;++i) Vis[i]=false;
		if(ans>0) printf("AYE\n");
		else printf("NAY\n");
		x=getint();
	}
	printf(".\n");
}

int main()
{
	while(1)
	{
		n=getint();
		if(n==0) break;
		Init();
		Read();
		Query();
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值