hdu 4812 D Tree(樹的點分治),hdu4812
D TreeTime Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 1687 Accepted Submission(s): 263
Problem DescriptionThere 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?
InputThere 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.
OutputFor 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 solutionHint1. “please print the lexicographically smallest one.”是指: 先按照第一個數位大小進行比較,若第一個數字大小相同,則按照第二個數字大小進行比較,依次類推。2. 若出現棧溢出,推薦使用C++語言提交,並通過以下方式擴棧:#pragma comment(linker,"/STACK:102400000,102400000")
Source2013ACM/ICPC亞洲區南京站現場賽——題目重現
Recommendliuyiding | We have carefully selected several similar problems for you: 5068 5067 5065 5064 5062 題意:給你一個n(1e5)個結點的數。每個結點有個權值v(1 <= vi < 106 + 3).問你是否存在一條路徑u->v。使得路徑上的結點的乘積為k(0 <=K < 106 + 3).有多解輸出字典序最小的解。思路:先找樹的重心。再按樹的重心分治。計運算元樹所有結點到分治重心路徑權值的乘積val。再查詢hash表看有沒有k*inv[val]存在。有就更新答案。分治完一顆子樹後再更新hash表。詳細見代碼:
#include<algorithm>#include<iostream>#include<string.h>#include<stdio.h>#pragma comment(linker,"/STACK:102400000,102400000")using namespace std;const int INF=0x3f3f3f3f;const int maxn=100010;const int mod=1e6+3;typedef long long ll;ll inv[mod+10];int val[maxn],ms[maxn],son[maxn],vis[maxn],cnt,kv;int mp[mod+10],use[mod+10],ptr,pos,ans1,ans2;struct node{ int v; node *next;} ed[maxn<<1],*head[maxn];void adde(int u,int v){ ed[cnt].v=v; ed[cnt].next=head[u]; head[u]=&ed[cnt++];}void dfs(int fa,int u){ son[u]=1; ms[u]=0; for(node *p=head[u];p!=NULL;p=p->next) { if(p->v==fa||vis[p->v]) continue; dfs(u,p->v); son[u]+=son[p->v]; ms[u]=max(ms[u],son[p->v]); }}void findroot(int fa,int u,int all){ ms[u]=max(ms[u],all-son[u]); if(ms[u]<ms[pos]) pos=u; for(node *p=head[u];p!=NULL;p=p->next) { if(p->v==fa||vis[p->v]) continue; findroot(u,p->v,all); }}void getroot(int u){ dfs(-1,u); pos=u; findroot(-1,u,son[u]);}void cal(int fa,int u,int mc)//計算答案{ mc=(ll)mc*val[u]%mod; int op=(ll)kv*inv[mc]%mod,a,b; if(mp[op]) { a=u,b=mp[op]; if(a>b) swap(a,b); if(a<ans1) ans1=a,ans2=b; else if(a==ans1&&b<ans2) ans2=b; } for(node *p=head[u];p!=NULL;p=p->next) { if(p->v==fa||vis[p->v]) continue; cal(u,p->v,mc); }}void update(int fa,int u,int mc)//更新hash表{ mc=(ll)mc*val[u]%mod; if(!mp[mc]) use[ptr++]=mc,mp[mc]=u; else mp[mc]=min(mp[mc],u); for(node *p=head[u];p!=NULL;p=p->next) { if(p->v==fa||vis[p->v]) continue; update(u,p->v,mc); }}void solve(int u)//每次處理一顆子樹{ int root,i; getroot(u); root=pos; mp[val[root]]=root; vis[root]=1,ptr=0; for(node *p=head[root];p!=NULL;p=p->next) { if(vis[p->v]) continue; cal(root,p->v,1);//計運算元樹路徑 update(root,p->v,val[root]); } for(i=0;i<ptr;i++) mp[use[i]]=0; mp[val[root]]=0; for(node *p=head[root];p!=NULL;p=p->next) { if(vis[p->v]) continue; solve(p->v); }}int main(){ int n,i,u,v; inv[1]=1; for(i=2;i<mod;i++)//預先處理逆元 inv[i]=(mod-mod/i)*inv[mod%i]%mod; while(~scanf("%d%d",&n,&kv)) { for(i=1;i<=n;i++) scanf("%d",&val[i]); memset(head,0,sizeof head); memset(vis,0,sizeof vis); cnt=0; for(i=1;i<n;i++) { scanf("%d%d",&u,&v); adde(u,v); adde(v,u); } ans1=ans2=INF; solve(1); if(ans1!=INF) printf("%d %d\n",ans1,ans2); else printf("No solution\n"); } return 0;}