1. 程式人生 > >hdu4812 D Tree

hdu4812 D Tree

mis sci 處理 info between name ger scanf vertex

地址:http://acm.hdu.edu.cn/showproblem.php?pid=4812

題目:

D Tree

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


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 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") 思路:   樹分治,然後On記錄答案。   註意下以下幾點:   1.不要每次求 ax Ξ k (mod p),p=1e6+3 中x的值。要預處理,並且並不是預處理ax Ξ k (mod p)的結果,而是記錄 ax ≡ 1 (mod p)的結果,即a的逆元。     然後根據   ax ≡ k (mod p).           ay*x ≡ 1 (mod p).(y是k的逆元)
    這樣只需要預處理【1,p)內的數的逆元即可。   2.不要用map等記錄值為x的最小節點號,可能T。     用int數組記錄即可,然後增加一次dfs清楚hash值。   3.路徑的端點可能是父親節點,不一定是要跨父親節點。   4.兩個p內的數相乘會爆int。   5.常數寫小點,可能T。   6.。。。沒有6了
  1 #include <cstdio>
  2 #include <algorithm>
  3 #include <vector>
  4 #include <cmath>
  5 #include <cstring>
  6 using namespace std;
  7 
  8 #define MP make_pair
  9 #define PB push_back
 10 typedef long long LL;
 11 typedef pair<int,int> PII;
 12 const double eps=1e-8;
 13 const double pi=acos(-1.0);
 14 const int K=1e6+7;
 15 const int mod=1e6+3;
 16 
 17 PII ans,mx;
 18 vector<int >mp[K];
 19 int cnt,tk,vis[K],dis[K],tdis[K];
 20 int fd[K],v[K],hs[K];
 21 int qpow(int x,int y)
 22 {
 23     int ret=1;
 24     for(;y;y>>=1,x=((LL)x*x)%mod)
 25     if(y&1) ret=((LL)ret*x)%mod;
 26     return ret;
 27 }
 28 void pre()
 29 {
 30     for(int i=1;i<mod;i++)
 31         fd[i]=qpow(i,mod-2);
 32 }
 33 struct CenterTree
 34 {
 35     int n,ret,mx,son[K];
 36     void dfs(int x,int f)
 37     {
 38         son[x]=1;
 39         int tmp=0;
 40         for(int i=0;i<mp[x].size();i++)
 41         if(f!=mp[x][i] && !vis[mp[x][i]])
 42         {
 43             dfs(mp[x][i],x);
 44             son[x]+=son[mp[x][i]];
 45             tmp=max(tmp,son[mp[x][i]]);
 46         }
 47         tmp=max(tmp,n-son[x]);
 48         if(tmp<mx)
 49             mx=tmp,ret=x;
 50     }
 51     int getCenter(int x,int num)
 52     {
 53         n=num,mx=0x3f3f3f3f;
 54         dfs(x,0);
 55         return ret;
 56     }
 57 }ct;
 58 void sc(int x,int f,LL td)
 59 {
 60     dis[++cnt]=td,tdis[cnt]=x;
 61     for(int i=0;i<mp[x].size();i++)
 62     if(!vis[mp[x][i]] && f!=mp[x][i])
 63         sc(mp[x][i],x,(td*v[mp[x][i]])%mod);
 64 }
 65 void calc(int x)
 66 {
 67     hs[v[x]]=x;
 68     for(int i=0;i<mp[x].size();i++)
 69     if(!vis[mp[x][i]])
 70     {
 71         cnt=0;
 72         sc(mp[x][i],x,v[mp[x][i]]);
 73         for(int j=1;j<=cnt;j++)
 74         {
 75             int tx=fd[((LL)dis[j]*fd[tk])%mod];
 76             if(hs[tx])
 77                 ans=min(ans,MP(min(hs[tx],tdis[j]),max(hs[tx],tdis[j])));
 78         }
 79         for(int j=1;j<=cnt;j++)
 80         {
 81             int tx=((LL)dis[j]*v[x])%mod;
 82             if(!hs[tx]||(hs[tx]!=0&&hs[tx]>tdis[j]))
 83             hs[tx]=tdis[j];
 84         }
 85 
 86     }
 87     hs[v[x]]=0;
 88     for(int i=0;i<mp[x].size();i++)
 89     if(!vis[mp[x][i]])
 90     {
 91         cnt=0;
 92         sc(mp[x][i],x,v[mp[x][i]]);
 93         for(int j=1;j<=cnt;j++)
 94             hs[((LL)dis[j]*v[x])%mod]=0;
 95     }
 96 }
 97 void solve(int x)
 98 {
 99     vis[x]=1;
100     calc(x);
101     for(int i=0;i<mp[x].size();i++)
102     if(!vis[mp[x][i]])
103         solve(ct.getCenter(mp[x][i],ct.son[mp[x][i]]));
104 }
105 int main(void)
106 {
107     int n;
108     pre();
109     //freopen("in.acm","r",stdin);
110     mx=MP(0x3f3f3f3f,0x3f3f3f3f);
111     while(~scanf("%d%d",&n,&tk))
112     {
113         for(int i=1;i<=n;i++)
114             scanf("%d",v+i),vis[i]=0,mp[i].clear();
115         for(int i=1,x,y;i<n;i++)
116             scanf("%d%d",&x,&y),mp[x].PB(y),mp[y].PB(x);
117         ans=mx;
118         solve(ct.getCenter(1,n));
119         if(ans!=mx)
120             printf("%d %d\n",ans.first,ans.second);
121         else
122             printf("No solution\n");
123     }
124     return 0;
125 }

hdu4812 D Tree