【点分治】poj1741 Tree / poj2114 Boatherds / poj1987 Distance Statistics

三道题都很类似。给出1741的代码

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define MAXN 10001
typedef pair<int,int> Point;
int n,K,ans;
int v[MAXN<<1],w[MAXN<<1],first[MAXN],next[MAXN<<1],en;
void AddEdge(const int &U,const int &V,const int &W)
{
	v[++en]=V;
	w[en]=W;
	next[en]=first[U];
	first[U]=en;
}
bool centroid[MAXN];//顶点是否已经作为重心删除的标记
int size[MAXN];//以该顶点为根的子树的大小
//计算子树的大小
int calc_sizes(int U,int Fa)
{
	int res=1;
	for(int i=first[U];i;i=next[i])
	  if(v[i]!=Fa&&(!centroid[v[i]]))
		res+=calc_sizes(v[i],U);
	return size[U]=res;
}
//查找重心的递归函数,nn是整个子树的大小
//在以U为根的子树中寻找一个顶点,使得删除该顶点后得到的最大子树的顶点数最少
//返回值为(最大子树的顶点数,顶点编号)
Point calc_centroid(int U,int Fa,int nn)
{
	Point res=make_pair(2147483647,-1);
	int sum=1,maxv=0;
	for(int i=first[U];i;i=next[i])
	  if(v[i]!=Fa&&(!centroid[v[i]]))
	    {
	      res=min(res,calc_centroid(v[i],U,nn));
	      maxv=max(maxv,size[v[i]]);
	      sum+=size[v[i]];
	    }
	maxv=max(maxv,nn-sum);
	res=min(res,make_pair(maxv,U));
	return res;
}
int td[MAXN],en2,ds[MAXN],en3;
//计算子树中所有顶点到重心的距离的递归函数
void calc_dis(int U,int Fa,int d)
{
	td[en2++]=d;
	for(int i=first[U];i;i=next[i])
	  if(v[i]!=Fa&&(!centroid[v[i]]))
	    calc_dis(v[i],U,d+w[i]);
}
int calc_pairs(int dis[],int En)
{
	int res=0;
	sort(dis,dis+En);
	for(int i=0;i<En;++i)
	  res+=upper_bound(dis+i+1,dis+En,K-dis[i])-(dis+i+1);
	return res;
}
void solve(int U)
{
	calc_sizes(U,-1);
	int s=calc_centroid(U,-1,size[U]).second;
	centroid[s]=1;
	//情况1:递归统计按重心s分割后的子树中的对数
	for(int i=first[s];i;i=next[i])
	  if(!centroid[v[i]])
	    solve(v[i]);
	//情况2:统计经过重心s的对数
	en3=0; ds[en3++]=0;
	for(int i=first[s];i;i=next[i])
	  if(!centroid[v[i]])
	    {
	      en2=0; calc_dis(v[i],s,w[i]);
	      ans-=calc_pairs(td,en2);//先把重复统计的部分(即情况1)减掉
	      memcpy(ds+en3,td,en2*sizeof(int)); en3+=en2;
	    }
	ans+=calc_pairs(ds,en3);
	centroid[s]=0;
}
void init()
{
	memset(first,0,sizeof(first));
	en=ans=0;
}
int main()
{
	while(1)
	  {
	  	scanf("%d%d",&n,&K);
		if(!n&&!K) break;
		init();
		int a,b,c;
		for(int i=1;i<n;++i)
	  	  {
	  		scanf("%d%d%d",&a,&b,&c);
	  		AddEdge(a,b,c); AddEdge(b,a,c);
	 	   }
	 	solve(1);
	 	printf("%d\n",ans);
	  }
	return 0;
}
时间: 2024-08-02 23:33:59

【点分治】poj1741 Tree / poj2114 Boatherds / poj1987 Distance Statistics的相关文章

POJ1987——Distance Statistics

Distance Statistics Time Limit: 2000MS   Memory Limit: 64000K Total Submissions: 1667   Accepted: 532 Case Time Limit: 1000MS Description Frustrated at the number of distance queries required to find a reasonable route for his cow marathon, FJ decide

poj1987 Distance Statistics

普通dfs访问每个点对的复杂度是O(n^2)的,显然会超时. 考虑访问到当前子树的根节点时,统计所有经过根的点(u, v)满足: dist(u) + dist(v) <= maxd,并且 belong(u)≠belong(v)(即u,v不在同一子树). 这里说的距离指的是节点到跟的距离. 可以用作差法,即用所有满足条件的点对数减去那些在根节点为当前子树根节点的儿子节点的点对数. 上面一步可以用O(nlogn)的复杂度解决,即先排序再比较. 根节点子树可以递归解决,用树的点分治. 总复杂度上界是O

POJ 1987 Distance Statistics 树分治

Distance Statistics Description Frustrated at the number of distance queries required to find a reasonable route for his cow marathon, FJ decides to ask queries from which he can learn more information. Specifically, he supplies an integer K (1 <= K

【POJ1987】Distance Statistics ==【POJ1741】 树分治

广告: #include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/44307489"); } 题意&题解 http://blog.csdn.net/vmurder/article/details/44302921 代码:(同一道题) #include <cstdio> #inclu

树分治基础模板以及树的重心(poj1741 tree)

好久没有更新博文了,这里更新一发~~ Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist(u,v)=The min distance between node u and v. Give an integer k,for every pair (u,v) of vertices is called valid if and only if dist(u,v

POJ-1741 Tree 【点分治】

Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist(u,v)=The min distance between node u and v. Give an integer k,for every pair (u,v) of vertices is called valid if and only if dist(u,v) not e

poj1741 Tree(点分治)

Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist(u,v)=The min distance between node u and v. Give an integer k,for every pair (u,v) of vertices is called valid if and only if dist(u,v) not e

POJ1741 Tree (点分治)

Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 25772   Accepted: 8566 Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist(u,v)=The min distance between node u and v. Give an

POJ1741:Tree——点分治

题面 POJ1741 解析  刚学了点分治,练一练模版题 过程就不多说了,主要说说细节 在每次查询下一棵子树时, 传进去的整棵子树大小是上一次的$siz$, 这个数据其实是错的, 但好像并不影响时间复杂度, 这样的话找重心就必须找最大子树最小的点了,否则会错.因此需要存一个当前最大子树最小的点的最大子树的大小, 以及当前的重心, 每次找重心之前,前者要赋为$inf$ 在每次统计以当前点为$lca$的链的答案时, 要先把这个重心加入数组中, 再按$dis$排序, 然后用双指针法或是其他可行的方法统