[hdu4670 Cube number on a tree]点分治
题意:给一个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]点分治的更多相关文章
- HDU4670 cube number on a tree(点分治+三进制加法)
The country Tom living in is famous for traveling. Every year, many tourists from all over the world ...
- HDU4670 Cube number on a tree 树分治
人生的第一道树分治,要是早点学我南京赛就不用那么挫了,树分治的思路其实很简单,就是对子树找到一个重心(Centroid),实现重心分解,然后递归的解决分开后的树的子问题,关键是合并,当要合并跨过重心的 ...
- 【点分治】【map】【哈希表】hdu4670 Cube number on a tree
求树上点权积为立方数的路径数. 显然,分解质因数后,若所有的质因子出现的次数都%3==0,则该数是立方数. 于是在模意义下暴力统计即可. 当然,为了不MLE/TLE,我们不能存一个30长度的数组,而要 ...
- 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/ ...
- HDU 4670 Cube number on a tree ( 树的点分治 )
题意 : 给你一棵树 . 树的每一个结点都有一个权值 . 问你有多少条路径权值的乘积是一个全然立方数 . 题目中给了你 K 个素数 ( K <= 30 ) , 全部权值都能分解成这k个素数 思路 ...
- HDU 4670 Cube number on a tree
divide and conquer on tree. #include <map> #include <vector> #include <cstdio> #in ...
- Square Number & Cube Number
Square Number: Description In mathematics, a square number is an integer that is the square of an in ...
- 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 ...
- 【BZOJ-1468】Tree 树分治
1468: Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1025 Solved: 534[Submit][Status][Discuss] ...
随机推荐
- redis的安装(ubuntu版本)
1.使用apt-get命令进行安装 安装gcc依赖 root@yatces-virtual-machine:~# apt-get update root@yatces-virtual-machine: ...
- tortoise 设置beyond Compare比较工具
1.桌面右击tortoiseSVN->setting->Diff Viewer面板,选择external,选中beyond Compare路径
- get 获取方式练习题及dom基础
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- awd平台搭建
1.先是使用 https://github.com/m0xiaoxi/AWD_CTF_Platform 这个平台搭建 这个平台很好用,是python脚本自动搭建,基本不需要怎么更改,自带了四道题的源码 ...
- redis: Zset有序集合类型(七)
存值:zadd myset 1 one 取值:zrange myset 0 -1 127.0.0.1:6379> zadd myset 1 one #存值 分值为1 (integer) 1 12 ...
- ISO及安全业务,机制
ISO 应用层 为应用软件提供接口,使应用程序能够使用网络服务. 各种应用程序协议如HTTP(Web),Telnet(远程控制),FTP(文本传输) 表示层 数据的交换格式.数据加密解密.数据的压缩解 ...
- Net core项目实战篇01---EFCore CodeFirs For Mysql 数据库初始化
从今天开始我们用Net Core进行项目实战,采用微服务构架,因此你会看到我各模块开始都是用的web api.项目中的代码直接可以复制.费话不多说,现在就来跟我一起开始吧! 1.打开VS2017—&g ...
- 霍夫变换(Hough Transform)
霍夫变换是图像处理中从图像中识别几何形状的基本方法之一,应用很广泛,也有很多改进算法.最基本的霍夫变换是从黑白图像中检测直线(线段). 我们先看这样一个问题: 设已知一黑白图像上画了一条直线,要求出这 ...
- Spring Boot filter
在Spring Boot中自定义filter 本文我们将会讲解如何在Spring Boot中自定义filter并指定执行顺序. 定义Filter很简单,我们只需要实现Filter接口即可,同时我们可指 ...
- cut,xargs,sort,tr,rename命令解析
cut 文件内容查看 显示行中的指定部分,删除文件中指定字段 显示文件的内容,类似于下的type命令. 语法: cut(选项)(参数) 选项: -b:仅显示行中指定直接范围的内容: -c:仅显示行中指 ...