HDU 4786 Fibonacci Tree (minimum spanning trees)

Source: Internet
Author: User

Fibonacci TreeTime limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2487 Accepted Submission (s): 796


Problem Description Coach Pang is interested in Fibonacci numbers when Uncle Yang wants him to doing some on Spann ing Tree. So Coach Pang decides to solve the following problem:
Consider a bidirectional graph G with N vertices and M edges. All edges is painted into the either white or black. Can we find a Spanning Tree with some positive Fibonacci number of white edges?
(Fibonacci number is defined as 1, 2, 3, 5, 8, ...)
Input the first line of the input contains an integer T, the number of test cases.
For each test case, the first line contains the integers n (1 <= n <=) and M (0 <= M <= 105).
Then M lines follow, each contains three integers u, V (1 <= u,v <= N, u<> v) and C (0 <= C <= 1), Indic Ating an edge between U and V with a color C (1 for white and 0 for black).
Output a line ' case #x: s ' for each test case. X is the case number and s are either "Yes" or "No" (without quotes) representing the answer to the problem.
Sample Input
24 41 2 12 3 13 4 11 4 05 61 2 11 3 11 4 11 5 13 5 14 2 1

Sample Output
Case #1: yescase #2: No



Test Instructions:give you an empty picture, some of which are white, some sides are black, ask if you have a life
into a tree and its white side is a number in the Fibonacci sequence.
Ideas:because the weight of the edge is white is 1, the weight of black is 0. That is, on the basis of the original image to find a
the smallest spanning tree and the largest spanning tree again. Assuming that the total weight of the smallest spanning tree is min, the maximum
The total weight of the spanning tree is Max, so we know that the smallest number of white edges in the spanning tree is min, the maximum white
the number of sides is Max. Then we just have to decide if there is a Fibonacci number between the two values.
The above conclusion is to prove that the number of white edges between Min and Max can be.
Cause: The largest spanning tree is stripped of the max-min white edge, and the maximum spanning tree becomes
max-min+1Connected blocks , and these connected blocks already have min strips of white edges, so this
max-min+1 a connected blockcan be connected by the black edge of the Max-min bar. Put this max-min+1
a connected block to see the max-min+1 point, that is, these points can be all through the white or black edge connected together.
It can be concluded that these points can also be connected by n white edges and M-bar black edges (m+n = max-min).

That is, the viable white edge number is ans = min+n, since 0 <= n <=max-min, so Min <= ans <=max.


#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace std;const int maxn=100010;const int n=30;struct edge{int u,v,val;} a[maxn];int id[maxn],f[n]= {0,1,2},n,m,t;bool CMP1 ( Edge P,edge Q) {return p.val<q.val;} BOOL Cmp2 (Edge P,edge q) {return p.val>q.val;}    int Find (int x) {if (x!=id[x]) Id[x]=find (id[x]); return id[x];}    void input () {scanf ("%d%d", &n,&m); for (int i=0; i<m; i++) scanf ("%d%d%d", &a[i].u,&a[i].v,&a[i].val);}    int Kru () {for (int i=0; i<maxn; i++) id[i]=i;    int res=0,num=0;        for (int i=0; i<m; i++) {int P=find (A[I].U), Q=find (A[I].V);            if (p!=q) {id[p]=q;        Res+=a[i].val;    }} for (int i=1; i<=n; i++) if (Find (i) ==i) num++;    if (num>1) return-1; return res;}    void Solve (int co) {int l,r;    Sort (A,A+M,CMP1);    L=kru ();    Sort (A,A+M,CMP2);    R=kru (); if (L==-1 | | r==-1) {PRINTF ("Case #%d:no\n", CO);    return;                } else {for (int i=1; i<n; i++) {if (f[i]>=l && f[i]<=r) {                printf ("Case #%d:yes\n", CO);            return;    }} printf ("Case #%d:no\n", CO);    }}int Main () {for (int i=3; i<n; i++) f[i]=f[i-1]+f[i-2];    scanf ("%d", &t);        for (int co=1; co<=t; co++) {input ();    Solve (CO); } return 0;}



HDU 4786 Fibonacci Tree (minimum spanning trees)

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.