hdu4812 D Tree

题意:

给定一棵 n 个点的树,每个点有权值 Vi

问是否存在一条路径使得路径上所有点的权值乘积 mod(10^6 + 3) 为 K

输出路径的首尾标号,若有多解,输出字典序最小的解

思路:这里用了mark数组和tim数组来进行标记。

#pragma comment(linker,"/STACK:102400000,102400000")
#include
#include
#include
#include 
#include
#include 
using namespace std;
typedef long long LL;
const int maxn = 100005;
const int maxm = 200005;
const LL mod = 1000003;
int he[maxn],ne[maxm],ver[maxm],tot,tim;
LL inv[1000005];
void pre()
{
    inv[1]=1;
    for(int i=2;i<=mod;i++)
    {
        int a=mod/i,b=mod%i;
        inv[i]=(inv[b]*(-a)%mod+mod)%mod;
    }
}
void add( int x,int y ){
    ver[++tot] = y;
    ne[tot] = he[x];
    he[x] = tot;
}
int root,  // 当前树的重心
        vis[maxn], // 标记点是否作为重心出现过
        n,        // 树的总点数
        sz[maxn],
        g[maxn];
void get_sz( int x,int f ){
    sz[x] = 1;
    g[x] = 0;
    for( int cure = he[x];cure;cure = ne[cure] ){
        int y  = ver[cure];
        if( y == f || vis[y] ) continue;
        get_sz( y,x );
        g[x] = max( g[x],sz[y] );
        sz[x] += sz[y];
    }
}
int find_root( int x,int f ){

    int minx = n;
    int re = x;
    for( int cure = he[x];cure;cure = ne[cure] ){
        int y  = ver[cure];
        if( vis[y] || y == f ) continue;
        int tmp = find_root( y,x );
        if( g[tmp] < minx ){
            re = tmp,minx = g[tmp];
        }
    }
    g[x] = max( g[x],sz[root]-sz[x] );
    if( g[x] < minx ) re = x;
    return re;
}
void get_root( int& x ){
    get_sz( x,0 );
    root = x;
    x = find_root( x,0 );
    root = x;
}
int dis[1000005],mark[1000005],v[maxn],cas[1000005];
vector >ve2;
int K;
pair ans(1000000,1000000);
void dfs_dis( int x,int f,LL res ){
    res *= v[x]; res%= mod;
    ve2.push_back( make_pair( res,x ) );
    LL tmp = res * v[root]%mod;
    if( tmp == K ){
        int a = root,b = x;
        if( a > b ) swap(a,b);
        ans = min( ans,make_pair(a,b) );
    }
    LL cur = K * inv[tmp] %mod;
    if( mark[cur]  == root && cas[cur] == tim ){
        int a = dis[cur],b = x;
        if( a > b ) swap(a,b);
        ans = min( ans,make_pair(a,b) );
    }
    for( int cure = he[x];cure;cure = ne[cure] ){
        int y = ver[cure];
        if( vis[y] || y==f ) continue;
        dfs_dis( y,x,res );
    }
}
void dfs_div( int x ){
    if(vis[x]) return;
    get_root(x);
    vis[x] = 1;
    for( int cure = he[x];cure;cure = ne[cure] ){
        int y = ver[cure];
        if( vis[y] ) continue;
        dfs_dis( y,x,1 );
        for( int i = 0;i < ve2.size();i++ ){
            int x = ve2[i].first;
            if( mark[x] == root && cas[x] == tim )
                dis[x] = min( dis[x],ve2[i].second );
            else dis[x] = ve2[i].second;
            mark[x] = root;cas[x] = tim;
        }
        ve2.clear();
    }
    for( int cure = he[x];cure;cure = ne[cure] ){
        int y = ver[cure];
        if( vis[y] ) continue;
        dfs_div( y );
    }
}
void init(int n){
    memset( he,0,sizeof(int)*(n+1) );
    memset( vis,0,sizeof(int)*(n+1) );
    tot = 1;
}
int main(){
    pre();
    while( 2 == scanf("%d%d",&n,&K) ){
        init(n);tim++;
        for( int i = 1;i <= n;i++ ){
            scanf("%d",&v[i]);
        }
        ans = make_pair(1000000,1000000);
        int x,y;
        for( int i = 1;i < n;i++ ){
            scanf("%d%d",&x,&y);
            add(x,y);add(y,x);
        }
        dfs_div(1);
        if( ans.first == 1000000 ){
            puts("No solution");
        }else{

            printf("%d %d\n",ans.first,ans.second);
        }

    }
    return 0;
}

 

你可能感兴趣的:(点分治)