HDOJ 4812 D Tree
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
Hint
1. “please print the lexicographically smallest one.”是指: 先按照第一个数字的大小进行比较,若第一个数字大小相同,则按照第二个数字大小进行比较,依次类推。 2. 若出现栈溢出,推荐使用C++语言提交,并通过以下方式扩栈:
#pragma comment(linker,"/STACK:102400000,102400000") 点分治的模板题,记录一下一些信息就行了。本题因为是路径上的点权之积而不是边权之积,所以切记
不要漏了点分的根的权值或者把它乘了两次。
(感觉我点分的模板好垃圾啊,每次都得写好久,还容易写错hhh)
#include<bits/stdc++.h>
#define ll long long
#define maxn 100005
#define ha 1000003
using namespace std;
int to[maxn*],ne[maxn*],num;
int hd[maxn],n,m,pt1,pt2,siz[maxn];
int ni[ha+],now[ha+],val[maxn];
int mn,sz,root;
ll k;
bool done[maxn]; inline void init(){
ni[]=;
for(int i=;i<ha;i++) ni[i]=-ni[ha%i]*(ll)(ha/i)%ha+ha;
} int find_siz(int x,int fa){
int an=;
for(int i=hd[x];i;i=ne[i]) if(!done[to[i]]&&to[i]!=fa) an+=find_siz(to[i],x);
return an;
} void find_root(int x,int fa){
siz[x]=;
int bal=;
for(int i=hd[x];i;i=ne[i]) if(!done[to[i]]&&to[i]!=fa){
find_root(to[i],x);
siz[x]+=siz[to[i]];
bal=max(bal,siz[to[i]]);
}
bal=max(bal,sz-siz[x]);
if(bal<mn) mn=bal,root=x;
} int dis[maxn],tt,loc[maxn]; void get_deep(int x,int fa,ll dd){
dis[++tt]=dd,loc[tt]=x;
int hh=k*ni[dd]%ha,a1=now[hh],a2=x;
if(a1>a2) swap(a1,a2);
if(a2<=n){
if(a1<pt1) pt1=a1,pt2=a2;
else if(a1==pt1&&a2<pt2) pt2=a2;
}
for(int i=hd[x];i;i=ne[i]) if(!done[to[i]]&&to[i]!=fa){
get_deep(to[i],x,dd*(ll)val[to[i]]%ha);
}
} inline void calc(int pos){
int pre=tt;
get_deep(pos,pos,val[pos]);
for(int i=pre+;i<=tt;i++) now[dis[i]]=min(now[dis[i]],loc[i]);
} inline void work(int tree,int trsiz){
mn=<<,sz=trsiz,root=;
find_root(tree,tree);
done[root]=;
k=k*ni[val[root]]%ha; dis[tt=]=;
now[]=root;
for(int i=hd[root];i;i=ne[i]) if(!done[to[i]]){
calc(to[i]);
} for(int i=;i<=tt;i++) now[dis[i]]=;
k=k*val[root]%ha; for(int i=hd[root];i;i=ne[i]) if(!done[to[i]]){
work(to[i],find_siz(to[i],to[i]));
}
} int main(){
init();
memset(now,0x3f,sizeof(now)); while(scanf("%d%lld",&n,&k)==){
memset(done,,sizeof(done));
memset(hd,,sizeof(hd)),num=; pt1=pt2=<<;
for(int i=;i<=n;i++) scanf("%d",val+i);
int uu,vv;
for(int i=;i<n;i++){
scanf("%d%d",&uu,&vv);
to[++num]=vv,ne[num]=hd[uu],hd[uu]=num;
to[++num]=uu,ne[num]=hd[vv],hd[vv]=num;
} work(,n); if(pt1>n) puts("No solution");
else printf("%d %d\n",pt1,pt2);
} return ;
}
HDOJ 4812 D Tree的更多相关文章
- hdoj 4925 Apple tree 【最小割】
题目:pid=4925">hdoj 4925 Apple tree 来源:2014 Multi-University Training Contest 6 题意:给出一个矩阵,然后每一 ...
- HDU 4812 D Tree 树分治+逆元处理
D Tree Problem Description There is a skyscraping tree standing on the playground of Nanjing Unive ...
- hdoj 4786 Fibonacci Tree【并查集+最小生成树(kruskal算法)】
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 4812 D Tree(树的点分治)
D Tree Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others) Total ...
- HDU 4812 D Tree
HDU 4812 思路: 点分治 先预处理好1e6 + 3以内到逆元 然后用map 映射以分治点为起点的链的值a 成他的下标 u 然后暴力跑出以分治点儿子为起点的链的值b,然后在map里查找inv[b ...
- HDU - 4812 D Tree 点分治
http://acm.hdu.edu.cn/showproblem.php?pid=4812 题意:有一棵树,每个点有一个权值要求找最小的一对点,路径上的乘积mod1e6+3为k 题解:点分治,挨个把 ...
- HDU 4812 D Tree 树分区+逆+hash新位置
意甲冠军: 特定n点树 K 以下n号码是正确的点 以下n-1行给出了树的侧. 问: 所以,如果有在正确的道路点图的路径 % mod = K 如果输出路径的两端存在. 多条路径则输出字典序最小的一条. ...
- HDU 4812 D Tree 树分治
题意: 给出一棵树,每个节点上有个权值.要找到一对字典序最小的点对\((u, v)(u < v)\),使得路径\(u \to v\)上所有节点权值的乘积模\(10^6 + 3\)的值为\(k\) ...
- HDU 4871 Shortest-path tree 最短路 + 树分治
题意: 输入一个带权的无向连通图 定义以顶点\(u\)为根的最短路生成树为: 树上任何点\(v\)到\(u\)的距离都是原图最短的,如果有多条最短路,取字典序最小的那条. 然后询问生成树上恰好包含\( ...
随机推荐
- Codeforces Round #534 (Div. 2) D. Game with modulo(取余性质+二分)
D. Game with modulo 题目链接:https://codeforces.com/contest/1104/problem/D 题意: 这题是一个交互题,首先一开始会有一个数a,你最终的 ...
- yaml语法
http://blog.csdn.net/mack415858775/article/details/51015662 name: Tom Smith age: 37 spouse: name: Ja ...
- region xx not deployed on any region server
ERROR: Region { meta => month_hotstatic,860010-2288000000_201405_5_exit_00000047486,1400144486405 ...
- DIV的变高与变宽
代码: <!DOCTYPE HTML><html><head> <meta charset="utf-8"> <title&g ...
- Type of flip id
http://www.haskell.org/pipermail/beginners/2011-March/006477.html The point is that the type of id h ...
- 单表扫描,MySQL索引选择不正确 并 详细解析OPTIMIZER_TRACE格式
一 表结构如下: 万行 CREATE TABLE t_audit_operate_log ( Fid bigint(16) AUTO_INCREMENT, Fcreate_time int(10 ...
- ZigBee PHY层
1. 介绍 ZigBee PHY层,即IEEE 802.15.4 PHY层,这里主要介绍了802.15.4-2003版本 PHY主要实现了如下功能 - 启动和关闭RF收发器 - 信道能量检测(Chan ...
- python基础===修改idle的输入风格
http://blog.csdn.net/aq_cainiao_aq/article/details/51701861
- BZOJ 1800
1800: [Ahoi2009]fly 飞行棋 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1622 Solved: 1293[Submit][St ...
- Eclipse SVN还原文件到历史版本详解
由于某些特殊原因,我们可能需要将SVN资源库中的某个文件回滚到以前的某个历史版本(准确地说,这不是"回滚","回滚"操作会导致指定版本到当前版本的变更记录丢失, ...