zoukankan      html  css  js  c++  java
  • hdoj 4786 Fibonacci Tree【并查集+最小生成树(kruskal算法)】

    Fibonacci Tree

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2915    Accepted Submission(s): 931


    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
    题意:两条边之间1代表是白边,0代表是黑边,求是否存在一棵最小树使它的边中有Fibonacci 数列( 1, 2, 3, 5, 8, ... )中
            数条白边(最小树中边可有白边可有黑边)
     
    题解:利用打表将Fibonacci 数列存在数组fib[]中先将边按照由白到黑排序求出生成一棵最小树最多需要白边多少条max;再将边按
             照有黑到白排序求出生成一棵最小树最少需要白边多少条min,如果存在Fibonacci 数列中一个数使min<=fib[i]<=max则输
             出yes否则输出no(如果无法生成一棵树也输出no)
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #define MAX 100010
    using namespace std;
    struct recode
    {
    	int beg;
    	int end;
    	int bian;
    }s[MAX];
    bool cmp1(recode a,recode b)
    {
    	return a.bian>b.bian;
    }
    bool cmp2(recode a,recode b)
    {
    	return a.bian<b.bian;
    }
    int set[MAX];
    int fib[MAX];
    void biao()
    {
    	int i,j;
    	fib[1]=1;
    	fib[2]=2;
    	for(i=3;fib[i]<MAX;i++)
    	{
    		fib[i]=fib[i-1]+fib[i-2];
    	}
    }
    int find(int fa)
    {
    	int t;
    	int ch=fa;
    	while(fa!=set[fa])
    	fa=set[fa];
    	while(ch!=fa)
    	{
    		t=set[ch];
    		set[ch]=fa;
    		ch=t;
    	}
    	return fa;
    }
    void mix(int x,int y)
    {
    	int fx,fy;
    	fx=find(x);
    	fy=find(y);
    	if(fx!=fy)
    	set[fx]=fy;
    }
    int main()
    {
    	int n,m,j,i,t;
    	scanf("%d",&t);
    	int k=1;
    	biao();
    	while(t--)
    	{
    		scanf("%d%d",&n,&m);
    		int sum=0;
    		for(i=0;i<m;i++)
    			scanf("%d%d%d",&s[i].beg,&s[i].end,&s[i].bian);
    		int min=0,max=0;
    		for(i=0;i<=n;i++)
    			set[i]=i;
    		sort(s,s+m,cmp1);
    		for(i=0;i<m;i++)
    		{
    			//printf("%d %d # ",s[i].beg,s[i].end);
    			if(find(s[i].beg)!=find(s[i].end))
    			{
    				mix(s[i].beg,s[i].end);
    				if(s[i].bian==1)
    				max++;
    			}
    		} 
    	//	printf("
    ");
    	//	printf("%d 
    ",max);
    		for(i=0;i<=n;i++)
    			set[i]=i;
    		sort(s,s+m,cmp2);
    		for(i=0;i<m;i++)
    		{
    		//	printf("%d %d # ",s[i].beg,s[i].end);
    			if(find(s[i].beg)!=find(s[i].end))
    			{
    				mix(s[i].beg,s[i].end);
    				if(s[i].bian==1)
    				min++;
    			}
    		}	
    	//	printf("
    ");
    	//	printf("%d 
    ",min);
    		printf("Case #%d: ",k++);
    		int wrong=0;
    		int mis=0;
    		for(i=1;i<=n;i++)
    		{
    			if(set[i]==i)
    			wrong++;
    			if(wrong>1)
    			{
    				mis=1;
    				break;
    			}
    		}
    		if(mis)
    		{
    			printf("No
    ");
    			continue;
    		}
    		int ok=0;
    		for(i=1;fib[i]<=m;i++)
    		{
    			if(fib[i]>=min&&fib[i]<=max)
    			{  
    				printf("Yes
    ");
    				ok=1;
    				break;
    			}	
    		}
    		if(!ok)
    		printf("No
    ");
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    PHP的跨域问题
    Linux常用命令
    搭建vagrant开发环境
    php 循环从数据库分页取数据批量修改数据
    Mac搭建pyhton+selenium+pycharm实现web自动化测试
    Mac 安装python 3.*新版本的详细步骤
    pycharm最新版本激活码(永久有效) python安装教程
    PHP操作redis 【转】
    php将图片存储在阿里云oss存储上
    Mac系统终端命令行不执行命令解决方法(command not found)
  • 原文地址:https://www.cnblogs.com/tonghao/p/4686131.html
Copyright © 2011-2022 走看看