1. 程式人生 > >HDOJ 4812 D Tree

HDOJ 4812 D Tree

ace ply output ems 路徑 roc cas sts clu

Discription 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?

Input

There 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.

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.

Sample Input

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++語言提交,並通過以下方式擴棧:
#pragma comment(linker,"/STACK:102400000,102400000")



點分治的模板題,記錄一下一些信息就行了。本題因為是路徑上的點權之積而不是邊權之積,所以切記
不要漏了點分的根的權值或者把它乘了兩次。
(感覺我點分的模板好垃圾啊,每次都得寫好久,還容易寫錯hhh)

#include<bits/stdc++.h>
#define ll long long
#define maxn 100005
#define ha 1000003
using namespace std;
int to[maxn*2],ne[maxn*2],num;
int hd[maxn],n,m,pt1,pt2,siz[maxn];
int ni[ha+5],now[ha+5],val[maxn];
int mn,sz,root;
ll k; 
bool done[maxn];

inline void init(){
    ni[1]=1;
    for(int i=2;i<ha;i++) ni[i]=-ni[ha%i]*(ll)(ha/i)%ha+ha;
}

int find_siz(int x,int fa){
    int an=1;
    for(int i=hd[x];i;i=ne[i]) if(!done[to[i]]&&to[i]!=fa) an+=find_siz(to[i],x);
    return an;
}

void find_root(int x,int fa){
    siz[x]=1;
    int bal=0;
    for(int i=hd[x];i;i=ne[i]) if(!done[to[i]]&&to[i]!=fa){
        find_root(to[i],x);
        siz[x]+=siz[to[i]];
        bal=max(bal,siz[to[i]]);
    }
    bal=max(bal,sz-siz[x]);
    if(bal<mn) mn=bal,root=x;
}

int dis[maxn],tt,loc[maxn];

void get_deep(int x,int fa,ll dd){
    dis[++tt]=dd,loc[tt]=x;
    int hh=k*ni[dd]%ha,a1=now[hh],a2=x;
    if(a1>a2) swap(a1,a2);
    if(a2<=n){
        if(a1<pt1) pt1=a1,pt2=a2;
        else if(a1==pt1&&a2<pt2) pt2=a2;
    }
    for(int i=hd[x];i;i=ne[i]) if(!done[to[i]]&&to[i]!=fa){
        get_deep(to[i],x,dd*(ll)val[to[i]]%ha);
    }
}

inline void calc(int pos){
    int pre=tt;
    get_deep(pos,pos,val[pos]);
    for(int i=pre+1;i<=tt;i++) now[dis[i]]=min(now[dis[i]],loc[i]);
}

inline void work(int tree,int trsiz){
    mn=1<<29,sz=trsiz,root=0;
    find_root(tree,tree);
    done[root]=1;
    k=k*ni[val[root]]%ha;
    
    dis[tt=1]=1;
    now[1]=root;
    for(int i=hd[root];i;i=ne[i]) if(!done[to[i]]){
        calc(to[i]);
    }
    
    for(int i=1;i<=tt;i++) now[dis[i]]=100000000;
    k=k*val[root]%ha;
    
    for(int i=hd[root];i;i=ne[i]) if(!done[to[i]]){
        work(to[i],find_siz(to[i],to[i]));
    }
}

int main(){
    init();
    memset(now,0x3f,sizeof(now));
    
    while(scanf("%d%lld",&n,&k)==2){
        memset(done,0,sizeof(done));
        memset(hd,0,sizeof(hd)),num=0;
        
        pt1=pt2=1<<29;
        for(int i=1;i<=n;i++) scanf("%d",val+i);
        int uu,vv;
        for(int i=1;i<n;i++){
            scanf("%d%d",&uu,&vv);
            to[++num]=vv,ne[num]=hd[uu],hd[uu]=num;
            to[++num]=uu,ne[num]=hd[vv],hd[vv]=num;
        }
        
        work(1,n);
        
        if(pt1>n) puts("No solution");
        else printf("%d %d\n",pt1,pt2);
    }
    
    return 0;
}


HDOJ 4812 D Tree