首页 > 其他 > 详细

HDOJ 4812 D Tree

时间:2018-01-19 21:18:45      阅读:213      评论:0      收藏:0      [点我收藏+]
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

原文:https://www.cnblogs.com/JYYHH/p/8318773.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!