zoukankan      html  css  js  c++  java
  • HDU4812 D-tree

    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
    ",ans1,ans2);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Json对象和Json字符串
    主界面的构造函数报错
    WPF DataGrid绑定的数据源更新后界面信息不更新
    WPF 数据绑定 ( 经验 )
    WPF 简单快速地去掉Button控件的边框
    字节数组与字符串形式的数字(序列号)之间的相互转换
    将十进制数字转换为字节数组
    WPF 调用线程无法访问此对象,因为另一个线程拥有该对象。
    cmd中一些命令
    Notepad++运行快捷键的设置
  • 原文地址:https://www.cnblogs.com/csushl/p/10923966.html
Copyright © 2011-2022 走看看