Fibonacci Tree(最小生成树,最大生成树)

Fibonacci Tree

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

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
 给了一个无向图..每个边要么是白的.要么是黑的..问能否构造一个生成树..让白边在生成树的个数为fibonacci数...
题解:
这个题就是求一遍最小的生成树,
求一遍最大的生成树
两个中间是不是有斐波那契数
最后还有判一下联通;
kruskal;
代码:
 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAXN=;
struct Node {
int s,e,c;
};
Node dt[MAXN];
int pre[MAXN];
int M,t1,N;
int cmp1(Node a,Node b){
return a.c<b.c;
}
int cmp2(Node a,Node b){
return a.c>b.c;
}
/*int cmp1(const void *a,const void *b){
if((*(Node *)a).c<(*(Node *)b).c)return -1;
else return 1;
}
int cmp2(const void *a,const void *b){
if((*(Node *)a).c>(*(Node *)b).c)return -1;
else return 1;
}*/
int find(int x){
return pre[x]= x==pre[x]?x:find(pre[x]);
}
bool merge(Node a){
if(!pre[a.s])pre[a.s]=a.s;
if(!pre[a.e])pre[a.e]=a.e;
int f1,f2;
f1=find(a.s);f2=find(a.e);
if(f1!=f2){
pre[f1]=f2;
t1++;
if(a.c)return true;
}
return false;
}
int kruskal(){int tot=;
t1=;
for(int i=;i<M;i++){
if(merge(dt[i]))tot++;
}
if(t1==N)return tot;
else return -;
}
bool fp[MAXN];
void gf(){
int a,b,c=;
memset(fp,false,sizeof(fp));
a=;b=;
fp[a]=fp[b]=true;
while(c<MAXN){
c=a+b;
fp[c]=true;
a=b;
b=c;
}
}
int main(){
int T,s1,s2,ans,flot=;
scanf("%d",&T);
while(T--){
flot++;
memset(pre,,sizeof(pre));
scanf("%d%d",&N,&M);
for(int i=;i<M;i++){
scanf("%d%d%d",&dt[i].s,&dt[i].e,&dt[i].c);
}
// qsort(dt,M,sizeof(dt[0]),cmp1);
sort(dt,dt+M,cmp1);
s1=kruskal();
//qsort(dt,M,sizeof(dt[0]),cmp2);
sort(dt,dt+M,cmp2);
memset(pre,,sizeof(pre));
s2=kruskal();
//printf("%d %d\n",s1,s2);
gf();
ans=;
if(s1<||s2<){
printf("Case #%d: No\n",flot);
continue;
}
//for(int i=0;i<100;i++)printf("fp[%d]=%d ",i,fp[i]);puts("");
if(s1>s2){
int q=s1;
s1=s2;
s2=q;
}
for(int i=s1;i<=s2;i++){
if(fp[i])ans=;
}
if(ans)printf("Case #%d: Yes\n",flot);
else printf("Case #%d: No\n",flot);
}
return ;
}
上一篇:Adobe Photoshop、Adobe Illustrator、Bohemian的Sketch、Figma比较


下一篇:用Photoshop处理图片使背景透明