hdu4812 D Tree(树分治)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4812

题意:

给定一棵树,N-1条边,K,每个结点都有一个权值。找到树上两个结点之间所有结点(包括起点和终点的结点)的权值乘积=K,若有多组答案,输出字典序最小的一组,否则输出"No solution"。

分析:

使用链式前向星的数据结构,不熟悉的先往这走:https://blog.csdn.net/acdreamers/article/details/16902023

首先找到整棵树的中心Root,以Root为根搜索答案,然后取以Root的子结点为根的子树上搜索答案,显然递归实现。

那么,以Root为根的树,如何去搜索答案呢?

把Root的子结点分别作为起点结点u,把以u为根的树上的其他结点v作为终点结点,可以得到u到v的路径的值,记录在arr数组里。显然,v到Root的路径值为arr*V[u](V[u]为u结点的权值)。

然后对于每一个arr[i],考虑是否存在另一个点到Root的任一子结点的路径值=K*inv[1LL*arr*V[Root]%INF]%INF,若存在,则这两个点之间的路径的值=(K*inv[1LL*arr*V[Root]%INF]%INF)*arr*V[Root]=K,满足题意。(INF=1e6+3)

实现:

链式前向星:
struct node{
	int to,next;
}edge[MAXN*2];
int V[MAXN],head[MAXN],tot,N,K,ans_x,ans_y;
void addEdge(int u,int v){//加边 
	edge[++tot].to=v;
	edge[tot].next=head[u];
	head[u]=tot;
}

以第一组数据为例:

5 60

2 5 2 3 3

1 2

1 3

2 4 2 5

得到的edge数组和head数组是这样的:

搜索的过程中,cnt记录找到多少个结点,id[cnt]记录这个结点属于Root的子结点u的子树,arr[u]记录这个结点到u的路径值,数组T[i]记录到Root的子结点的路径值为i的结点编号,T[arr[id[cnt]]]=id[cnt]。

对于以Root为根的树,搜索过程DFS如下:

1、设置T[1]=Root,vis[Root]=true,因为K*inv[1LL*arr*V[Root]%INF]%INF=1时说明其中另一个结点就是根Root。

2、遍历Root的每一个子结点u,如果未被访问过,调用dfs(edge[i].to,V[edge[i].to],Root),计算子树u的每一个结点到u的路径值arr,对于每一个arr寻找T数组是否有配对的结点,有则更新,然后更新T数组。

3、以Root为根的树已经搜索完毕,清空T数组。

4、遍历Root的每一个子结点u,递归调用DFS,去以u为根的子树搜索答案。

另外,逆元需要预处理,否则可能超时。

总结:

1、很好的复习了链式前向星,同时对树分治有了更深的理解。

2、忘了初始化Root变量,一直WA,调试了几个小时才发现。。。

代码:

#pragma comment(linker,"/STACK:102400000,102400000")
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXN=200010;
const int INF=1e6+3;
inline ll read(){
    char c = getchar(); ll x = 0, z = 1;
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + c - '0';
    return x * z;
}
/*
  链式前向星 
  edge[i].next:表示第i条边的下一条边的下标
  head[i]:表示结点i的最后一个子结点的序号
*/
struct node{
	int to,next;
}edge[MAXN*2];
int V[MAXN],head[MAXN],tot,N,K,ans_x,ans_y;
void addEdge(int u,int v){//加边 
	edge[++tot].to=v;
	edge[tot].next=head[u];
	head[u]=tot;
}
bool vis[MAXN];
int Root,Tsize,size[MAXN],wt[MAXN];
int arr[INF],id[MAXN],inv[INF],T[INF],cnt;
void getRoot(int u,int f){//找重心 
	size[u]=1;
	wt[u]=0;
	for(int i=head[u];i!=-1;i=edge[i].next){
		if(edge[i].to!=f&&(!vis[edge[i].to])){
			getRoot(edge[i].to,u);
			size[u]+=size[edge[i].to];
			wt[u]=max(wt[u],size[edge[i].to]);
		}
	}
	wt[u]=max(wt[u],Tsize-size[u]);
	if(wt[Root]>wt[u])
		Root=u;
}
void dfs(int u,int D,int f){//找到u这棵子树的所有到u的路径的乘积 
	id[++cnt]=u;
	arr[u]=D;
	for(int i=head[u];i!=-1;i=edge[i].next){
		if(edge[i].to!=f&&(!vis[edge[i].to]))
			dfs(edge[i].to,1LL*D*V[edge[i].to]%INF,u);
	}
}
void DFS(int u){
	T[1]=u;
	vis[u]=true;
	for(int i=head[u];i!=-1;i=edge[i].next){
		if(!vis[edge[i].to]){
			cnt=0;
			dfs(edge[i].to,V[edge[i].to],u);
			for(int i=1;i<=cnt;i++){
				int x=id[i],y=arr[x],z;
				z=T[1LL*K*inv[1LL*y*V[u]%INF]%INF];
				//更新答案 
				if(z){
					if(x>z)
						swap(x,z);
					if(x<ans_x||(x==ans_x&&z<ans_y)){
						ans_x=x;
						ans_y=z;
					}
				}
			}
			//更新T数组 
			for(int i=1;i<=cnt;i++){
				if(!T[arr[id[i]]] || id[i]<T[arr[id[i]]])
					T[arr[id[i]]]=id[i];
			} 
		} 
	}
	//清空T数组 
	for(int i=head[u];i!=-1;i=edge[i].next){
		if(!vis[edge[i].to]){
			cnt=0;
			dfs(edge[i].to,V[edge[i].to],u);
			for(int i=1;i<=cnt;i++)
				T[arr[id[i]]]=0;
		}
	}
	T[1]=0;
	for(int i=head[u];i!=-1;i=edge[i].next){
		if(!vis[edge[i].to]){
			Root=0;
			Tsize=size[edge[i].to];
			getRoot(edge[i].to,u);
			DFS(Root);
		}
	}
}
int main(){
	//预处理逆元
	inv[1]=1;
	for(int i=2;i<INF;i++)
		inv[i]=(1LL*(-(INF/i))*inv[INF%i]%INF+INF)%INF;
	while(scanf("%d%d",&N,&K)!=EOF){
		tot=Root=0;
		memset(head,-1,sizeof(head));
		memset(vis,false,sizeof(vis));
		for(int i=1;i<=N;i++)
			V[i]=read();
		for(int i=1;i<N;i++){
			int u=read();
			int v=read();
			addEdge(u,v);
			addEdge(v,u);
		}
		wt[0]=INF;
		Tsize=N; 
		ans_x=ans_y=N+1;
		getRoot(1,0);
		DFS(Root);
		if(ans_x>N)
			printf("No solution\n");
		else
			printf("%d %d\n",ans_x,ans_y);
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章