CF 23E (Tree-backpack merge)

Source: Internet
Author: User

Problem 2 tree (tree. cpp/c/pas) [Title Description] L invented a tree-related game (Friendly reminder: the tree is a connected graph without loops ): he deletes any number of edges (which can be 0) from the tree, calculates the product of all connected block sizes after deletion, and L gets so many scores. Your task is to find the maximum score L can get for a given tree. [Input format] the first line is an integer n, indicating the number of nodes in the tree. The next n-1 rows have two integers a [I], B [I] (1 <= a [I], B [I] <= n ), the edge connecting a [I] and B [I. Make sure that the input graph is a tree. Output Format: returns an integer that represents the maximum score that can be obtained by L. [Example input] Example 1: 51 22 33 44 5 Example 2: 81 21 32 42 53 63 76 8 Example 3: 31 21 3 [sample output] Example 1: 6 Example 2: 18 Example 3: 3 [data range] for 10% of data, 1 <= n <= 5; for 30% of data, 1 <= n <= 100; and 30% of data, ensure that data is a chain. For 100% of data, 1 <= n <= 700; f [I] [j] indicates that the connected block of the father of I has the maximum value of j in subtree I. So this is the combination of the tree-like Dp + backpack, and the combination of the backpack, and then the third ...... [Cpp] # include <cstdio> # include <cstring> # include <algorithm> # include <functional> # include <iostream> # include <cstdlib> # include <cmath> using namespace std; # define MAXN (700 + 10) # define ll long # define F (100000000) int n, edge [MAXN * 2], pre [MAXN], next [MAXN * 2], size = 0, son [MAXN]; struct bign {ll a [40]; bign () {memset (a, 0, sizeof (a); a [0] = 1;} bign (int x) {memset (a, 0, sizeof (a); a [0] = 1; a [1] = x;} ll & op Erator [] (const int I) {return a [I];} friend bign operator * (bign a, bign B) {bign c; for (int I = 1; I <= a [0]; I ++) for (int j = 1; j <= B [0]; j ++) {c [I + J-1] + = a [I] * B [j]; if (c [I + J-1]> F) {c [I + j] + = c [I + J-1]/F; c [I + J-1] % = F ;}} c [0] = min (a [0] + B [0], (long) 39); while (! C [c [0] & c [0]> 1) c [0] --; return c;} friend bool operator> (bign a, bign B) {if (a [0]! = B [0]) return a [0]> B [0]; for (int I = a [0], j = B [0]; I> 0; I --, j --) if (a [I]! = B [j]) return a [I]> B [j]; return false;} void print () {printf ("% I64d ", a [a [0]); for (int I = a [0]-1; I --) {printf ("%. 8I64d ", a [I]) ;}} f [MAXN] [MAXN]; bign max (bign a, bign B) {if (a> B) return; return B;} void addedge (int u, int v) {edge [++ size] = v; next [size] = pre [u]; pre [u] = size;} void dfs (int x, int father) {son [x] = 1; // f [x] [1] = 1; f [x] [1] = 1; for (int p = pre [x]; p = next [p]) {int & v = edge [p]; if (V! = Father) {dfs (v, x);/* for (int I = son [x] + son [v]; I> 0; I --) {if (I <son [x]) f [x] [I] = f [x] [I] * son [v]; for (int k = son [v]; k> = 0; k-) if (i-k-1> = 0) f [x] [I] = max (f [x] [I], f [x] [i-k-1] * f [v] [k]);} son [x] + = son [v]; bign maxv = son [v]; for (int k = 0; k <= son [v]-1; k ++) maxv = max (maxv, f [v] [k] * (k + 1); f [x] [0] = f [x] [0] * maxv; */for (int I = son [x]; I --) for (int j = son [v]; j> = 0; j --) f [x] [I + j] = max (f [x] [I + j], f [x] [I] * f [v] [j]); son [x] + = son [v] ;}} f [x] [0] = bign (son [x]); for (int I = 1; I <= son [x]; I ++) f [x] [0] = max (f [x] [0], f [x] [I] * bign (I); return;} int main () {// freopen ("tree. in "," r ", stdin); // freopen (" tree. out "," w ", stdout); scanf (" % d ", & n); memset (pre, 0, sizeof (pre); memset (next, 0, sizeof (next); for (int I = 1; I <n; I ++) {int u, v; scanf ("% d", & u, & v); addedge (u, v); addedge (v, u);} addedge (n + 1, 1); dfs (n + 1, 0 ); // n + 1 is ans/* for (int I = 1; I <= n + 1; I ++) {for (int j = 0; j <= son [I]-1; j ++) {f [I] [j]. print (); printf ("");} printf ("\ n");} * // f [n + 1] [1] = bign (123456789) * bigns (234567899); f [1] [0]. print (); printf ("\ n"); 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.