题意:给一个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. PHP-fpm 远程代码执行漏洞(CVE-2019-11043)复现

    简介 9 月 26 日,PHP 官方发布漏洞通告,其中指出:使用 Nginx + php-fpm 的服务器,在部分配置下,存在远程代码执行漏洞.并且该配置已被广泛使用,危害较大. 漏洞概述 Nginx ...

  2. eclipse 集成git工具

    1.eclipse git插件下载 打开Eclipse,然后点击Help>Install New Software>Add name:git location:http://downloa ...

  3. 美的PDF转换成Word转换器完全免费

    下载地址:百度网盘提取码:02ap 安装破解步骤:先安装主程序,末尾是full结尾的,安装完成后不要打开软件,然后接着安装破解补丁,即可破解成功! 需要的老铁们直接拿去用吧,亲测好用!有配套的功能强大 ...

  4. 点击表头取下标&js时间转时间戳

    1.Date.parse(new Date("2017-7-31")); 2.$("th").eq(this.cellIndex);  // 3.end($ar ...

  5. [转]探索 Android 内存优化方法

    前言 这篇文章的内容是我回顾和再学习 Android 内存优化的过程中整理出来的,整理的目的是让我自己对 Android 内存优化相关知识的认识更全面一些,分享的目的是希望大家也能从这些知识中得到一些 ...

  6. VR全景视图 Google VrPanoramaView

    2019独角兽企业重金招聘Python工程师标准>>> 一.背景简介 Welcome to VR at Google 进入Google VR主页,发现官方给我们提供了两套解决观看VR ...

  7. JAVA第一次blog总结

    JAVA第一次blog总结 0.前言 大一下学期我们开展了OPP这门课程,这也是我们第一次接触到JAVA.与上学期我们在学校里学C语言不同的是,这学期由于疫情原因我们是以网课的方式在学习.在学习中我发 ...

  8. CentOS 7 编译错误解决方法集合

    解决 error: the HTTP XSLT module requires the libxml2/libxslt 错误 yum -y install libxml2 libxml2-dev yu ...

  9. Blockchain

    一.中心化 中心化原则是我们日常比较常见的支付手段. 科普文章喜欢用网购举例: 1.你在某宝支付了一件商品,钱先到马云爸爸手中,通知商家发货: 2.商家发货,你收货后确认无误,点击确认收货: 3.马云 ...

  10. C++编程入门题目--No.4

    题目: 输入某年某月某日,判断这一天是这一年的第几天? 程序分析: 以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天. #incl ...