HDU 4786 Fibonacci Tree(最小生成树变式)
来源:互联网 发布:js删除对象元素 编辑:程序博客网 时间:2024/05/09 10:48
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, ... )
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).
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
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
题目大意:给定一个图,里面有的边是白色,剩下的全为黑色。问存不存在一个生成树里面包含的白色边的个数恰为Fibonacci numbers。
看了题解始终没明白。。。题解的解法是白边边权为 1 ,黑边边权为 0 。构造一个最大生成树,用掉的白边数就是所有生成树里白边的最大个数Max。再构造一个最小生树,用掉的白边数就是所有生成树里白边的最小个数Min。若Min~Max之间包含 Fibonacci numbers 就符合题意。据说是因为一条黑边可以被一条白边代替,依然保持图的连通性,所以只要Min ~ Max之间包含 Fibonacci numbers 就OK。依然不明白是为什么。。。求大神告知。生成树用 Kruskal,查找Fibonacci numbers 是打了一个表然后二分找的。
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 1e5 + 5;int p[maxn],f[100],v[maxn],u[maxn],e[maxn],w[maxn];int len = 1;int n,m;bool cmp(int x,int y){ return w[x] < w[y];}void init(){ memset(f,0,sizeof(f)); f[0] = 1,f[1] = 2; while(f[len] <= 100000) ++len,f[len] = f[len - 1] + f[len - 2];}int Find(int x){ return p[x] == x ? x : p[x] = Find(p[x]);}int main(){ int t,x,y,c; scanf("%d",&t); init(); int pre = t; while(t--) { scanf("%d %d",&n,&m); for(int i = 1;i <= m; ++i) e[i] = i; for(int i = 1;i <= m; ++i) scanf("%d %d %d",&u[i],&v[i],&w[i]); sort(e + 1,e + m + 1,cmp); int temp = 0,mi = 0,ma = 0; for(int i = 1;i <= n; ++i) p[i] = i; for(int i = 1;i <= m; ++i) { int now = e[i]; int x = Find(u[now]),y = Find(v[now]); if(x != y) { ++temp; if(w[now] == 1) ++mi; p[y] = x; } } if(temp < n - 1) { printf("Case #%d: No\n",pre - t); continue; } for(int i = 1;i <= n; ++i) p[i] = i; for(int i = m;i >= 1; --i) { int now = e[i]; int x = Find(u[now]),y = Find(v[now]); if(x != y) { if(w[now] == 1) ++ma; p[y] = x; } } temp = lower_bound(f,f + len,mi) - f; printf("Case #%d: ",pre - t); if(f[temp] <= ma) printf("Yes\n"); else printf("No\n"); } return 0;}
阅读全文
0 0
- HDU 4786 Fibonacci Tree(最小生成树变式)
- 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 (最小生成树变形)
- 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(最小生成树--kruskal+并查集)
- Android多进程总结一:生成多进程(android:process属性)
- 程序员应该访问的最佳网站中文版
- 手把手教你实现线性回归模型
- 虚函数解释说明
- 红黑树下
- HDU 4786 Fibonacci Tree(最小生成树变式)
- vc2010创建lib和使用
- 100个GDB小技巧(一)
- vue依赖webpack的环境配置(一)
- 简单对比几个技术博客类网站
- SpringBoot入门(五)数据库操作入门
- C程序的存储区及堆与栈区别
- 计算机网络(2):其他补充
- Java重要的基础语法(一)