链接:戳这里
Fibonacci Tree
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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
题意:
给出n个点m条边的无向图,边可以染成黑色或白色。
问是否在图上存在一颗生成树使得生成树上的白色边的数目是一个斐波那契数。
思路:
算出一颗生成树上最少的白边数目L。
算出一颗生成树上最多的白边数目R。
判断区间[L,R]上是否存在斐波那契数
因为在数目L->R的生成树里,肯定每个区间里的白边条数值val,都可以通过将黑边换成白边得到。所以不需要知道换哪些边
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
struct edge{
int u,v,w;
}e[200100];
int n,m;
int fa[100100];
int Find(int x){
if(x!=fa[x]) fa[x]=Find(fa[x]);
return fa[x];
}
int Fib[30];
int num[200100],vis[200100];
int main(){
Fib[1]=1;Fib[2]=2;
vis[1]=vis[2]=1;
for(int i=3;;i++){
Fib[i]=Fib[i-1]+Fib[i-2];
vis[Fib[i]]=1;
if(Fib[i]>100000) break;
}
for(int i=1;i<=100000;i++) num[i]=num[i-1]+vis[i];
int T;
scanf("%d",&T);
for(int cas=1;cas<=T;cas++){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) fa[i]=i;
for(int i=1;i<=m;i++) scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
printf("Case #%d: ",cas);
int k=0;
for(int i=1;i<=m;i++){
int X=Find(e[i].u);
int Y=Find(e[i].v);
if(X!=Y) {
fa[X]=Y;
k++;
}
if(k==n-1) break;
}
if(k!=n-1){
printf("No\n");
continue;
}
for(int i=1;i<=n;i++) fa[i]=i;
int L=0,R=0;
for(int i=1;i<=m;i++){
if(e[i].w==1) continue;
int X=Find(e[i].u);
int Y=Find(e[i].v);
if(X!=Y) {
fa[X]=Y;
L++;
}
}
L=n-1-L;
for(int i=1;i<=n;i++) fa[i]=i;
for(int i=1;i<=m;i++){
if(e[i].w==0) continue;
int X=Find(e[i].u);
int Y=Find(e[i].v);
if(X!=Y) {
fa[X]=Y;
R++;
}
}
if(num[R]-num[L-1]==0) printf("No\n");
else printf("Yes\n");
}
return 0;
}