这题说的给了一颗树 然后树上有一些整数值,分别由他给的那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 树分治的更多相关文章

  1. HDU4670 Cube number on a tree 树分治

    人生的第一道树分治,要是早点学我南京赛就不用那么挫了,树分治的思路其实很简单,就是对子树找到一个重心(Centroid),实现重心分解,然后递归的解决分开后的树的子问题,关键是合并,当要合并跨过重心的 ...

  2. hdu-5977 Garden of Eden(树分治)

    题目链接: Garden of Eden Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/ ...

  3. 【BZOJ-1468】Tree 树分治

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

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

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

  5. BZOJ 2152: 聪聪可可 树分治

    2152: 聪聪可可 Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一 ...

  6. POJ 1741 Tree 树分治

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

  7. UVALive 7148 LRIP【树分治+线段树】

    题意就是要求一棵树上的最长不下降序列,同时不下降序列的最小值与最大值不超过D. 做法是树分治+线段树,假设树根是x,y是其当前需要处理的子树,对于子树y,需要处理出两个数组MN,MX,MN[i]表示以 ...

  8. BZOJ 2566 xmastree(树分治+multiset)

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2566 题意:一棵有边权的树.结点有颜色.每次修改一个点的颜色.求每次修改后所有同色 ...

  9. 树分治&树链剖分相关题目讨论

    预备知识 树分治,树链剖分   poj1741 •一棵有n个节点的树,节点之间的边有长度.方方方想知道,有多少个点对距离不超过m 题解 点分治模板题.详见我早上写的http://www.cnblogs ...

随机推荐

  1. 20165225《Java程序设计》第三周学习总结

    20165225<Java程序设计>第三周学习总结 1.视频与课本中的学习: 遇到的问题: 问题如下,无法编译. 最后经同学点出要放在同一个打包的文件夹里,于是就运行成功了,下面是4_15 ...

  2. ECharts图形库

    ECharts图形库百度的项目,图形的创建也比较简单,直接引用Javascript即可 1,引入<script src="{{ url_for("static",f ...

  3. zabbix宏(macro)使用:自定义监控阈值

    一.简单应用场景 zabbix在监控cpu load时并没有考虑客户端cpu的个数和核心数量,当平均5分钟的负载达到5时zabbix执行报警动作,这样是非常不合理的,笔者的被监控机器有四核和单核,现在 ...

  4. env:bash \r解决

    1.brew install dos2unix2.find . -type f -exec dos2unix {} \;

  5. 前端HTML目录

    前端 HTML 简介 前端 HTML文档结构介绍 前端 HTML文档 详解 前端 HTML 注释 前端 HTML标签介绍 前端 HTML的规范 前端 HTML 常用标签 head标签相关内容 前端 H ...

  6. MongoDB limit 选取 skip跳过 sort排序 方法

    MongoDB  limit 选取 skip跳过 sort排序 在mysql里有order by  MongoDB用sort代替order by > db.user.find() { " ...

  7. CentOS 7.2编译安装nginx1.10.3+MySQL5.5.38+PHP5.5.38

    1.关闭firewallad 关闭防火墙 systemctl stop firewalld.service 禁止firewall开机启动 systemctl disable firewalld.ser ...

  8. AFNetWorking上传JSON串

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.responseSerializer = [AFJSON ...

  9. 通过Tesseract实现简单的OCR

    Tesseract 简介 Tesseract 的 OCR 引擎最先由 HP 实验室于 1985 年开始研发,至 1995 年时已经成为 OCR 业内最准确的三款识别引擎之一.然而,HP 不久便决定放弃 ...

  10. 正交表和TCG的使用

    正交表法是一种有效减少测试用例个数的设计方法. 正交表法的依据是Galois理论,从大量的实验数据中挑选适量的.有代表性的点,从而合理的安排实验的一种科学实验设计方法.在测试用例的设计中,可以从大量的 ...