Fibonacci tree (minimum spanning trees, maximum spanning tree)

Source: Internet
Author: User

Fibonacci Tree

Time limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3006 Accepted Submission (s): 966


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 Input2 4 4 1 2 1 2 3 1 3 4 1 1 4 0 5 6 1 2 1 1 3 1 1 4 1 1 5 1 3 5 1 4 2 1

Sample outputcase #1: Yes case #2: The problem is to ask for the smallest spanning tree, to find the largest spanning tree in the middle of two is not the Fibonacci number and finally sentenced to unicom; Kruskal; code:
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <algorithm>5 using namespacestd;6 Const intmaxn=100010;7 structNode {8     ints,e,c;9 };Ten Node DT[MAXN]; One intPRE[MAXN]; A intM,t1,n; - intCmp1 (Node a,node b) { -     returna.c<B.C; the } - intCMP2 (Node a,node b) { -     returnA.c>B.C; - } + /*int cmp1 (const void *a,const void *b) { - if ((* (node *) a) .c< (* (node *) b). c) return-1; + else return 1; A } at int cmp2 (const void *a,const void *b) { - if ((* (node *) a) .c> (* (node *) b). c) return-1; - else return 1; - }*/ - intFindintx) { -     returnpre[x]= x==pre[x]?X:find (pre[x]); in } - BOOLmerge (Node a) { to     if(!pre[a.s]) pre[a.s]=A.S; +     if(!PRE[A.E]) pre[a.e]=A.E; -     intF1,f2; theF1=find (A.S); f2=find (A.E); *     if(f1!=F2) { $pre[f1]=F2;Panax Notoginsengt1++; -         if(A.C)return true; the     } +     return false; A } the intKruskal () {inttot=0; +t1=1; -      for(intI=0; i<m;i++){ $         if(merge (Dt[i])) tot++; $     } -     if(t1==n)returntot; -     Else return-1; the } - BOOLFP[MAXN];Wuyi voidGF () { the     intA,b,c=0; -memset (FP,false,sizeof(FP)); WuA=1; b=2; -fp[a]=fp[b]=true; About      while(c<MAXN) { $c=a+b; -fp[c]=true; -A=b; -b=C; A     } + } the intMain () { -     intt,s1,s2,ans,flot=0; $scanf"%d",&T); the      while(t--){ theflot++; thememset (PRE,0,sizeof(pre)); thescanf"%d%d",&n,&M); -          for(intI=0; i<m;i++){ inscanf"%d%d%d",&dt[i].s,&dt[i].e,&dt[i].c); the         } the        //qsort (dt,m,sizeof (dt[0]), CMP1); AboutSort (dt,dt+m,cmp1); thes1=Kruskal (); the         //qsort (dt,m,sizeof (dt[0]), CMP2); theSort (dt,dt+m,cmp2); +memset (PRE,0,sizeof(pre)); -S2=Kruskal (); the         //printf ("%d%d\n", s1,s2);Bayi GF (); theans=0; the         if(s1<0|| s2<0){ -printf"Case #%d:no\n", flot); -             Continue; the         } the        //for (int i=0;i<100;i++) printf ("fp[%d]=%d", I,fp[i]);p UTS (""); the         if(s1>S2) { the             intq=S1; -s1=S2; theS2=Q; the         } the          for(inti=s1;i<=s2;i++){94             if(Fp[i]) ans=1; the         } the         if(ANS) printf ("Case #%d:yes\n", flot); the         Elseprintf"Case #%d:no\n", flot);98     } About     return 0; -}

Fibonacci tree (minimum spanning trees, 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.