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 ...
随机推荐
- Fiddler怎么可以抓取https的请求包
对于https的协议是带有安全的功能,所有有一些的https的协议是无法抓到的,所以需要通过设置filler中来对,来使用filler的方式的来抓取到https的请求包,那么如何使用filler中抓取 ...
- ORACLE UNDO
UNDO 数据操纵 数据操纵语言(DML)由以下SQL语句组成: INSERT,DELETE,UPDATE,MERGE DML始终作为事务处理的一部分执行,它可以: 使用Rollback命令执行回退 ...
- Java之旅_高级教程_实例_打印图形
1.打印菱形 public class MainClass{ public static void main(String[] args){ printStar(10); } public stati ...
- elasticsearch安装IK分词插件
一 打开网页:https://github.com/medcl/elasticsearch-analysis-ik/releases 这个是ik相关的包,找到你想下载的版本,下载对应的zip包 二 然 ...
- linux基本介绍和使用
基本介绍 Linux入门教程 快捷键 linux 快捷键 用户及用户组 linux之用户和用户组
- 【pyqtgraph绘图】线条,填充和颜色
解读官方API-线条,填充和颜色 参考: http://www.pyqtgraph.org/documentation/style.html 线条,填充和颜色 Qt依靠其QColor,QPen和QBr ...
- Python 标准输出 sys.stdout 重定向(转)
add by zhj: 其实很少使用sys.stdout,之前django的manage.py命令的源码中使用了sys.stdout和sys.stderr,所以专门查了一下 这两个命令与print的区 ...
- 解读EXPLAIN执行计划中的key_len(转)
原文:http://imysql.com/2015/10/20/mysql-faq-key-len-in-explain.shtml 导读 EXPLAIN中的key_len一列表示什么意思,该如何解读 ...
- python练习题-day2
1.判断下列逻辑语句的True,False 1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 True ...
- Hibernate三种状态,缓存,以及update更新问题
一. Hibernate中对象的三种状态 1. 瞬时状态(transient) 当我们通过Java的new关键字来生成一个实体对象时,这时这个实体对象就处于自由状态,此时该对象只是通过JVM获得了一块 ...