hdu4812(树分治)

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 10 6 + 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 <= 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.
 

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

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

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

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

#pragma comment(linker,"/STACK:102400000,102400000")
#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         using namespace std; #define maxn 100010 #define mod 1000003 #define INF ((1<<31)-1) typedef long long ll; typedef pair 
        
          pii; vector 
         
           vect; int cnum[maxn]; int num[maxn]; int center; int root; int cu[1000010]; int head[maxn]; int edge[maxn<<1]; int next[maxn<<1]; int vex[2]; int hhh[1000010]; int val[maxn]; ll ww[maxn]; ll inv[1000010]; int d; int ans; int n,k; int maxi(int x,int y) { return x>y?x:y; } void dfs(int u,int pre,int total) { int i; num[u]=1; cnum[u]=0; for(i=head[u];i!=-1;i=next[i])if(edge[i]!=pre&&!val[edge[i]]) { dfs(edge[i],u,total); num[u]+=num[edge[i]]; if(num[edge[i]]>cnum[u])cnum[u]=num[edge[i]]; } int temp=maxi(total-num[u],cnum[u]); if(temp 
          
            v){ temp=u; u=v; v=temp; } if(u 
           
             vv.first){ cu[vv.second]=vv.first; hhh[dd++]=vv.second; } } vect.clear(); } for(i=0;i 
             
            
           
          
         
       
      
      
     
     
    
    
   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值