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): 2952 Accepted Submission (s): 947


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

Source

Asia Chengdu Regional Contest

There are n points, M-bars, there are black and white points, asked to connect all points in the edge, the number of white edges can be a number of Fibonacci sequence
Idea: first with white edge even figure, find out the number of white edge, and then with black edge even figure, find out the white edge of another value, these two values is the number of white edge value range
2015,7,30

#include <stdio.h> #include <string.h> #include <algorithm>using namespace std; #define M 100000+ 10struct node{int S,e,val;} Sd[m];int x[m];int A[55];bool cmp1 (node A,node b) {return a.val>b.val;} BOOL CMP2 (node A,node b) {return a.val<b.val;} void Init () {for (int i=0;i<m;i++) x[i]=i;} int find (int k) {if (x[k]==k) return K;x[k]=find (X[k]); return x[k];} int main () {int t,m,n,v=1,i;int num,start,end;a[1]=1; a[2]=2;for (i=3;i<55;i++)//Because the edge has a maximum of 100,000, so the Fibonacci number is greater than this number can be, 55 is sufficient a[i]=a[i-1]+a[i-2];scanf ("%d", &t), while (t--) {scanf ("%d%d", &n,&m), and for (i=0;i<m;i++) scanf ("%d %d%d ", &sd[i].s,&sd[i].e,&sd[i].val); sort (sd,sd+m,cmp2); num=0; Init (); for (i=0;i<m;i++) {int fa=find (SD[I].S); int fb=find (SD[I].E); if (FA!=FB) {x[fa]=fb;if (sd[i].val==1) num++;}} Start=num;sort (SD,SD+M,CMP1); Num=0;init (); for (i=0;i<m;i++) {int fa=find (SD[I].S); int fb=find (SD[I].E); if (fa!= FB) {x[fa]=fb;if (sd[i].val==1) num++;}} End=num;int ok=0;for (i=1;i<=n;i++) {if (Find (i)!=find (1)) {Ok=1;break;}}printf ("Case #%d:", v++); if (OK) printf ("no\n");//If all points are not connected directly output no else{for (i=1;i<50;i++) {//Note this I is starting from 1, Because I want to output no when I enter 1 points, 0 edges, I find a half-day error = =+ if (a[i]>= start && a[i]<=end) {ok=1;break;}} if (OK) printf ("yes\n"), Else printf ("no\n");}} 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.