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\)的距离都是原图最短的,如果有多条最短路,取字典序最小的那条. 然后询问生成树上恰好包含\( ...
随机推荐
- 【NOIP模拟赛】就 反悔贪心
biubiu~~~ 这道题,考场上上来就dp然后发现怎么优化也不行.............最后发现是贪心............. 正解:带反悔的贪心,原理是,假设我们现在得到了取i个的最优解那么我 ...
- git使用笔记(十)杂项
By francis_hao Nov 27,2016 删除不被版本控制的文件 删除没有被git追踪的文件,当然,被ignore的文件不会被删除. git clean -f 查看文件的每一 ...
- 构建一个类jq的函数库
jqfree core var $ = function(selector, context) { return new $.fn.init(selector, context); }; $.fn = ...
- ecplise中修改reviewboard密码
一.概述 如果想在ecplise中修改reviewboard密码,步骤请参考如下图片:
- mybatis注解动态sql
@Insert("INSERT INTO user (name, age, gender, experience) VALUES (<a href="http://www.o ...
- 自定义UINavigationController push和pop动画
http://segmentfault.com/q/1010000000143983 默认的UINavigationController push和pop的默认动画都是左右滑动推出,我的应用要求这种界 ...
- Launcher3无图标问题
MTK8382/8121平台. 机器(8寸,默认竖屏)第一次烧录完成后,以横放姿势启动,发现Launcher没有图标,而竖屏启动是没有这个问题的.在测试过程中发现,在设置中clear data后也会有 ...
- Java坦克大战 (四) 之子弹的产生
本文来自:小易博客专栏.转载请注明出处:http://blog.csdn.net/oldinaction 在此小易将坦克大战这个项目分为几个版本,以此对J2SE的知识进行回顾和总结,希望这样也能给刚学 ...
- HTML5-坦克大战一画出敌人坦克并让自己的坦克可以发子弹的功能(二)
上一篇博客只画出了,一个坦克,并让其可以上下左右移动,这篇博客将画出敌人的坦克,并让自己的坦克可以发子弹,但是还不是尽善尽美,还有一些问题,将会在下篇博客说明: html代码: <!DOCTYP ...
- java Class.forName()
Java程序在运行时,Java运行时系统一直对所有的对象进行所谓的运行时类型标识.这项信息纪录了每个对象所属的类. 虚拟机通常使用运行时类型信息选准正确方法去执行,用来保存这些类型信息的类是Class ...