HDU 4670 Cube number on a tree (tree verbose), hdu4670

Source: Internet
Author: User

HDU 4670 Cube number on a tree (tree verbose), hdu4670

Question: Give You A tree. Each node of the tree has a weight value. The product of how many path weights you have is a full cube.

The title gives you K prime numbers (K <= 30). The ownership values can be divided into these k prime numbers.


Idea: the number of prime factors in a complete cube is a multiple of three, so we only need the number of prime numbers, and we only care about the remainder of the number to three.

Therefore, we can use a long integer to represent the number of prime factors from each node to the root (triple compression ). However, because bitwise operations are faster, I use a four-digit system, that is, each two represents the number of a prime factor.

When merging in the middle, calculate the number that is equal to 0 after the sum, and then use map for statistics.


Note: The pi value in the question cannot be obtained from 0 (how to deal with it for a long time? =)


# Include <stdio. h> # include <string. h ># include <map ># include <algorithm> # pragma comment (linker, "/STACK: 102400000,102400000") using namespace std; # define MAXN 50005 # define INF 0x3f3f3f _ int64 prime [35]; int n, k; struct Tree {struct Edge {int to, nex; Edge () {} Edge (int _ to, int _ nex) {to = _ to; nex = _ nex;} edge [MAXN * 2]; int head [MAXN]; int Index; void init () {memset (head,-1, size Of (head); Index = 0;} void add (int from, int to) {edge [Index] = Edge (to, head [from]); head [from] = Index ++;} tree; inline int get (_ int64 val, int pos) {return (val >>( pos <1 )) & 3;} inline void set (_ int64 & val, int pos, int k) {val | = (_ int64) k <(pos <1) ;:__ int64 add (_ int64 a, _ int64 B) {_ int64 ans = 0; for (int I = 0; I <k; I ++) {int Aa = get (a, I); int bb = get (B, I); aa + = bb; aa % = 3; set (ans, I, aa );} return ans ;}__ int64 fan (_ int64 a) {_ int64 ans = 0; for (int I = 0; I <k; I ++) {int tmp = get (a, I); tmp = 3-tmp; set (ans, I, tmp % 3);} return ans;} bool vis [MAXN]; __int64 val [MAXN] ;__ int64 ans; // The maximum number of subtree whose I node is the root int dp [MAXN]; // int sum [MAXN]; int Sum; int Min, M Inid; void dfs1 (int u, int f) {sum [u] = 1; dp [u] = 0; for (int I = tree. head [u]; ~ I; I = tree. edge [I]. nex) {int ch = tree. edge [I]. to; if (ch = f | vis [ch]) continue; dfs1 (ch, u); sum [u] + = sum [ch]; dp [u] = max (dp [u], sum [ch]) ;}} void dfs2 (int u, int f) {int M = max (dp [u], sum-sum [u]); if (M <Min) {Min = M; Minid = u;} for (int I = tree. head [u]; ~ I; I = tree. edge [I]. nex) {int ch = tree. edge [I]. to; if (ch = f | vis [ch]) continue; dfs2 (ch, u) ;}// return the center of gravity of the tree int getRoot (int u) {// The First Time dfs finds the size of the subtree of all vertices, and the maximum number of subtree followed by child dfs1 (u, 0); Sum = sum [u]; min = 0x3f3f3f; Minid =-1; // calculate the center of gravity for the second dfs request/* If the whole tree calculates the center of gravity, we can only perform the dfs operation, because this is part of the tree, we need dfs to determine which nodes are in the current tree */dfs2 (u, 0); return Minid;} int tot ;__ int64 Val [MAXN]; void getVal (int u, int Fa, _ int64 vv) {Val [tot ++] = add (vv, val [u]); for (int I = tree. head [u]; ~ I; I = tree. edge [I]. nex) {int to = tree. edge [I]. to; if (vis [to] = true) continue; if (to = fa) continue; getVal (to, u, add (vv, val [u]) ;}} map <__ int64, int> mp ;__ int64 cal (int root, _ int64 val, _ int64 root_val) {tot = 0; getVal (root, 0, val); // sort (Val, Val + tot); mp. clear (); _ int64 ans = 0; for (int I = 0; I <tot; I ++) {ans + = mp [add (root_val, fan (V Al [I])]; mp [Val [I] ++;} return ans;} void solve (int u) {// obtain the center of gravity int root = getRoot (u) of the current subtree; // calculate the root result ans + = cal (root, 0, val [root]); if (val [root] = 0) ans ++; vis [root] = true; for (int I = tree. head [root]; ~ I; I = tree. edge [I]. nex) {int ch = tree. edge [I]. to; if (vis [ch]) continue; ans-= cal (ch, val [root], val [root]); solve (ch) ;}} int main () {while (scanf ("% d", & n )! = EOF) {scanf ("% d", & k); for (int I = 0; I <k; I ++) {scanf ("% d ", & prime [I]);} tree. init (); memset (vis, false, sizeof (vis); memset (val, 0, sizeof (val); for (int I = 1; I <= n; I ++) {_ int64 tmp; scanf ("% I64d", & tmp); for (int j = 0; j <k; j ++) {int cnt = 0; while (tmp % prime [j] = 0) {cnt ++; tmp/= prime [j];} cnt % = 3; set (val [I], j, cnt) ;}}for (int I = 0; I <n-1; I ++) {int u, v; scanf ("% d", & u, & v); tree. add (u, v); tree. add (v, u);} ans = 0; solve (1); printf ("% I64d \ n", ans);} return 0 ;}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.