hdoj4812 D Tree(点分治)
题目链接:https://vjudge.net/problem/HDU-4812
题意:给定一颗带点权的树,求是否存在一条路经的上点的权值积取模后等于k,如果存在多组点对,输出字典序最小的。
思路:
点分治模板题。按照套路,找重心,求出子树中节点到重心的权值积取模后的值dis[i](包括重心的权值,也不可以不包括,一样的),用id[j]记录该路径的端点,用的是点分治的第二种写法。递归时,用桶mine[i]记录到重心权值积为i的最小编号。然后查找时,对dis[j],满足要求的tmp为:tmp*dis[j]%MOD=k*V[u],因为我们的dis中包括重心的权值,所以重心的权值被乘了2次。然后用到了逆元,tmp=k*V[u]%MOD*inv[dis[j]]%MOD。更新完答案后就更新桶。
因为没看清题目,题目求得点对(a,b)是a<b,而我以为(a,a)也满足,然后就找了两小时bug,崩溃到想锤电脑QAQ...。
AC代码:
#include<cstdio>
#include<algorithm>
#include<cctype>
#include<cstring>
using namespace std; inline int read(){
int x=,f=;char ch=;
while(!isdigit(ch)) {f|=ch=='-';ch=getchar();}
while(isdigit(ch)) x=(x<<)+(x<<)+(ch^),ch=getchar();
return f?-x:x;
} typedef long long LL;
const int maxn=1e5+;
const int MOD=1e6+;
const int maxk=1e6+;
const int inf=0x3f3f3f3f;
struct node1{
int v,nex;
}edge[maxn<<]; struct node2{
int x,y;
node2(){x=y=;}
node2(int x,int y):x(x),y(y){}
}ans; bool operator < (node2 a,node2 b){
if(a.x==b.x) return a.y<b.y;
return a.x<b.x;
} int n,k,cnt,head[maxn],sz[maxn],mson[maxn],Min,root,size;
int vis[maxn],mine[maxk],id[maxn],t,tt;
LL V[maxn],dis[maxn],inv[maxk]; void init(){
inv[]=;
for(int i=;i<maxk;++i)
inv[i]=(MOD-MOD/i)*inv[MOD%i]%MOD;
} void adde(int u,int v){
edge[++cnt].v=v;
edge[cnt].nex=head[u];
head[u]=cnt;
} void getroot(int u,int fa){
sz[u]=,mson[u]=;
for(int i=head[u];i;i=edge[i].nex){
int v=edge[i].v;
if(vis[v]||v==fa) continue;
getroot(v,u);
sz[u]+=sz[v];
mson[u]=max(mson[u],sz[v]);
}
mson[u]=max(mson[u],size-sz[u]);
if(mson[u]<Min) Min=mson[u],root=u;
} void getdis(int u,int fa,LL len){
dis[++t]=len,id[t]=u;
for(int i=head[u];i;i=edge[i].nex){
int v=edge[i].v;
if(vis[v]||v==fa) continue;
getdis(v,u,len*V[v]%MOD);
}
} void solve(int u){
mine[V[u]]=u,t=;
for(int i=head[u];i;i=edge[i].nex){
int v=edge[i].v;
if(vis[v]) continue;
tt=t;
getdis(v,u,V[u]*V[v]%MOD);
for(int j=tt+;j<=t;++j){
LL tmp=1LL*k*V[u]%MOD*inv[dis[j]]%MOD;
if(mine[tmp]==inf) continue;
node2 other;
if(mine[tmp]<id[j]) other.x=mine[tmp],other.y=id[j];
else other.x=id[j],other.y=mine[tmp];
if(other<ans) ans=other;
}
for(int j=tt+;j<=t;++j)
mine[dis[j]]=min(mine[dis[j]],id[j]);
}
mine[V[u]]=inf;
for(int i=;i<=t;++i)
mine[dis[i]]=inf;
} void fenzhi(int u,int ssize){
vis[u]=;
solve(u);
for(int i=head[u];i;i=edge[i].nex){
int v=edge[i].v;
if(vis[v]) continue;
Min=inf,root=;
size=sz[v]<sz[u]?sz[v]:(ssize-sz[u]);
getroot(v,);
fenzhi(root,size);
}
} int main(){
init();
memset(mine,0x3f,sizeof(mine));
while(~scanf("%d%d",&n,&k)){
cnt=;
ans.x=ans.y=n+;
for(int i=;i<=n;++i)
head[i]=vis[i]=;
for(int i=;i<=n;++i){
int tmp=read();
V[i]=tmp;
}
for(int i=;i<n;++i){
int u=read(),v=read();
adde(u,v);
adde(v,u);
}
Min=inf,root=,size=n;
getroot(,);
fenzhi(root,n);
if(ans.x==n+)
printf("No solution\n");
else
printf("%d %d\n",ans.x,ans.y);
}
return ;
}
hdoj4812 D Tree(点分治)的更多相关文章
- 【BZOJ-1468】Tree 树分治
1468: Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1025 Solved: 534[Submit][Status][Discuss] ...
- HDU 4812 D Tree 树分治+逆元处理
D Tree Problem Description There is a skyscraping tree standing on the playground of Nanjing Unive ...
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- [bzoj 1468][poj 1741]Tree [点分治]
Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...
- 【CF434E】Furukawa Nagisa's Tree 点分治
[CF434E]Furukawa Nagisa's Tree 题意:一棵n个点的树,点有点权.定义$G(a,b)$表示:我们将树上从a走到b经过的点都拿出来,设这些点的点权分别为$z_0,z_1... ...
- POJ 1741 Tree(点分治点对<=k)
Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...
- POJ 1741.Tree 树分治 树形dp 树上点对
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24258 Accepted: 8062 Description ...
- poj 1744 tree 树分治
Tree Time Limit: 1000MS Memory Limit: 30000K Description Give a tree with n vertices,each ed ...
- CF1039D You Are Given a Tree 根号分治,贪心
CF1039D You Are Given a Tree LG传送门 根号分治好题. 这题可以整体二分,但我太菜了,不会. 根号分治怎么考虑呢?先想想\(n^2\)暴力吧.对于每一个要求的\(k\), ...
随机推荐
- 从斐波那契数列看java方法的调用过程
先看斐波那契数列的定义: 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为 ...
- 【线性代数】3-2:零空间(Nullspace)
title: [线性代数]3-2:零空间(Nullspace) categories: Mathematic Linear Algebra keywords: Nullspace Pivot Colu ...
- 【CUDA 基础】6.2 并发内核执行
title: [CUDA 基础]6.2 并发内核执行 categories: - CUDA - Freshman tags: - 流 - 事件 - 深度优先 - 广度优先 - 硬件工作队列 - 默认流 ...
- PowerShell入门学习
一.概要 Powershell是运行在windows机器上实现系统和应用程序管理自动化的命令行脚本环境. powershell需要.NET环境的支持,同时支持.NET对象.之所以将Powershell ...
- 【MyBatis】【SQL】没有最快,只有更快,从一千万条记录中删除八百万条仅用1分9秒
这次直接使用delete from emp where cdate<'2018-02-02',看看究竟会发生什么. Mapper里写好SQL: <?xml version="1. ...
- 2.2 Go语言基础之位运算操作
一.位运算符 位运算符对整数在内存中的二进制位进行操作. 运算符 描述 & 参与运算的两数各对应的二进位相与. (两位均为1才为1) | 参与运算的两数各对应的二进位相或. (两位有一个为1就 ...
- jQuery常用操作部分总结
注意:$(“.xxx”) 类,一定要在前面加上点callback为完成后执行的函数名称隐藏显示:hide() show()淡入淡出:fadeIn() fadeOut() fadetoggl ...
- mybatis之动态SQL操作之插入
1) 根据条件,插入一个学生 /** * 持久层*/ public class StudentDao { /** * 动态SQL--插入 */ public void dynaSQLwithInse ...
- 'pybot.bat' 不是内部或外部命令,也不是可运行的程序
在通过命令行工具 运行RobotFramework的文件, 会使用到pybot.bat. 在dos输入pybot提示'pybot' 不是内部或外部命令,也不是可运行的程序或批处理文件, 可以在pyth ...
- 使用AOP思想封装JDBC
看代码 package learning.aop2; import org.springframework.stereotype.Component; import java.sql.SQLExcep ...