Poj-1737 connected graph ***

Source: Internet
Author: User
1 /*
2
3 [transfer] http://hi.baidu.com/accplaystation/blog/item/51417bdca982bea4cc116660.html
4
5 This question is said to have two ideas:
6
71-reduce the total number of disconnections.
8
9 The total number of solutions is 2 ^ (C (n, 2). The number of unconnected solutions can be considered as follows:
10
11 when there are K points connected to point 1, the number of the solution is C (n-1, k) * F (k + 1), other n-k-1 can be connected to any edge between the points,
Number of schemes is 2 ^ (C (n-k-1, 2), so the number of such schemes is C (n-1, k) * F (k + 1) * 2 ^ (C (n-k-1, 2.
12
13 so we can get the recursive formula:
14
15 F (n) = 2 ^ (C (n, 2)-sum (C (n-1, k) * F (k + 1) * 2 ^ (C (n-k-1, 2) | 0 <= k <n)
16
17 The advantage of this idea lies in its simplicity and clarity, but its disadvantage lies in the complexity of programming. to calculate the power of addition, subtraction, and multiplication with high precision, the power of 2 is also very large,
So someone ignores it ......
18

192 -- directly calculate the number of connected solutions.
20
21. consider removing point 1, point 2, and set K points in the connected block where point 2 is located. The K points and the remaining n-k points are in one connected block respectively,
The number of solutions is F (k) * F (n-k), point 2 needs to take the K-1 points in the points of points 1 and 2 to form a connected block, therefore, the number of solutions is C (n-2, k-1 ),
The total K points and the remaining n-k-1 points except point 1 must be connected through point 1, that is, the K points and point 1 have at least one EDGE connection,
The number of such schemes is 2 ^ K-1, so there is a total of F (k) * F (n-k) * C (n-2, k-1) * (2 ^ k-1.
22
23. Therefore, the recursive formula is as follows:
24
25 F (n) = sum (f (k) * F (n-k) * C (n-2, k-1) * (2 ^ k-1) | 1 <= k <n ).
26
27 although this idea is difficult to understand, I do not know if I have explained it clearly ...... However, it greatly reduces the programming burden and only requires high-precision addition and multiplication,
The power of 2 is only 50. My program is solved by thinking 2.
28
29. I have fixed my thinking, and the rest is the issue of high precision. Of course, for Java, it's just a little too much ......
30
31 core code:
32 */
33 void calc (int n)
34
35 {
36
37 int K;
38
39 ans [N]. Len = 1;
40
41 memset (ANS [N]. Num, 0, sizeof (ANS [N]. Num ));
42
43 large tmpa, tmpb, tmpc, tmpd;
44
45 for (k = 1; k <n; ++ K)
46
47 {
48
49 lcopy (ANS [N], tmpa );
50
51 lmul (ANS [K], ANS [n-k], tmpb );
52
53 lmul (tmpb, C [N-2] [k-1], tmpc );
54
55 lmulone (tmpc, ptwo [k]-1, tmpd );
56
57 Ladd (tmpa, tmpd, ANS [N]);
58
59}
60
61}

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.