zoukankan      html  css  js  c++  java
  • HDU 4812 D Tree

    D Tree

    Time Limit: 5000ms
    Memory Limit: 102400KB
    This problem will be judged on HDU. Original ID: 4812
    64-bit integer IO format: %I64d      Java class name: Main
    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 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")

    Source

     
    解题:树分治点分治
      1 #include <bits/stdc++.h>
      2 #define A first
      3 #define B second
      4 using namespace std;
      5 using PII = pair<int,int>;
      6 using LL = long long;
      7 const int B = 100000;
      8 const int maxn = 100010;
      9 const int mod = 1e6 + 3;
     10 const PII INF = PII(~0U>>2,~0U>>2);
     11 struct arc {
     12     int to,next;
     13     arc(int x = 0,int y = -1) {
     14         to = x;
     15         next = y;
     16     }
     17 } e[maxn<<1];
     18 bool vis[maxn];
     19 LL inv[1000010],val[maxn],F[1000010],K,P;
     20 PII ans;
     21 int head[maxn],sz[maxn],maxson[maxn],tot,n;
     22 void init() {
     23     inv[1] = 1;
     24     for(int i = 2; i < mod; ++i)
     25         inv[i] = (mod - mod/i)*inv[mod%i]%mod;
     26 }
     27 void add(int u,int v) {
     28     e[tot] = arc(v,head[u]);
     29     head[u] = tot++;
     30 }
     31 void dfs(int u,int fa) {
     32     sz[u] = 1;
     33     maxson[u] = 0;
     34     for(int i = head[u]; ~i; i = e[i].next) {
     35         if(e[i].to == fa || vis[e[i].to]) continue;
     36         dfs(e[i].to,u);
     37         sz[u] += sz[e[i].to];
     38         maxson[u] = max(maxson[u],sz[e[i].to]);
     39     }
     40 }
     41 int FindRoot(int sum,int u,int fa) {
     42     int ret = u;
     43     maxson[u] = max(maxson[u],sum - sz[u]);
     44     for(int i = head[u]; ~i; i = e[i].next) {
     45         if(e[i].to == fa || vis[e[i].to]) continue;
     46         int x = FindRoot(sum,e[i].to,u);
     47         if(maxson[x] < maxson[ret]) ret = x;
     48     }
     49     return ret;
     50 }
     51 void calc(int u,int fa,LL d,int flag){
     52     d = d*val[u]%mod;
     53     if(flag){
     54         if(F[K*inv[d]%mod] > P){
     55             int x = F[K*inv[d]%mod] - P,y = u;
     56             if(x > y) swap(x,y);
     57             ans = min(ans,PII(x,y));
     58         }
     59     }else if(F[d] <= P) F[d] = P + u;
     60     else F[d] = min(F[d],P + u);
     61     for(int i = head[u]; ~i; i = e[i].next){
     62         if(e[i].to == fa || vis[e[i].to]) continue;
     63         calc(e[i].to,u,d,flag);
     64     }
     65 }
     66 void solve(int u) {
     67     dfs(u,0);
     68     int rt = FindRoot(sz[u],u,0);
     69     vis[rt] = true;
     70     P += B;
     71     F[val[rt]] = P + rt;
     72     for(int i = head[rt]; ~i; i = e[i].next){
     73         if(vis[e[i].to]) continue;
     74         calc(e[i].to,rt,1,1);
     75         calc(e[i].to,rt,val[rt],0);
     76     }
     77     for(int i = head[rt]; ~i; i = e[i].next)
     78         if(!vis[e[i].to]) solve(e[i].to);
     79 }
     80 int main() {
     81     init();
     82     int u,v;
     83     while(~scanf("%d%I64d",&n,&K)) {
     84         memset(head,-1,sizeof head);
     85         memset(vis,false,sizeof vis);
     86         tot = 0;
     87         for(int i = 1; i <= n; ++i)
     88             scanf("%I64d",val + i);
     89         for(int i = 1; i < n; ++i) {
     90             scanf("%d%d",&u,&v);
     91             add(u,v);
     92             add(v,u);
     93         }
     94         ans = INF;
     95         solve(1);
     96         if(ans == INF) puts("No solution");
     97         else printf("%d %d
    ",ans.A,ans.B);
     98     }
     99     return 0;
    100 }
    View Code
  • 相关阅读:
    大叔手记(9):小心使用IHttpHandler下的IsReusable属性
    探讨神奇的需求变更:程序员遭遇车祸成植物人,需求变更将其唤醒
    大型JavaScript应用程序架构模式
    HTML5学习笔记简明版(9):变化的元素和属性
    大叔手记(6):巧用ASP.net MVC3里的HandleErrorAttribute
    大叔手记(7):构建自己的JavaScript模板小引擎
    HTML5学习笔记简明版(10):废弃的元素和属性
    RX/TX/GND是什么
    光影中的十大非常学校
    把音量键改成back键。
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4907480.html
Copyright © 2011-2022 走看看