傳送門: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;
}