[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;
}

  

posted @ 2015-11-12 20:40  jklongint  阅读(334)  评论(0编辑  收藏  举报