hdu4812 D Tree
地址: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?
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.
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.
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 }
作者:weeping
出处:www.cnblogs.com/weeping/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。