hdu4670 树分治
这题说的给了一颗树 然后树上有一些整数值,分别由他给的那30个素数组成,有多少条路径的路径上的点的乘积为立方数, 把每个数分解成相应的素数模3后的值,然后压缩为一个3进制的数
然后进行树的分支
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <string.h>
#include <map>
using namespace std;
const int maxn=;
typedef long long LL;
int H[maxn],nx[maxn*],to[maxn*],numofE;
LL prime[],V[maxn],ans;
LL bas[];
int N,K;
LL fenjie(LL a)
{
LL an=;
for(int i=; i<K; i++)
{
int d=;
while((a%prime[i])==){
d++;a/=prime[i];
}
an+=bas[i]*(d%);
}
return an;
}
void add(int u, int v)
{
numofE++;
to[numofE]=v;
nx[numofE]=H[u];
H[u]=numofE;
}
int Q[maxn],fa[maxn],subnum[maxn];
LL P[maxn];
bool center[maxn];
int searchroot(int cur)
{
int rear=;
fa[cur]=-,Q[rear++]=cur;
for(int i=; i<rear; i++)
{
int x=Q[i];
for(int j = H[x]; j; j=nx[j])
{
int tto = to[j];
if( tto == fa[x] || center[tto] )continue;
Q[rear++]=tto; fa[tto]=x;
}
}
int MIN=maxn*,root=cur;
for(int i=rear-; i>=; i--)
{
int x=Q[i];
subnum[x]=;
int MA=;
for(int j=H[x]; j; j=nx[j])
{
int tto=to[j];
if(tto == fa[x] || center[tto] )continue;
MA=max(MA,subnum[tto]);
subnum[x]+=subnum[tto];
}
MA=max(MA,rear-subnum[x]);
if(MIN>MA){
MIN=MA; root=x;
}
}
return root;
}
LL requrenum(LL a)
{
LL an=;
for(int i=; i<K; i++)
{
LL d=-(a%);
a/=;
if(d>)d=;
an+=d*bas[i];
}
return an;
}
LL temp[maxn];
LL jia(LL a, LL b)
{
LL an=;
for(int i=; i<K; i++)
{
int d=(a% + b%)%;
a/=;b/=;
an+=bas[i]*d;
}
return an;
}
void count_pair(map<LL,LL>&ds,map<LL,LL>tds,int root)
{
map<LL,LL>::iterator it;
it=tds.begin();
while(it!=tds.end()){
LL re=requrenum(it->first);
if(ds.count(re)){
ans+=ds[re]*(it->second);
}
++it;
}
it=tds.begin();
while(it!=tds.end()){
LL vv=jia(it->first,V[root]);
if(ds.count(vv)){
ds[vv]+=it->second;
}else
ds[vv]=it->second;
it++;
}
}
void updateedg(int cur, map<LL,LL> &ds)
{
int rear=;
fa[cur]=;
Q[rear++]=cur;
P[]=V[cur];
if(ds.count(P[]))ds[P[]]++;
else ds[P[]]=;
for(int i=; i<rear; i++)
{
int x=Q[i];
for(int j=H[x]; j; j=nx[j])
{
int tto=to[j];
if(tto==fa[x]||center[tto])continue;
fa[tto]=x;
P[rear]=jia(P[i],V[tto]);
if(ds.count(P[rear]))ds[ P[rear] ]++;
else ds[ P[rear] ]=;
Q[rear++]=tto;
}
}
}
void dfs(int cur)
{
int root;
root=searchroot(cur);
center[root]=true;
map<LL,LL>ds,tds;
ds[V[root]]=;
for(int i=H[root]; i; i=nx[i])
{
int tto=to[i];
if(center[tto])continue;
dfs(tto);
tds.clear();
updateedg(tto,tds);
count_pair(ds,tds,root);
}
center[root]=false; return ;
}
int main()
{
bas[]=;
for(int i=;i<=; i++)
bas[i]=bas[i-]*;
while(scanf("%d",&N)==)
{
scanf("%d",&K);
numofE=ans=;
for(int i=; i<K; i++)scanf("%I64d",&prime[i]);
for(int i=; i<=N; i++){
H[i]=;
scanf("%I64d",&V[i]);
V[i]=fenjie(V[i]);
if(V[i]==)ans++;
}
for(int i=; i<N; i++)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
dfs();
printf("%I64d\n",ans);
}
return ;
}
hdu4670 树分治的更多相关文章
- HDU4670 Cube number on a tree 树分治
人生的第一道树分治,要是早点学我南京赛就不用那么挫了,树分治的思路其实很简单,就是对子树找到一个重心(Centroid),实现重心分解,然后递归的解决分开后的树的子问题,关键是合并,当要合并跨过重心的 ...
- hdu-5977 Garden of Eden(树分治)
题目链接: Garden of Eden Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/ ...
- 【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 ...
- BZOJ 2152: 聪聪可可 树分治
2152: 聪聪可可 Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一 ...
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- UVALive 7148 LRIP【树分治+线段树】
题意就是要求一棵树上的最长不下降序列,同时不下降序列的最小值与最大值不超过D. 做法是树分治+线段树,假设树根是x,y是其当前需要处理的子树,对于子树y,需要处理出两个数组MN,MX,MN[i]表示以 ...
- BZOJ 2566 xmastree(树分治+multiset)
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2566 题意:一棵有边权的树.结点有颜色.每次修改一个点的颜色.求每次修改后所有同色 ...
- 树分治&树链剖分相关题目讨论
预备知识 树分治,树链剖分 poj1741 •一棵有n个节点的树,节点之间的边有长度.方方方想知道,有多少个点对距离不超过m 题解 点分治模板题.详见我早上写的http://www.cnblogs ...
随机推荐
- ADO多线程数据库总结
ADO多线程数据库查询通常会出现以下问题: 1.CoInitialize 没有调用(CoInitialize was not called):所以,在使用任何dbGo对象前,必须手 调用CoIniti ...
- Spring 测试
1. pom.xml ==> Depency <!-- Test --> <dependency> <groupId>junit</groupId> ...
- tensorflow入门笔记(二) 滑动平均模型
tensorflow提供的tf.train.ExponentialMovingAverage 类利用指数衰减维持变量的滑动平均. 当训练模型的时候,保持训练参数的滑动平均是非常有益的.评估时使用取平均 ...
- PyChram快捷键
- caffe matlab接口编译遇到的问题记录
今天编译的过程中遇到的问题以及查阅到的资料,记录在这里,希望可以帮到其他人. BVLC的caffe源码,如果要编译matlab的接口时,首先需要将makefile.config文件中的matlab的安 ...
- oracle表空间的管理
1.创建表空间 CREATE TABLESPACE TBS_TR_DATA DATAFILE '/oradata/rTBS_TR_DATA_001.dbf' SIZE 64G EXTENT MANAG ...
- Maven中groupId和artifactId的含义
groupId和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven仓库去,你想要找到你的项目就必须根据这两个id去查找.groupId是项目组织唯一的标识 ...
- yum方式安装lnmp
1.安装mysql>>>yum -y install mariadb mariadb-server 给root用户设置密码 >>>mysqladmin -uroot ...
- 自动化工具之三:pywinauto
Python自动化工具:pywinauto 一.pywinauto的安装 (1)安装命令 pip install -U pywinauto/pip3 install -U pywinauto (2)验 ...
- pip升级时报错--- No module named 'pip._internal'
一.问题: 之前python3.6是安装的pip版本为:pip=9.0.1,我按照提示升级报错,一直装不上pip18.0,于是直接在site-package目录下删掉了pi ...