zoukankan      html  css  js  c++  java
  • hdu 4786 Fibonacci Tree(最小生成树)

    Fibonacci Tree

    Time 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 while Uncle Yang wants him to do some research on Spanning Tree. So Coach Pang decides to solve the following problem:
      Consider a bidirectional graph G with N vertices and M edges. All edges are painted into 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 two integers N(1 <= N <= 105) 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), indicating an edge between u and v with a color c (1 for white and 0 for black).
     

    Output
      For each test case, output a line “Case #x: s”. x is the case number and s is either “Yes” or “No” (without quotes) representing the answer to the problem.
     

    Sample Input
    2 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 Output
    Case #1: Yes Case #2: No
     

    Source

    2013 Asia Chengdu Regional Contest 

    有n个点,m条边。有黑白之分,问连通全部点时的边中,白边的个数能不能是斐波那契数列中的一个数
    思路:先用白边连图。求出白边的个数,再先用黑边连图,求出白边另个值,。这两个值就是白边个数的取值范围 
    2015,7,30 

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    #define M 100000+10
    struct 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++)//由于边最多有100000条,所以斐波那契数大于这个数时就能够了,55足够了 
    		a[i]=a[i-1]+a[i-2];
    		
    	scanf("%d",&t);
    	while(t--){
    		scanf("%d%d",&n,&m);
    		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
    ");//假设没有连通全部点直接输出No 
    		else{
    			for(i=1;i<50;i++){//注意这个i要从1開始,由于输入1个点0条边时要输出No。。我找了半天错= =+ 
    				if(a[i]>= start && a[i]<=end){
    					ok=1;
    					break;
    				}
    			}
    			if(ok) printf("Yes
    ");
    			else printf("No
    ");
    		}
    	} 
    	return 0;
    }


  • 相关阅读:
    Gallery平滑移动
    HDU 4308 BFS Saving Princess claire_
    css 浏览器兼容性问题解决
    CCPlace,CCFlip*,CCToggleVisibility,CCMoveTo*,CCJumpTo*,CCScale*,CCRotate*,CCSkew*,fade,CCCardinalSp*
    存储过程和输出分辨率表菜单JSON格式字符串
    Vertica变化Local时间到GMT时间
    【codeforces】Codeforces Round #277 (Div. 2) 解读
    Android 百度地图 SDK v3.0.0 (四) 离线地图功能介绍
    计算质数-埃里克森筛法(间隔黄金武器)
    IP地址分类
  • 原文地址:https://www.cnblogs.com/wzjhoutai/p/6761927.html
Copyright © 2011-2022 走看看