博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU4786:Fibonacci Tree(并查集)
阅读量:5757 次
发布时间:2019-06-18

本文共 2093 字,大约阅读时间需要 6 分钟。

Fibonacci Tree

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


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
 

Source
题意:给n个点,m条边,每条边要么黑色要么白色,判断有无一种方案连通所有点且白色边数目为斐波拉契数。

思路:先排序一次将黑色边放前面,求一次最小生成树用到的白色边数,再排序一次将白色边放前面求一次最小生成树用到的白色边数,最后判断两数之间有无斐波拉契数即可。

# include 
# include
# define MAXN 100000using namespace std;int pre[MAXN+1], f[25]={1,1};struct node{ int a, b, c;}edge[MAXN+1];bool cmp1(node a, node b){ return a.c < b.c;}bool cmp2(node a, node b){ return a.c > b.c;}void init(int n){ for(int i=0; i<=n; ++i) pre[i] = i;}int find(int x){ if(x != pre[x]) pre[x] = find(pre[x]); return pre[x];}int main(){ int t, n, m, cas=1; for(int i=2; i<25; ++i) f[i] = f[i-1] + f[i-2]; scanf("%d",&t); while(t--) { bool flag = false; int road=0, white1=0, white2=0; scanf("%d%d",&n,&m); init(n); for(int i=0; i

转载于:https://www.cnblogs.com/junior19/p/6730004.html

你可能感兴趣的文章
[CareerCup] 1.6 Rotate Image 翻转图像
查看>>
jQuery中$.fn的用法示例介绍
查看>>
Python中的画图初体验
查看>>
Java程序员的日常 —— 响应式导航Demo
查看>>
objective-c内存管理基础
查看>>
sap关于价值串的说法(转载)
查看>>
Migration to S/4HANA
查看>>
sed 对目录进行操作
查看>>
什么是代码
查看>>
洛谷P2891 [USACO07OPEN]吃饭Dining
查看>>
移动端开发单位——rem,动态使用
查看>>
系列文章目录
查看>>
SVG 新手入门
查看>>
手把手教你如何提高神经网络的性能
查看>>
前端布局原理涉及到的相关概念总结
查看>>
递归调用 VS 循环调用
查看>>
通用词向量、句向量研究现状
查看>>
win10安装sdkman(posh-gvm)
查看>>
常用排序算法总结
查看>>
使用sstream读取字符串中的数字(c++)
查看>>