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;
    }
    

      

  • 相关阅读:
    crontab
    待重写
    待重写
    多套开发资源使用情况
    待重写
    待重写
    待重写
    docker安装es
    docker run启动时目录映射研究
    rabbitmq第二篇:使用插件实现延迟功能
  • 原文地址:https://www.cnblogs.com/tonghao/p/4686131.html
Copyright © 2011-2022 走看看