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\)的距离都是原图最短的,如果有多条最短路,取字典序最小的那条. 然后询问生成树上恰好包含\( ...
随机推荐
- 使用mysqldump命令备份恢复MySQL数据库
1.各种用法说明 A. 最简单的用法: mysqldump -uroot -pPassword [database name] > [dump file] 上述命令将指定数据库备份到某dump文 ...
- SLF4J 与Log4J
为什么要使用SLF4J而不是Log4J 每一个Java程序员都知道日志对于任何一个Java应用程序,尤其是服务端程序是至关重要的,而很多程序员也已经熟悉各种不同的日志库如java.util.loggi ...
- 设备VMnet0上的网络桥接当前未在运行解决办法
问题: 今天把自己的VM从C盘挪到了D盘,然后再open所有VM都会显示网卡无法桥接了 “vmware 没有未桥接的主机网络适配器” 解决办法: 1.关闭所有VM 2.打开 编辑-虚拟网络编辑器,会发 ...
- Python爬虫学习笔记之抓取猫眼的排行榜
代码: import json import requests from requests.exceptions import RequestException import re import ti ...
- 创建Maven项目出现:An internal error occurred during: "Retrieving archetypes:". Java heap space 错误解决办法
首先说明一下网上的方法: 在Eclipse中创建Maven的Web项目时出现错误:An internal error occurred during: "Retrieving archety ...
- JRE集成到Tomcat
将jdk集成到tomcat里面(不用客户安装JRE) 或者 tomcat使用指定的jdk_ 给客户安装软件的时候,也许客户不想你在人家机器的环境变量里设置来设置去,那么就要在tomcat里指定要使用的 ...
- 【Foreign】数数 [打表][DP]
数数 Time Limit: 10 Sec Memory Limit: 128 MB Description Input 仅一行两个整数L,R Output 仅一行一个整数表示答案. Sample ...
- unity中绘制战争迷雾
接上一篇中说的游戏,我们已经实现了client.host上的一个物体可见不可见的行为.之后我们可以加入类似检查两个单位之间的距离.或是两个单位之间有无阻挡物来进一步实现游戏机制. 在这篇随笔中我会首先 ...
- 【洛谷 P1667】 数列 (贪心)
题目链接 对于一个区间\([x,y]\),设这个区间的总和为\(S\) 那么我们在前缀和(设为\(sum[i]\))的意义上考虑到原操作其实就是\(sum[x−1]+=S\) , \(sum[x]+S ...
- Nginx的主要配置参数说明
#定义Nginx运行的用户和用户组user www www; #nginx进程数,建议设置为等于CPU总核心数.worker_processes 8; #全局错误日志定义类型,[ debug | in ...