Hdu 4786 Fibonacci Tree (minimum and maximum spanning Tree)

Source: Internet
Author: User

Question:

N vertices and M edges. Each edge is connected to two vertices u and v, and has a weight value of c, which is a non-zero value.

Ask if we can form N vertices into a spanning tree, and the edge weight of this tree is a number of ononacii. (Fibonacii number = 1, 2, 3, 5, 8 ....)

 

Ideas:

If a tree can be generated. The minimum spanning tree and maximum spanning tree are available. Assume that the minimum mst p and maximum mst q have been generated.

Replace P with one edge to get another spanning tree. The edge weight is 1 larger than the edge weight of P if it is not equal to P. (Because the edge value is non-zero, that is, one ). Similarly, we can get Q.

So all the values between the edge right of P and the edge right of Q can be obtained. Therefore, you can determine whether the number of fiber-ACII instances exists.

 

Code:

Struct node {int u, v, c;} edge [100005]; bool cmp (node a, node B) {return. c <B. c;} int fa [100005]; int T, n, m; int findFa (int x) {return fa [x] = x? X: fa [x] = findFa (fa [x]);} int kruskal1 () {rep (I, 1, n) fa [I] = I; int res = 0; rep (I, 1, m) {int fx = findFa (edge [I]. u); int fy = findFa (edge [I]. v); if (fx! = Fy) {fa [fx] = fy; res + = edge [I]. c ;}} int tx = findFa (1); rep (I, 2, n) if (findFa (I )! = Tx) return-1; return res;} int kruskal2 () {rep (I, 1, n) fa [I] = I; int res = 0; rep2 (I, m, 1) {int fx = findFa (edge [I]. u); int fy = findFa (edge [I]. v); if (fx! = Fy) {fa [fx] = fy; res + = edge [I]. c ;}} int tx = findFa (1); rep (I, 2, n) if (findFa (I )! = Tx) return-1; return res;} bool isFibo [100005]; void FiboD () {mem (isFibo, false); int a = 1, B = 2; isFibo [1] = isFibo [2] = true; for (;) {int t = a + B; a = B, B = t; if (t> 100000) break; isFibo [t] = true;} int main () {// freopen ("test. in "," r ", stdin); cin> T; FiboD (); rep (t, 1, T) {scanf (" % d ", & n, & m); rep (I, 1, m) scanf ("% d", & edge [I]. u, & edge [I]. v, & edge [I]. c); sort (edge + 1, edge + 1 + m, cmp); int mins = kruskal1 (); int maxs = kruskal2 (); printf ("Case # % d: ", t); if (mins =-1 | maxs =-1) puts (" No "); else {bool flag = false; rep (I, mins, maxs) if (isFibo [I]) {flag = true; break;} if (flag) puts ("Yes"); else puts ("No ");}} // fclose (stdin );}

 

Hdu 4786 Fibonacci Tree (minimum and maximum spanning Tree)

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.