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
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(may) swap(x,y);
        if(x

 

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