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 ...
随机推荐
- pandas之DataFrame
DataFrame 类型类似于数据库表结构的数据结构,其含有行索引和列索引,可以将DataFrame 想成是由相同索引的Series组成的Dict类型.在其底层是通过二维以及一维的数据块实现. 1,D ...
- java 抽象类 abstract
package cn.sasa.com; //抽象类 被abstract修饰的类 public abstract class Animal { //抽象类的成员变量 的定义 与 一般类是一样的 pri ...
- oracle日志归档空间清理
进入机器,设置环境变量,如: export ORACLE_HOME=/main/app/oracle/product//db_1 export ORACLE_SID=devdb 然后切换oracle用 ...
- membership DB生成 & dll 强命名 & 证书生成
UPD(Membership)数据库安装1.使用 Aspnet_regsql.exe 安装数据库 在 C:\WINDOWS\Microsoft.NET\Framework\\aspnet_regsql ...
- python wmi模块 获取windows内部信息
WMI (Windows Management Instrumentation) 模块可用于获取 Windows 内部信息,在使用Python获取Windows系统上的相关的信息可以使用WMI接口来获 ...
- laravel用crud修改产品items-新建resource controller和routing
前面我们创建了laravel简单的items产品api,但是需要在数据库添加,如何在网页上直接添加呢?我们可以用view来操作crud(增加Create.读取查询Retrieve.更新Update和删 ...
- OC图片滑动验证
没事逛cocoaChina的时候有人问图片验证码怎么做,我看了下,网上有很多第三方的框架,但好多都是收费的,所以考虑自己能否做一个,该封装有点简陋,不过可以根据自己需要自行修改 该代码用到的技术,UI ...
- OC分割输入验证码的视觉效果
效果图: 用到的类: UITextField+VerCodeTF.h #import <UIKit/UIKit.h> @protocol VerCodeTFDelegate <UIT ...
- RN无限轮播以及ScrollView的大小调节问题
如果你的ScrollView的大小是全屏,height不能用,这种情况需要给ScrollView添加一个容器View,然后调节容器View的大小 无限轮播这里我使用的是一个第三方的插件react-na ...
- Java中的boxing和unboxing(转)
测试代码: System.out.println(0L == 0);//true System.out.println(((Long)0L).equals(0));//false Integer i1 ...