HDU 4786 Fibonacci Tree



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 <= 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
求个最大和最小生成树,只要中间有斐波那契数就是yes
#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF / 3;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
const int read() 
{
	char ch = getchar();
	while (ch<'0' || ch>'9') ch = getchar();
	int x = ch - '0';
	while ((ch = getchar()) >= '0'&&ch <= '9') x = x * 10 + ch - '0';
	return x;
}
int T, n, m, x[N], y[N], z[N], a[N];
int fa[N], g[N], l, r, cas = 0, flag;

bool cmp(int x, int y)
{
	return z[x] < z[y];
}

int check()
{
	return upper_bound(g, g + 30, r) - lower_bound(g, g + 30, l);
}

int get(int x)
{
	return fa[x] == x ? x : fa[x] = get(fa[x]);
}

int main()
{
	g[0] = 1; g[1] = 2; rep(i, 2, 30) g[i] = g[i - 1] + g[i - 2];
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d%d", &n, &m);
		rep(i, 1, m) scanf("%d%d%d", &x[i], &y[i], &z[i]), a[i] = i;
		sort(a + 1, a + m + 1, cmp);
		l = r = 0;
		flag = n - 1;
		rep(i, 1, n) fa[i] = i;
		rep(i, 1, m)
		{
			int fx = get(x[a[i]]), fy = get(y[a[i]]);
			if (fx == fy) continue;
			fa[fx] = fy;	l += z[a[i]];
		}
		rep(i, 1, n) fa[i] = i;
		per(i, m, 1)
		{
			int fx = get(x[a[i]]), fy = get(y[a[i]]);
			if (fx == fy) continue;
			fa[fx] = fy;	r += z[a[i]];	flag--;
		}
		printf("Case #%d: %s\n", ++cas, !flag && check() ? "Yes" : "No");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值