HDU 4786 Fibonacci Tree 最小生成树
来源:互联网 发布:mfc windows pdf下载 编辑:程序博客网 时间:2024/04/28 20:04
题目描述:
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
24 41 2 12 3 13 4 11 4 05 61 2 11 3 11 4 11 5 13 5 14 2 1
Sample Output
Case #1: YesCase #2: No
题目分析:
一个n个点,m条边的图,边有白色(1表示)黑色(0表示)两种情况,问是否存在这样一个生成树,其中白色边(1)的数量是斐波那契数列中的元素?
斐波那契递推公式:
fibo[i]=fibo[i-1]+fibo[i-2];
这道题是判断有无的,所以首先要做的是判断这个图是否是连通图,不是连通图直接No,然后只看白色边,将尽可能多的白色边都连起来看是否能使之成为树,找到最大值maxx,然后看黑色边,可以找到必须加入白色边(使之成为树)的最小值minn,然后在minn和maxx间寻找是否有斐波那契数。(因为白色边权是1,黑色边权是0,所以用Kruskal返回的权值就是白色边数)
代码如下:
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;typedef long long ll;const int INF = 0x3f3f3f3f;const int MAXN=100010;struct sa{ int u,v,c;}mp[MAXN];int fi[30];int f[MAXN];int T;int n,m;int getfibo(){ fi[0]=1,fi[1]=2; for(int i=2; ; i++) { fi[i]=fi[i-1]+fi[i-2]; if(fi[i]>100000) {return i;break;} }}int findfather(int x){ if (x!=f[x]) return f[x]=findfather(f[x]); else return f[x];}int solve(int col){ int num=0; for(int i=1; i<=n; i++)f[i]=i; for(int i=1; i<=m; i++) { if(mp[i].c!=col) { int x=findfather(mp[i].u);int y=findfather(mp[i].v); if(x!=y) { f[x]=y; num++; } } } return num;}int main(){ scanf("%d",&T); for(int t=1; t<=T; t++) { scanf("%d%d",&n,&m); for(int i=1; i<=m; i++) { scanf("%d%d%d",&mp[i].u,&mp[i].v,&mp[i].c); } printf("Case #%d: ",t); int maxx=solve(0); int minn=n-1-solve(1); int tmp=solve(2); if (tmp!=n-1) { printf("No\n");continue; } bool flag=false; for(int i=0; i<=getfibo(); i++) { if (fi[i]>=minn && fi[i]<=maxx) {flag=true;break;} } if (flag) printf("Yes\n"); else printf("No\n"); } return 0;}
- hdu 4786 Fibonacci Tree ( 最小生成树 )
- HDU 4786 Fibonacci Tree 最小生成树
- HDU 4786 Fibonacci Tree 最小生成树 kruskal算法
- HDU - 4786 Fibonacci Tree (最小生成树)
- HDU-#4786 Fibonacci Tree(最大最小生成树)
- hdu 4786 Fibonacci Tree (最小生成树扩展)
- HDU 4786--Fibonacci Tree【并查集,最小生成树】
- hdu 4786 Fibonacci Tree(最小生成树)
- HDU-4786 Fibonacci Tree(最小生成树[Kruskal])
- HDU 4786 Fibonacci Tree(最小生成树)
- hdu 4786 Fibonacci Tree(最小生成树)
- HDU 4786 Fibonacci Tree kruskal最小生成树应用
- hdu 4786 Fibonacci Tree 乱搞 智商题目 最小生成树
- HDU 4786 Fibonacci Tree (最小生成树变形)
- hdu 4786 Fibonacci Tree(最小生成树)
- hdu 4786 Fibonacci Tree(最小生成树变形)
- HDU 4786 Fibonacci Tree(最小生成树变式)
- HDU 4786 Fibonacci Tree 最小生成树 + 最大生成树 (2013成都区域赛 F题)
- Android Studio - 使用Gradle自定义你的BuildConfig
- mybatis抛出ORA-01745: 无效的主机/绑定变量名
- JavaScript split() 方法
- SQL初学学习笔记(二)
- c++_union
- HDU 4786 Fibonacci Tree 最小生成树
- Linux启动流程
- react-router 使用
- UISegmentedControl分段控制 跳转各界面
- Java内存分配
- Reading package lists... Error!解决
- Android Studio中实现图片倒影效果
- The content of element type "package" must match "(result-types?,interceptors?,default-interceptor-r
- 腾讯2016在线模拟笔试