题意:
问你在一个无向图中是否存在一个生成树,权值为斐波拉契数。
思路:
生成树的权值范围必定在最小生成树和最大生成树的权值范围之间, 因此用 kruskal 分别求出最大最小生成树权值后,然后二分判断 是否是FIB数,或者用STL都可以。
(有段时间没做题了,做做水题找下感觉)
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a));
#define lowbit(x) x&-x;
#define debugint(name,x) printf("%s: %d\n",name,x);
#define debugstring(name,x) printf("%s: %s\n",name,x);
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-6;
const int maxn = 1e5+5;
const int mod = 1e9+7;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,m;
int fib[105];
struct node{
int x,y,w;
}p[maxn];
int fa[maxn];
int find(int x){
return x == fa[x]? x:fa[x] = find(fa[x]);
}
void init(){
for(int i = 0; i <= n; i++) fa[i] = i;
}
int solve(){
int ans = 0, cnt = 0;
for(int i = 1; i <= m; i++){
int x = find(p[i].x);
int y = find(p[i].y);
int w = p[i].w;
if(x != y){
ans += w;
fa[x] = y;
cnt++;
}
if(cnt == n-1) break;
}
return cnt == n-1?ans:-1;
}
bool check(int a,int b){
if(a == -1 || b == -1) return false;
for(int i = a; i <= b; i++){
int id = lower_bound(fib+1, fib+30+1, i) - fib;
//printf("%d\n",id);
if(fib[id] != i) continue;
return true;
}
return false;
}
int main() {
int T;
int ca = 1;
scanf("%d",&T);
fib[1] = 1, fib[2] = 2;
for(int i = 3; i <= 30; i++){
fib[i] = fib[i-1] + fib[i-2];
//if(fib[i] > 1e6) break;
}
while(T--){
scanf("%d%d",&n,&m);
for(int i = 1; i <= m; i++){
scanf("%d%d%d", &p[i].x, &p[i].y, &p[i].w);
}
sort(p+1,p+m+1,[](node a,node b){return a.w < b.w;});
init();
int ans1 = solve();
//printf("%d\n",ans1);
for(int i = 1; i <= m; i++){
p[i].w *= -1;
}
sort(p+1,p+m+1,[](node a,node b){return a.w < b.w;});
init();
int ans2 = solve();
//printf("%d\n",-ans2);
bool ok = check(ans1, -ans2);
if(ok) printf("Case #%d: Yes\n",ca++);
else printf("Case #%d: No\n",ca++);
}
}