HDU4812
树分治
求逆元请递推,不然会TLE
开桶记录即可
注意常数
# pragma comment(linker,"/STACK:102400000,102400000")
# include <stdio.h>
# include <stdlib.h>
# include <iostream>
# include <algorithm>
# include <string.h>
# include <vector>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
IL ll Read(){
RG char c = getchar(); RG ll x = 0, z = 1;
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + c - '0';
return x * z;
}
const int MAXN(200010), INF(2147483647), P(1e6 + 3);
int n, cnt, fst[MAXN], to[MAXN], nxt[MAXN], w[MAXN], k, ansx, ansy;
int size[MAXN], rt, sz, mx[MAXN], vis[MAXN], inv[P], d[P], S[MAXN], top, T[P];
IL void Add(RG int u, RG int v){ nxt[cnt] = fst[u]; to[cnt] = v; fst[u] = cnt++; }
IL void Getroot(RG int u, RG int fa){
size[u] = 1; mx[u] = 0;
for(RG int e = fst[u]; e != -1; e = nxt[e]){
if(vis[to[e]] || to[e] == fa) continue;
Getroot(to[e], u);
size[u] += size[to[e]];
mx[u] = max(mx[u], size[to[e]]);
}
mx[u] = max(mx[u], sz - size[u]);
if(mx[u] < mx[rt]) rt = u;
}
IL void Getdeep(RG int u, RG int fa, RG int dis){
d[u] = dis; S[++top] = u;
for(RG int e = fst[u]; e != -1; e = nxt[e]){
if(vis[to[e]] || to[e] == fa) continue;
Getdeep(to[e], u, 1LL * dis * w[to[e]] % P);
}
}
IL void Solve(RG int u){
vis[u] = 1; T[1] = u;
for(RG int e = fst[u]; e != -1; e = nxt[e]){
if(vis[to[e]]) continue;
top = 0; Getdeep(to[e], u, w[to[e]]);
for(RG int i = 1; i <= top; i++){
RG int x = S[i], y = d[x], z = 0;
z = T[1LL * k * inv[1LL * y * w[u] % P] % P];
if(!z) continue;
if(x > z) swap(x, z);
if(x < ansx || (x == ansx && z < ansy)) ansx = x, ansy = z;
}
for(RG int i = 1; i <= top; i++) if(!T[d[S[i]]] || S[i] < T[d[S[i]]]) T[d[S[i]]] = S[i];
}
for(RG int e = fst[u]; e != -1; e = nxt[e]){
if(vis[to[e]]) continue;
top = 0; Getdeep(to[e], u, w[to[e]]);
for(RG int i = 1; i <= top; i++) T[d[S[i]]] = 0;
}
T[1] = 0;
for(RG int e = fst[u]; e != -1; e = nxt[e]){
if(vis[to[e]]) continue;
rt = 0; sz = size[to[e]];
Getroot(to[e], u);
Solve(rt);
}
}
int main(RG int argc, RG char* argv[]){
mx[0] = INF; inv[1] = 1;
for(RG int i = 2; i < P; i++) inv[i] = (1LL * (-(P / i)) * inv[P % i] % P + P) % P;
while(scanf("%d%d", &n, &k) != EOF){
Fill(fst, -1); Fill(vis, 0);
rt = cnt = 0; sz = n; ansx = ansy = n + 1;
for(RG int i = 1; i <= n; i++) w[i] = Read();
for(RG int i = 1, u, v; i < n; i++) u = Read(), v = Read(), Add(u, v), Add(v, u);
Getroot(1, 0); Solve(rt);
if(ansx > n) puts("No solution");
else printf("%d %d\n", ansx, ansy);
}
return 0;
}
HDU4812的更多相关文章
- HDU4812 D Tree(树的点分治)
题目大概说给一棵有点权的树,输出字典序最小的点对,使这两点间路径上点权的乘积模1000003的结果为k. 树的点分治搞了.因为是点权过根的两条路径的LCA会被重复统计,而注意到1000003是质数,所 ...
- [HDU4812]D Tree
vjudge 题意:给一棵树,每个点上有一个权值,求一条路径使得路径上权值的乘积膜\(10^6+3\)的结果为\(K\),输出路径的两个端点\(x,y\).如有多解,设\(x<y\),输出\(x ...
- hdu4812 逆元+树分治
逆元链接:https://www.cnblogs.com/zzqc/p/7192436.html 经典的树分治题 #pragma comment("linker,"/STACK:1 ...
- HDU4812 D tree 【点分治 + 乘法逆元】
D树 时间限制:10000/5000 MS(Java / Others)内存限制:102400/102400 K(Java / Others) 总共提交5400个已接受的提交1144 问题描述 南京理 ...
- 【点分治】【乘法逆元】hdu4812 D Tree
思路比较裸,但是要把答案存到哈希表里面,这里需要一定技巧,否则会被K=1且点权全是1的数据卡飞.预处理乘法逆元.TLE了一天.换了种点分治的姿势…… #pragma comment(linker,&q ...
- [hdu4812]D Tree(点分治)
题意:问有多少条路径,符合路径上所有节点的权值乘积模1000003等于k. 解题关键:预处理阶乘逆元,然后通过hash和树形dp$O(1)$的判定乘积存在问题,注意此道题是如何处理路径保证不重复的,具 ...
- #YCB#待做题目与填坑资料
各种填坑资料(qwq) 主席树(by YL)戳 树套树(by ZSY)戳 不要问我这些题咋来的(查大佬的水表呗) 题目列表: [HDU5977]Garden of Eden [BZOJ2752][HA ...
- Noip前的大抱佛脚----赛前任务
赛前任务 tags:任务清单 前言 现在xzy太弱了,而且他最近越来越弱了,天天被爆踩,天天被爆踩 题单不会在作业部落发布,所以可(yi)能(ding)会不及时更新 省选前的练习莫名其妙地成为了Noi ...
- D Tree HDU - 4812
https://vjudge.net/problem/HDU-4812 点分就没一道不卡常的? 卡常记录: 1.求逆元忘开longlong 2.把solve中分离各个子树的方法,由“一开始全部加入,处 ...
随机推荐
- 使用netstat检测及监测网络连接
在讲监测检测之前,先给大家在普及一个知识,那就是TCP连接的状态,TCP进行3次握手,其过程有很多状态,不同的连接状态,都有想对应的状态码,看下面列表: LISTEN:侦听来自远方的TCP端口的连接请 ...
- Asp.net中Request.Url的各个属性对应的意义介绍
Asp.net中Request.Url的各个属性对应的意义介绍 本文转载自 http://www.jb51.net/article/30254.htm 网络上关于Request.Url的说明已经很多也 ...
- 微信小程序 页面跳转传递数据
点击view 跳转页面 <view class="album_image" data-album-obj="{{item}}" bindtap=" ...
- Android查缺补漏(IPC篇)-- 进程间通讯基础知识热身
本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8479282.html 在Android中进程间通信是比较难的一部分,同时又非常 ...
- Spring Boot 注解详解
一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...
- Java经典编程题50道之三十八
编写一个函数:输入n为偶数时,调用函数求1/2+1/4+...+1/n:当输入n为奇数时,调用函数1/1+1/3+...+1/n. public class Example38 { public ...
- [翻译] 编写高性能 .NET 代码--第二章 GC -- 减少分配率, 最重要的规则,缩短对象的生命周期,减少对象层次的深度,减少对象之间的引用,避免钉住对象(Pinning)
减少分配率 这个几乎不用解释,减少了内存的使用量,自然就减少GC回收时的压力,同时降低了内存碎片与CPU的使用量.你可以用一些方法来达到这一目的,但它可能会与其它设计相冲突. 你需要在设计对象时仔细检 ...
- ubuntu终端常用命令及solarized配色(护眼)
ubuntu终端常用命令及solarized配色(护眼) ubuntu 终端 命令 1.常用命令 ctrl + l - 清屏 . cLear ctrl + c - 终止命令. ctrl + d ...
- Tomcat8+Spring-Security 启用安全通道(https)的一步步实现
近日学习Spring Security框架,学习到利用安全框架完成系统的安全通道控制时,来来回回遇到了不少问题.spring教程上写的略简单,对于我等小白来讲不足以支撑看书编码,好在网络上有资料可以查 ...
- Linux定时任务深入学习
为当前用户创建cron服务 1. 键入 crontab -e 编辑crontab服务文件 例如 文件内容如下: */2 * * * * /bin/sh /home/admin/jiaoben/bu ...