hdu4812(树分治)

来源:互联网 发布:电脑防辐射软件 编辑:程序博客网 时间:2024/06/05 07:50

D Tree

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 1154    Accepted Submission(s): 173


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.
 

Sample Input
5 602 5 2 3 31 21 32 42 55 22 5 2 3 31 21 32 42 5
 

Sample Output
3 4No solution

题意:给一棵带点权的树,求一条点权的乘积等于K的路径,输出满足路径的最小的两个端点

思路:这题真心只是一道很水的树分治,可能自己代码能力不够,比赛的时候无限TLE,最后改成了预处理出所有的逆元数组就过了,不过这题时间卡的真心紧

思路很简单,每次找到树的重心,然后以重心为中心对子树进行遍历求解即可,注意保存当前的每个值所对应的最优的点的标号,然后边找边判断即可,sad~~~

0 0
原创粉丝点击