【Fibonacci Tree】(最小生成树排序 2013icpc成都)

探讨在带有黑白边的双向图中,寻找含有斐波那契数白色边的生成树。通过优先选择不同颜色边的策略,确定生成树中白色边的数量范围,并判断该范围是否包含斐波那契数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

  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 <= 10 5) and M(0 <= M <= 10 5). 
  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

解题报告:

在刚上来的时候没有看明白这个题目的意思,以为是全部由白色边去构建生成树的,wa了两发,然后去看题目,发现了题目的意思是,看看是否存在有斐波那契数的白色边数的生成树存在,咱们不可能去暴力的尝试每一条边的取舍顺序,因为边数太大了,所以就开始考虑极端情况,那么就是优先去选择黑色的边去使用,然后最后不足以后再去考虑白色边,这个时候使用的白色边数是最小的,然后去考虑另一个极端情况,就是优先去使用白色边,然后不足再去考虑黑色边,这个时候的白色边数的最大的,说明构建成生成树中白色的边数是介于两者之间的,因为咱们可以替换某一条白色边用黑色边,在这个题目中我错了18次,刚开始两次是没有读明白题目,之后的是斐波那契数列爆数据范围了,最后的硬核错误是边数的数组范围开小了,一直错误,自闭中,就一直去寻找过程是否存在错误,没有去细看数组的范围之类的,最后在考虑重新写的时候,发现了这个硬核错误。以后一定要注意啦!

ac代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;

const int maxn=30;
int feib_num[maxn];
struct node{
	int x;
	int y;
	int color;
}edge[100100];
void db_Onion()
{
	feib_num[0]=0;
	feib_num[1]=1;
	for(int i=2;i<=maxn;i++)
	{
		feib_num[i]=feib_num[i-1]+feib_num[i-2];
	}	
}
int T,n,m;
int far[100001];
int find(int x)
{
	if(far[x]==x)
		return far[x];
	else 
		return far[x]=find(far[x]);
}
bool merge(int a,int b)
{
	int pa=find(a);
	int pb=find(b);
	if(pa==pb)
		return false;	
	else
	{
		far[pa]=pb;
		return true;
	}
}
void init()
{
	for(int i=1;i<=n;i++)
		far[i]=i;
}

bool cmp1(node a,node b)
{
	return a.color>b.color;
}
bool cmp2(node a,node b)
{
	return a.color<b.color;
}
int main()
{
	db_Onion();
	scanf("%d",&T);
	int kase=0;
	while(T--)
	{
		scanf("%d%d",&n,&m);
		int a,b,c;
		int cnt=0;
		for(int i=0;i<m;i++)
		{
			scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].color);	
		} 
		int cnt1=0,cnt2=0;
		int w1=0,w2=0;
		init();
		sort(edge,edge+m,cmp1);
		for(int i=0;i<m;i++)
		{
			if(merge(edge[i].x,edge[i].y))
			{
				cnt1++;
				if(edge[i].color==1)
					w1++;
			}
		}
		if(cnt1!=n-1)
		{
			printf("Case #%d: No\n",++kase);
			continue;
		}
		init();
		sort(edge,edge+m,cmp2);
		for(int i=0;i<m;i++)
		{
			if(merge(edge[i].x,edge[i].y))
			{
				cnt2++;
				if(edge[i].color==1)
					w2++;
			}
		}
		
//		printf("minx: %d  maxx:%d\n",w2,w1);
		bool flag=false;
		if(w1!=-1)
		{
			for(int i=1;i<maxn;i++)
			{
				if(feib_num[i]>=w2&&feib_num[i]<=w1)
				{
					flag=1;
					break;
				}
			}
		}
		printf("Case #%d: ",++kase);
		if(flag)
			printf("Yes\n");
		else
			printf("No\n");
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值