题意:给一个N个带权节点的树,权值以给定的K个素数为因子,求路径上节点乘积为立方数的路径条数

思路:立方数的性质是每个因子的个数为3的倍数,那么每个因子只需要保存0-2三个状态即可,然后路径就可以转化为一个K位3进制数,点分治后,便可以用一个map来查询路径经过根的答案。代码与上一题(poj1741)类似:http://www.cnblogs.com/jklongint/p/4960052.html

#pragma comment(linker,"/STACK:10240000,10240000")
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <map>
#include <vector>
using namespace std;
#define X first
#define Y second
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define all(a) (a).begin(), (a).end()
#define mset(a, x) memset(a, x, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(b))
#define cas() int T, cas = 0; cin >> T; while (T --)
template<typename T>bool umax(T&a, const T&b){return a<b?(a=b,true):false;}
template<typename T>bool umin(T&a, const T&b){return b<a?(a=b,true):false;}
typedef long long ll;
typedef pair<int, int> pii; #ifndef ONLINE_JUDGE
#include "local.h"
#endif const int N = 5e4 + 7;
const int M = N;
const int inf = 1e9 + 7; namespace Edge {
int last[N], to[M << 1], next[M << 1], cntE;
void init() {
cntE = 0;
memset(last, -1, sizeof(last));
}
void addEdge(int u, int v) {
to[cntE] = v;
next[cntE] = last[u];
last[u] = cntE ++;
}
} int n, K; struct Node {
char p[33];
char &operator[] (int x) {
return p[x];
}
ll getNum() {
ll ans = 0;
for (int i = 0; i < K; i ++) {
ans = ans * 3 + p[i];
}
return ans;
}
ll getComplement() {
ll ans = 0;
for (int i = 0; i < K; i ++) {
ans = ans * 3 + (p[i]? 3 - p[i] : 0);
}
return ans;
}
Node operator+ (Node &that) {
Node ans;
for (int i = 0; i < K; i ++) {
ans[i] = p[i] + that[i];
if (ans[i] >= 3) ans[i] -= 3;
}
return ans;
}
Node operator- (Node &that) {
Node ans;
for (int i = 0; i < K; i ++) {
ans[i] = p[i] - that[i];
if (ans[i] < 0) ans[i] += 3;
}
return ans;
}
}; namespace Center {
int root, siz, son[N];
void init() {
siz = inf;
}
void getRoot(int cur, int fa, int total, bool used[]) {
son[cur] = 0;
int buf = 0;
for (int i = Edge::last[cur]; ~i; i = Edge::next[i]) {
int to = Edge::to[i];
if (to != fa && !used[to]) {
getRoot(to, cur, total, used);
son[cur] += son[to] + 1;
buf = max(buf, son[to] + 1);
}
}
buf = max(buf, total - son[cur] - 1);
if (buf < siz || buf == siz && cur < siz) {
siz = buf;
root = cur;
}
}
} bool used[N];
Node r[N]; void getNode(int cur, int fa, Node sum, vector<Node> &vt, bool used[]) {
vt.pb(sum);
for (int i = Edge::last[cur]; ~i; i = Edge::next[i]) {
int to = Edge::to[i];
if (to != fa && !used[to]) getNode(to, cur, sum + r[to], vt, used);
}
} ll getAns(vector<Node> &vt, Node &s) {
ll ans = 0;
map<ll, int> mp;
for (int i = 0; i < vt.size(); i ++) {
mp[vt[i].getNum()] ++;
ans += mp[(vt[i] - s).getComplement()];
}
return ans;
} ll work(int cur) {
used[cur] = true;
vector<Node> total;
total.push_back(r[cur]);
ll ans = 0;
for (int i = Edge::last[cur]; ~i; i = Edge::next[i]) {
int to = Edge::to[i];
if (!used[to]) {
vector<Node> local;
getNode(to, cur, r[cur] + r[to], local, used);
ans -= getAns(local, r[cur]);
for (int j = 0; j < local.size(); j ++) {
total.push_back(local[j]);
}
Center::init();
Center::getRoot(to, cur, local.size(), used);
ans += work(Center::root);
}
}
return ans += getAns(total, r[cur]);
} ll p[N], a[N]; int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
while (cin >> n >> K) {
Edge::init();
Center::init();
mset(r, 0);
mset(used, 0);
for (int i = 0; i < K; i ++) {
scanf("%I64d", p + i);
}
for (int i = 1; i <= n; i ++) {
scanf("%I64d", a + i);
for (int j = 0; j < K; j ++) {
ll cur = p[j];
while (a[i] % cur == 0) {
r[i][j] ++;
if (r[i][j] == 3) r[i][j] = 0;
cur *= p[j];
}
}
}
int u, v;
for (int i = 1; i < n; i ++) {
scanf("%d%d", &u, &v);
Edge::addEdge(u, v);
Edge::addEdge(v, u);
}
Center::getRoot(1, 0, n, used);
cout << work(Center::root) << endl;
}
return 0;
}

  

[hdu4670 Cube number on a tree]点分治的更多相关文章

  1. HDU4670 cube number on a tree(点分治+三进制加法)

    The country Tom living in is famous for traveling. Every year, many tourists from all over the world ...

  2. HDU4670 Cube number on a tree 树分治

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

  3. 【点分治】【map】【哈希表】hdu4670 Cube number on a tree

    求树上点权积为立方数的路径数. 显然,分解质因数后,若所有的质因子出现的次数都%3==0,则该数是立方数. 于是在模意义下暴力统计即可. 当然,为了不MLE/TLE,我们不能存一个30长度的数组,而要 ...

  4. hdu 4670 Cube number on a tree(点分治)

    Cube number on a tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/ ...

  5. HDU 4670 Cube number on a tree ( 树的点分治 )

    题意 : 给你一棵树 . 树的每一个结点都有一个权值 . 问你有多少条路径权值的乘积是一个全然立方数 . 题目中给了你 K 个素数 ( K <= 30 ) , 全部权值都能分解成这k个素数 思路 ...

  6. HDU 4670 Cube number on a tree

    divide and conquer on tree. #include <map> #include <vector> #include <cstdio> #in ...

  7. Square Number & Cube Number

    Square Number: Description In mathematics, a square number is an integer that is the square of an in ...

  8. CodeChef - PRIMEDST Prime Distance On Tree 树分治 + FFT

    Prime Distance On Tree Problem description. You are given a tree. If we select 2 distinct nodes unif ...

  9. 【BZOJ-1468】Tree 树分治

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

随机推荐

  1. Apache solr velocity模块 漏洞复现

    0x01 Solr简单介绍 Solr是建立在Apache Lucene ™之上的一个流行.快速.开放源代码的企业搜索平台. Solr具有高度的可靠性,可伸缩性和容错能力,可提供分布式索引,复制和负载平 ...

  2. PHP文件包含漏洞(利用phpinfo)复现

    0x01 简介 PHP文件包含漏洞中,如果找不到可以包含的文件,我们可以通过包含临时文件的方法来getshell.因为临时文件名是随机的,如果目标网站上存在phpinfo,则可以通过phpinfo来获 ...

  3. 使用vue.js封装一个包含图片的跑马灯组件

    初衷: 学习完Vuejs后,来准备练习仿写一下老东家的门户页面,主要是为了熟悉一下常用插件的使用,比如video.js,wow.js,swiper等等:而其中涉及到一个包含图片跑马灯组件,大概长这样( ...

  4. [WPF] 考古Expression Web:微软当年最漂亮的WPF软件

    1. 什么是Expression Web Expression Studio是微软在2007年推出的一套针对设计师的套件,其中包含专业的设计工具和新技术,可以弹性且自由地将设计方案转为实际--无论设计 ...

  5. <algorithm>中sort()函数的用法

    先说一下,本篇文章我没有讲sort()实现排序的原理,我写在另一篇文章中了,如果想了解的话,可以看一下,附上链接:https://www.cnblogs.com/buanxu/p/12772700.h ...

  6. Asynchronous Disk I/O Appears as Synchronous on Windows

    Summary File I/O on Microsoft Windows can be synchronous or asynchronous. The default behavior for I ...

  7. Spring5参考指南: BeanWrapper和PropertyEditor

    文章目录 BeanWrapper PropertyEditor BeanWrapper 通常来说一个Bean包含一个默认的无参构造函数,和属性的get,set方法. org.springframewo ...

  8. Scala教程之:面向对象的scala

    文章目录 面向对象的scala Unified Types Classes Traits 面向对象的scala 我们知道Scala是一种JVM语言,可以合java无缝衔接,这也就大大的扩展了scala ...

  9. 突然地心血来潮,为 MaixPy( k210 micropython ) 添加看门狗(WDT) C 模块的开发过程记录,给后来的人做开发参考。

    事情是前几天群里有人说做个看门狗不难吧,5分钟的事情,然后我就怼了几句,后来才发现,原来真的没有看门狗模块鸭. 那好吧,那我就写一下好了,今天是(2020年4月30日)想着最后一天了,不如做点什么有价 ...

  10. QQ网站的源代码

    链接:https://pan.baidu.com/s/1mqetTbauKTI0KJOaU8wW5A 提取码请加QQ:2669803073获取 声明:仅供学习,切勿用于其他用途