题意:

给定一棵 n 个点的树,每个点有权值 Vi

问是否存在一条路径使得路径上所有点的权值乘积 mod(10^6 + 3) 为 K

输出路径的首尾标号,若有多解,输出字典序最小的解

对于100%的数据,有1≤n≤10^5,0≤K≤10^6+2,1≤vi ≤10^6+2

思路:RYZ作业

预处理逆元

哈希路径权值乘积的模数,保存编号用来查询

 const mo=;
var head,vet,next,len,flag,son:array[..]of longint;
f:array[..]of longint;
dis,a:array[..]of int64;
q:array[..,..]of longint;
exf:array[..mo]of int64;
hash:array[..mo]of longint;
n,m,i,j,tot,x,y,z,ans1,ans2,sum,root,top,k:longint; procedure add(a,b:longint);
begin
inc(tot);
next[tot]:=head[a];
vet[tot]:=b;
head[a]:=tot;
end; function max(x,y:longint):longint;
begin
if x>y then exit(x);
exit(y);
end; procedure swap(var x,y:longint);
var t:longint;
begin
t:=x; x:=y; y:=t;
end; function getroot(u,fa:longint):longint;
var e,v:longint;
begin
son[u]:=; f[u]:=;
e:=head[u];
while e<> do
begin
v:=vet[e];
if (v<>fa)and(flag[v]=) then
begin
getroot(v,u);
son[u]:=son[u]+son[v];
f[u]:=max(f[u],son[v]);
end;
e:=next[e];
end;
f[u]:=max(f[u],sum-f[u]);
if f[u]<f[root] then root:=u;
end; procedure dfs(u,fa:longint);
var e,v:longint;
begin
inc(top); q[top,]:=dis[u]; q[top,]:=u;
e:=head[u];
while e<> do
begin
v:=vet[e];
if (v<>fa)and(flag[v]=) then
begin
dis[v]:=dis[u]*a[v] mod mo;
dfs(v,u);
end;
e:=next[e];
end;
end; procedure cmp(x,id:longint);
var y:longint;
begin
x:=exf[x]*k mod mo;
y:=hash[x];
if y= then exit;
if y>id then swap(y,id);
if (y<ans1)or((y=ans1)and(id<ans2)) then
begin
ans1:=y; ans2:=id;
end;
end; procedure solve(u:longint);
var e,v,i,now:longint;
begin
flag[u]:=; hash[a[u]]:=u;
e:=head[u];
while e<> do
begin
v:=vet[e];
if flag[v]= then
begin
top:=; dis[v]:=a[v];
dfs(v,u);
for i:= to top do cmp(q[i,],q[i,]); //对于每一个子树,因为不能重复计算u这个根结点,分别计算子树内结果,后面再用加上根结点的答案更新
top:=; dis[v]:=a[u]*a[v] mod mo;
dfs(v,u);
for i:= to top do
begin
now:=hash[q[i,]];
if (now=)or(q[i,]<now) then hash[q[i,]]:=q[i,]; //用子树中的路径加上根结点,更新经过根结点的答案
end;
end;
e:=next[e];
end;
hash[a[u]]:=;
e:=head[u];
while e<> do
begin
v:=vet[e];
if flag[v]= then
begin
top:=; dis[v]:=a[u]*a[v] mod mo;
dfs(v,u);
for i:= to top do hash[q[i,]]:=; //清空所有信息
end;
e:=next[e];
end;
e:=head[u];
while e<> do
begin
v:=vet[e];
if flag[v]= then
begin
root:=; sum:=son[v];
getroot(v,);
solve(root);
end;
e:=next[e];
end;
end; begin
assign(input,'hdoj4812.in'); reset(input);
assign(output,'hdoj4812.out'); rewrite(output);
exf[]:=; exf[]:=;
for i:= to do exf[i]:=exf[mo mod i]*(mo-mo div i) mod mo;
while not eof do
begin
// fillchar(head,sizeof(head),);
//fillchar(flag,sizeof(flag),);
for i:= to n do
begin
head[i]:=; flag[i]:=;
end;
tot:=; ans1:=maxlongint; ans2:=maxlongint;
read(n,k);
if n= then break;
for i:= to n do read(a[i]);
for i:= to n- do
begin
read(x,y);
add(x,y);
add(y,x);
end;
root:=; sum:=n; f[]:=n+;
getroot(,);
solve(root);
if ans1=maxlongint then writeln('No solution')
else writeln(ans1,' ',ans2);
end;
close(input);
close(output);
end.

【HDOJ4812】D Tree(点分治)的更多相关文章

  1. hdoj4812 D Tree(点分治)

    题目链接:https://vjudge.net/problem/HDU-4812 题意:给定一颗带点权的树,求是否存在一条路经的上点的权值积取模后等于k,如果存在多组点对,输出字典序最小的. 思路: ...

  2. 【BZOJ-1468】Tree 树分治

    1468: Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1025  Solved: 534[Submit][Status][Discuss] ...

  3. HDU 4812 D Tree 树分治+逆元处理

    D Tree Problem Description   There is a skyscraping tree standing on the playground of Nanjing Unive ...

  4. POJ 1741 Tree 树分治

    Tree     Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...

  5. [bzoj 1468][poj 1741]Tree [点分治]

    Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...

  6. 【CF434E】Furukawa Nagisa's Tree 点分治

    [CF434E]Furukawa Nagisa's Tree 题意:一棵n个点的树,点有点权.定义$G(a,b)$表示:我们将树上从a走到b经过的点都拿出来,设这些点的点权分别为$z_0,z_1... ...

  7. POJ 1741 Tree(点分治点对<=k)

    Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...

  8. POJ 1741.Tree 树分治 树形dp 树上点对

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24258   Accepted: 8062 Description ...

  9. poj 1744 tree 树分治

    Tree Time Limit: 1000MS   Memory Limit: 30000K       Description Give a tree with n vertices,each ed ...

  10. CF1039D You Are Given a Tree 根号分治,贪心

    CF1039D You Are Given a Tree LG传送门 根号分治好题. 这题可以整体二分,但我太菜了,不会. 根号分治怎么考虑呢?先想想\(n^2\)暴力吧.对于每一个要求的\(k\), ...

随机推荐

  1. jmeter(二)元件的作用域与执行顺序

    1.元件的作用域 JMeter中共有8类可被执行的元件(测试计划与线程组不属于元件),这些元件中,取样器是典型的不与其它元件发生交互作用的元件,逻辑控制器只对其子节点的取样器有效,而其它元件(conf ...

  2. [转]F# Samples 101 - Visual Studio 2010

    http://code.msdn.microsoft.com/F-Samples-101-0576cb9f/sourcecode?fileId=18956&pathId=1045958806 ...

  3. 【原】无脑操作:Eclipse + Maven + jFinal + MariaDB 环境搭建

    一.开发环境 1.windows 7 企业版 2.Eclipse IDE for Enterprise Java Developers  Version: 2019-03 (4.11.0) 3.JDK ...

  4. 解决::processDebugResourcesERROR: In<declare-styleable> FontFamilyFont编译报错

    cordova编译时报错 错误信息 :processDebugResourcesERROR: In <declare-styleable> FontFamilyFont, unable t ...

  5. 最新最强短视频SDK——来自RDSDK.COM

    北京锐动天地信息技术有限公司成立于2007年9月.多年来一直专注于音视频领域核心技术的研发, 拥有Windows.iOS.Android全平台自主知识产权的领先技术产品. 2011年获得新浪战略投资, ...

  6. [转] NTFS Permission issue with TAKEOWN & ICACLS

    (转自:NTFS Permission issue with TAKEOWN & ICACLS - SAUGATA   原文日期:2013.11.19) Most of us using TA ...

  7. 如何优雅地从CSDN转载文章

    复制粘贴应该是最显而易见的方法,但是不仅会有丢失内容,而且格式也会丢失.要想达到更好的效果,可以从html源码入手. 1.在chrome浏览器中打开要转载的文章,右键选择检查 2.在chrome的右方 ...

  8. 开放API接口

    [开放API]——知乎.博客园等开放API接口(更新ing)   Cnodejs.org: https://cnodejs.org/api/ 和风天气: http://docs.heweather.c ...

  9. windows sdk编程隐藏窗体标题栏

    #include <windows.h> /*消息处理函数声明*/ HRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM ...

  10. Oracle数据库自定义函数练习20181031

    --测试函数3 CREATE OR REPLACE FUNCTION FN_TEST3 (NUM IN VARCHAR2) RETURN VARCHAR2 IS TYPE VARCHAR2_ARR ) ...