HDU 4812 D Tree 树分区+逆+hash新位置
意甲冠军:
特定n点树 K
以下n号码是正确的点
以下n-1行给出了树的侧。
问:
所以,如果有在正确的道路点图的路径 % mod = K
如果输出路径的两端存在。
多条路径则输出字典序最小的一条。
思路:
按树重心分治。
分成路径是否经过树重心。
然后用力码。
。
has[x] = u;
表示乘积为x 相应的点是u
但这样has就不能用计数器来优化清空。
所以用2个数组: has[x] = cnt; has_id[x] = u;
这样has里存的是乘积为x是否存在。has_id[x] 来记录点。
#pragma comment(linker, "/STACK:10240000000000,10240000000000")
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <map>
#include <algorithm>
using namespace std;
const int inf = 10000000;
typedef pair<int,int> pii;
typedef long long ll;
const int mod = 1000000+3;
int ni[mod];
void quick(int x){
int ans = 1, tmp = x, y = mod-2;
while(y){
if(y&1)
ans = (ll)ans * (ll)x % (ll)mod;
x = (ll)x*(ll)x%(ll)mod;
y>>=1;
}
ni[tmp] = ans;
}
template <class T>
inline bool rd(T &ret) {
char c; int sgn;
if(c=getchar(),c==EOF) return 0;
while(c!='-'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')? -1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
ret*=sgn;
return 1;
}
template <class T>
inline void pt(T x) {
if (x <0) {
putchar('-');
x = -x;
}
if(x>9) pt(x/10);
putchar(x%10+'0');
}
#define N 100005
pii ans;
void updata(int x, int y){
if(x>y)swap(x,y);
if(ans.first > x || ans.first==x && ans.second > y)
ans = pii(x,y);
}
struct Edge{
int to, nex;
}edge[N<<1];
int head[N], edgenum;
void init(){memset(head,-1,sizeof head); edgenum = 0;}
void add(int u, int v){
Edge E = {v,head[u]};
edge[edgenum] = E;
head[u] = edgenum++;
}
int a[N], K, mul[N];
int n, siz[N];
int s[N], id[N], top;
int has[mod], has_id[mod], tim;
bool vis[N];
void add_hash(int u, int multi_ans){
if(has[multi_ans] == tim)
has_id[multi_ans] = min(has_id[multi_ans], u);
else {
has[multi_ans] = tim;
has_id[multi_ans] = u;
}
}
int wval, wroot;
void getroot(int u, int fa){
siz[u] = 1;
int maxv = 0;
for(int i = head[u]; ~i; i = edge[i].nex){
int v = edge[i].to; if(v == fa || vis[v])continue;
getroot(v, u);
siz[u] += siz[v];
maxv = max(maxv, siz[v]);
}
maxv = max(maxv, siz[0] - siz[u]);
if(maxv < wval)wval = maxv, wroot = u;
}
int find_root(int u, int fa){ //找到子树的重心
wval = 1000000;
getroot(u, fa);
return wroot;
} void multi(int u, int fa){
mul[u] = ((ll)a[u]*(ll)mul[fa])%(ll)mod;
siz[u] = 1;
s[top] = mul[u]; id[top++] = u;
for(int i = head[u];~i;i=edge[i].nex){
int v = edge[i].to; if(v == fa || vis[v])continue;
multi(v, u);
siz[u] += siz[v];
}
}
void solve(int u){
tim++;
mul[u] = 1;
vis[u] = 1;
for(int i = head[u]; ~i; i = edge[i].nex){
int v = edge[i].to; if(vis[v])continue;
top = 0;
multi(v, u);
for(int i = 0; i < top; i++)
{
if(K == (ll)s[i]*(ll)a[u]%(ll)mod)
updata(u, id[i]);
int x = (ll)K * (ll)ni[ (ll)s[i] * (ll)a[u] % (ll)mod ] % (ll)mod;
if(has[x] == tim)
updata(id[i], has_id[x]);
}
for(int i = 0; i < top; i++)
{
add_hash(id[i], s[i]);
}
}
for(int i = head[u];~i;i=edge[i].nex){
int v = edge[i].to; if(vis[v])continue;
siz[0] = siz[v];
solve(find_root(v, u));
}
}
void input(){
init();
for(int i = 1; i <= n; i++)rd(a[i]);
for(int i = 1, u, v; i < n; i++)
{
rd(u); rd(v);
add(u,v); add(v,u);
}
}
int main() {
tim = 0;
memset(has, 0, sizeof has);
for(int i = 0; i < mod; i++) quick(i);
while(rd(n) && rd(K)) {
input();
ans.first = inf;
memset(vis, 0, sizeof vis);
siz[0] = n;
solve(find_root(1,1));
if(ans.first == inf)puts("No solution");
else {
pt(ans.first); putchar(' ');
pt(ans.second); putchar('\n');
}
}
return 0;
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
HDU 4812 D Tree 树分区+逆+hash新位置的更多相关文章
- 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 树分治+逆元处理
D Tree Problem Description There is a skyscraping tree standing on the playground of Nanjing Unive ...
- HDU 4812 D Tree 树分治
题意: 给出一棵树,每个节点上有个权值.要找到一对字典序最小的点对\((u, v)(u < v)\),使得路径\(u \to v\)上所有节点权值的乘积模\(10^6 + 3\)的值为\(k\) ...
- HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)
题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...
- 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 5379 Mahjong tree(树的遍历&组合数学)
本文纯属原创,转载请注明出处.谢谢. http://blog.csdn.net/zip_fan 题目传送门:http://acm.hdu.edu.cn/showproblem.php? pid=537 ...
- HDU 3333 Turing Tree (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...
- HDU 3333 Turing Tree --树状数组+离线处理
题意:统计一段序列[L,R]的和,重复元素只算一次. 解法:容易看出在线做很难处理重复的情况,干脆全部讲查询读进来,然后将查询根据右端点排个序,然后离散化数据以后就可以操作了. 每次读入一个数,如果这 ...
随机推荐
- 中介模式和学习日记Effective C++
中介模式(Mediator):利用中介对象来封装一组对象交互.中保使对象并不需要显式地相互引用,使得松耦合,的交互. (1).中介者模式非常easy在系统中应用,也非常easy在系统中误用.当系统出现 ...
- S3C2416裸机开发系列19_Fatfs播放录像wav音频文件
S3C2416裸机开发系列19 Fatfs播放录像wav音频文件 国际象棋男孩 1048272975 多媒体资源,一般都是以文件的形式存储在固化存储器中.Fatfs所支持的fat32为windo ...
- 于ubuntu-kylin14.10下一个,无法使用apt-get具( libc6-i386 : 赖: libc6 (= 2.15-0ubuntu10.5) 但 2.19-0ubuntu6 一个已)
这篇文章有xhz1234(徐洪志)书写.转载请注明出处. http://blog.csdn.net/xhz1234/article/details/37044531 作者:徐洪志 背景:安装wine. ...
- Duanxx的C++学习 : 数字转换String
下面是这两个数字转换String道路.件:sstream string num2str1(unsigned int num) { stringstream ss; ss<<num; ret ...
- HDU 3172 Virtual Friends(并用正确的设置检查)
职务地址:pid=3172">HDU 3172 带权并查集水题.每次合并的时候维护一下权值.注意坑爹的输入. . 代码例如以下: #include <iostream> # ...
- 探寻宝藏(双向DP)
题目描述 传说HMH大沙漠中有一个M*N迷宫,里面藏有许多宝物.某天,Dr.Kong找到了迷宫的地图,他发现迷宫内处处有宝物,最珍贵的宝物就藏在右下角,迷宫的进出口在左上角.当然,迷宫中的通路不是平坦 ...
- C# Windows Schedule task此次收购task下一步执行时间
最近进行了一次需求和Windows Schedule task相关职能,通过schedule,计算下一次运行task时间. 它用于由第三方DLL实现,以下网站,以下载来自: http://tasksc ...
- Apriori算法Python实现
Apriori如果数据挖掘算法的头发模式挖掘鼻祖,从60年代开始流行,该算法非常简单朴素的思维.首先挖掘长度1频繁模式,然后k=2 这些频繁模式的长度合并k频繁模式.计算它们的频繁的数目,并确保其充分 ...
- Codeforces Round #265 (Div. 2) C. No to Palindromes! 构建无回文串子
http://codeforces.com/contest/465/problem/C 给定n和m,以及一个字符串s,s不存在长度大于2的回文子串,如今要求输出一个字典比s大的字符串,且串中字母在一定 ...
- 转载使用Flurl制作可复用的分页组件
使用Flurl制作可复用的分页组件 使用ASP.NET MVC查询时,一直使用MvcPaging组件,虽然需要自定义MvcPaging.Pager才能达到我想要的效果,但在没有较好的URL库时,还是这 ...