## HDU4812 D-tree

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 10 6 + 3) equals to K?
Can you help them in solving this problem?

InputThere are several test cases, please process till EOF.
Each test case starts with a line containing two integers N(1 <= N <= 10 5) and K(0 <=K < 10 6 + 3). The following line contains n numbers v i(1 <= v i < 10 6 + 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.OutputFor 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.
```5 60
2 5 2 3 3
1 2
1 3
2 4
2 5
5 2
2 5 2 3 3
1 2
1 3
2 4
2 5```
Sample Output
```3 4
No solution

```
Hint
```1. “please print the lexicographically smallest one.”是指: 先按照第一个数字的大小进行比较，若第一个数字大小相同，则按照第二个数字大小进行比较，依次类推。

2. 若出现栈溢出，推荐使用C++语言提交，并通过以下方式扩栈：

```#include<bits/stdc++.h>
#define inf 1000000000
#define P 1000003
#define ll long long
using namespace std;
{
int x=0;char ch=getchar();
while(ch<'0'||ch>'9')ch=getchar();
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x;
}
int n,K,cnt,rt,sum,top;
int id[100005],f[100005],size[100005],last[100005];
int ans1,ans2;
ll tmp[100005],val[100005],dis[100005];
ll ine[1000005],mp[1000005];
bool vis[100005];
struct edge{
int to,next;
}e[200005];

void pre()//预处理逆元
{
ine[1]=1;
for(int i=2;i<P;i++)
{
int a=P/i,b=P%i;
ine[i]=(ine[b]*(-a)%P+P)%P;
}
}

void insert(int u,int v)
{
e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt;
e[++cnt].to=u;e[cnt].next=last[v];last[v]=cnt;
}

void getrt(int x,int fa)//找重心
{
f[x]=0;size[x]=1;
for(int i=last[x];i;i=e[i].next)
{
if(!vis[e[i].to]&&e[i].to!=fa)
{
getrt(e[i].to,x);
size[x]+=size[e[i].to];
f[x]=max(f[x],size[e[i].to]);
}
}
f[x]=max(f[x],sum-size[x]);
if(f[x]<f[rt])rt=x;
}

void dfs(int x,int fa)
{
tmp[++top]=dis[x];id[top]=x;
for(int i=last[x];i;i=e[i].next)
{
if(!vis[e[i].to]&&e[i].to!=fa)
{
dis[e[i].to]=(dis[x]*val[e[i].to])%P;
dfs(e[i].to,x);
}
}
}

void query(int x,int id)
{
x=ine[x]*K%P;
int y=mp[x];
if(y==0) return;
if(y>id) swap(y,id);
if(y<ans1||(y==ans1&&id<ans2)) ans1=y,ans2=id;
}

void solve(int x,int S)
{
vis[x]=1;
mp[val[x]]=x;
for(int i=last[x];i;i=e[i].next)
{
if(!vis[e[i].to])
{
top=0;
dis[e[i].to]=val[e[i].to];
dfs(e[i].to,x);
for(int j=1;j<=top;j++) query(tmp[j],id[j]);
top=0;
dis[e[i].to]=(val[x]*val[e[i].to])%P;
dfs(e[i].to,x);

for(int j=1;j<=top;j++)
{
int now=mp[tmp[j]];
if(!now||id[j]<now) mp[tmp[j]]=id[j];
}
}
}

mp[val[x]]=0;
for(int i=last[x];i;i=e[i].next)
{
if(!vis[e[i].to])
{
top=0;
dis[e[i].to]=(val[x]*val[e[i].to])%P;
dfs(e[i].to,x);
for(int j=1;j<=top;j++) mp[tmp[j]]=0;//清空
}
}

for(int i=last[x];i;i=e[i].next)
{
if(!vis[e[i].to])
{
rt=0;
sum=size[e[i].to];
if(size[e[i].to]>size[x]) sum=S-size[x];
getrt(e[i].to,0);
solve(rt,sum);
}
}
}

int main()
{
pre();
while(scanf("%d%d",&n,&K)!=EOF)
{
memset(vis,0,sizeof(vis));
cnt=0;ans1=ans2=inf;
memset(last,0,sizeof(last));
for(int i=1;i<n;i++)
{
insert(u,v);
}
rt=0;sum=n;f[0]=n+1;
getrt(1,0);
solve(rt,sum);
if(ans1==inf) puts("No solution");
else printf("%d %d\n",ans1,ans2);
}
return 0;
}```
View Code

0条评论