这题说的给了一颗树 然后树上有一些整数值,分别由他给的那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. SYSAUX表空间如何清理

    ############################################################ 操作方案################################### ...

  2. 新建oracle用户

    1.oracle 账户登录linux:2.如果存在多个切换实例:命令:export ORACLE_SID=实例名:如:export ORACLE_SID=utf81863.切换至管理员账户:sqlpl ...

  3. es6原型的继承

    class Parent { name = 'liangcheng'; } const parent = new Parent(); console.log(parent); // 类继承某实例对象属 ...

  4. oracle中in和exists的区别

    IN适合于外表大而内表小的情况:EXISTS适合于外表小而内表大的情况. 性能上的比较 比如Select * from T1 where x in ( select y from T2 ) 执行的过程 ...

  5. ConcurrentHashMap详解

    public class ConcurrentHashMap<K,V>extends AbstractMap<K,V>implements ConcurrentMap<K ...

  6. (1.10)SQL优化——mysql 常见SQL优化

    (1.10)常用SQL优化 insert优化.order by 优化 1.insert 优化 2.order by 优化 [2.1]mysql排序方式: (1)索引扫描排序:通过有序索引扫描直接返回有 ...

  7. SVN服务的模式和多种访问方式 多种访问原理图解与优缺点

    SVN企业应用场景 SVN任是当前企业的主流.git正在发展,未来会成为主流.如果大家精力足够,建议同时掌握. 1.4运维人员掌握版本管理 对于版本管理系统,运维人员需要掌握的技术点: 1.安装.部署 ...

  8. Node.js之npm使用

    1.使用国内镜像 本人window8系统安装node.js之后,使用npm的命令安装Express一直失败: npm install express -g (以上命令会将 Express 框架安装在当 ...

  9. 入门 Webpack,看这篇就够了

    转:https://segmentfault.com/a/1190000006178770 2018年8月25日更新,目前 webpack 已经更新值 4.17.1 ,本文所用到的各种库或多或少有些过 ...

  10. MyBatis——模糊查询

    在mybatis中可以使用三种模糊查询的方式: <!-- 模糊查询 --> <select id="selectListByTitle" parameterTyp ...