hdu 4812 D Tree(树上点分治)

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值