D Tree
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 6331 Accepted Submission(s): 1347
Problem Description
There is a skyscraping tree standing on the playground of Nanjing University of Science and Technology. On each branch of the tree is an integer (The tree can be treated as a connected graph with N vertices, while each branch can be treated as a vertex). Today the students under the tree are considering a problem: Can we find such a chain on the tree so that the multiplication of all integers on the chain (mod 106 + 3) equals to K?
Can you help them in solving this problem?
Input
There are several test cases, please process till EOF.
Each test case starts with a line containing two integers N(1 <= N <= 105) and K(0 <=K < 106 + 3). The following line contains n numbers vi(1 <= vi < 106 + 3), where vi indicates the integer on vertex i. Then follows N - 1 lines. Each line contains two integers x and y, representing an undirected edge between vertex x and vertex y.
Output
For each test case, print a single line containing two integers a and b (where a < b), representing the two endpoints of the chain. If multiply solutions exist, please print the lexicographically smallest one. In case no solution exists, print “No solution”(without quotes) instead.
For more information, please refer to the Sample Output below.
思路:
ai*aj%mod=k%mod 即a[i]=k*inv[a[j]]%mod。
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod=1e6+3;
const int maxn=1e6+10;
ll a[maxn],n,k,val[maxn],inv[maxn];
struct node
{
int to,next;
}G[maxn];
int root,now_size,son[maxn],cnt,id[maxn];
int ansx,ansy,head[maxn],tol,mp[maxn];
bool vis[maxn];
void add_edge(int a,int b)
{
G[tol].to=b;
G[tol].next=head[a];
head[a]=tol++;
}
void get_root(int v,int fa,int SIZE)
{
son[v]=1;
int ma=0;
for(int i=head[v];i!=-1;i=G[i].next)
{
int to=G[i].to;
if(to==fa||vis[to]) continue;
get_root(to,v,SIZE);
son[v]+=son[to];
ma=max(ma,son[to]);
}
ma=max(ma,SIZE-son[v]);
if(ma<now_size)
{
now_size=ma;
root=v;
}
}
void update(int v,int fa,ll s)
{
val[cnt]=s;id[cnt++]=v;
for(int i=head[v];i!=-1;i=G[i].next)
{
int to=G[i].to;
if(to==fa||vis[to]) continue;
update(to,v,s*a[to]%mod);
}
}
void check(ll s,int y)
{
int x=mp[k*inv[s]%mod];
if(x!=y&&x)
{
if(x>y) swap(x,y);
if(x<ansx||(x==ansx&&y<ansy))
ansx=x,ansy=y;
}
}
void dfs(int v,int fa)
{
vis[v]=1;
mp[a[v]]=v;
for(int i=head[v];i!=-1;i=G[i].next)
{
int to=G[i].to;
if(to==fa||vis[to]) continue;
cnt=0;
update(to,v,a[to]%mod);
for(int j=0;j<cnt;j++)
check(val[j]%mod,id[j]);
for(int j=0;j<cnt;j++)
{
val[j]=val[j]*a[v]%mod;
if(!mp[val[j]]) mp[val[j]]=id[j];
else mp[val[j]]=min(mp[val[j]],id[j]);
}
}
mp[a[v]]=0;
for(int i=head[v];i!=-1;i=G[i].next)
{
int to=G[i].to;
if(to==fa||vis[to]) continue;
cnt=0;
update(to,v,a[to]%mod);
for(int j=0;j<cnt;j++)
{
val[j]=val[j]*a[v]%mod;
mp[val[j]]=0;
}
}
for(int i=head[v];i!=-1;i=G[i].next)
{
int to=G[i].to;
if(to==fa||vis[to]) continue;
now_size=1e9;
get_root(root=to,v,son[to]);
dfs(root,0);
}
}
int main()
{
inv[1] = 1;
for (ll i=2;i<=mod+5;i++)
inv[i]=(mod-mod/i)*inv[mod%i]%mod;
while(~scanf("%lld%lld",&n,&k))
{
ansx=ansy=1e9;tol=0;
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
for(int i=1;i<n;i++)
{
int x,y;scanf("%d%d",&x,&y);
add_edge(x,y);add_edge(y,x);
}
now_size=1e9;
get_root(root=1,0,n);
dfs(root,0);
if(ansx==1e9) printf("No solution\n");
else printf("%d %d\n",ansx,ansy);
}
return 0;
}
本文介绍了一种利用树形动态规划方法解决特定路径乘积问题的算法。问题要求在给定的树状结构中寻找一条路径,使得路径上节点数值的乘积模特定值等于给定的目标值。通过递归分解和状态更新实现了高效的解决方案。
4万+

被折叠的 条评论
为什么被折叠?



