ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

HDU4812 D-tree

2019-05-25 21:45:52  阅读:172  来源: 互联网

标签:integers ch HDU4812 tree please two print


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.
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")

        
题意:给出一棵树,让你寻找一条路径,使得路径上的点相乘mod10^6+3等于k,输出路径的两个端点,按照字典序最小输出。

思路:这类问题很容易想到树的分治,每次找出树的重心,以重心为根,将树分成若干棵子树,然后对于每棵子树再一样的操作,现在就需要求一重心为根,寻找路径,依次遍历每一个子树,然后记录子树中点到根的权值的乘积X,然后通过在哈希表中寻找K×逆元(x),看是否存在,存在则更新答案。

参考代码:
#include<bits/stdc++.h>
#define inf 1000000000
#define P 1000003
#define ll long long
using namespace std;
inline int read()
{
    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++) val[i]=read();
        for(int i=1;i<n;i++)
        {
            int u=read(),v=read();
            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

 

标签:integers,ch,HDU4812,tree,please,two,print
来源: https://www.cnblogs.com/songorz/p/10923966.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有