HDU 4786(最小生成树 kruskal)
来源:互联网 发布:淘宝怎么装修手机店铺 编辑:程序博客网 时间:2024/06/13 03:47
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4786
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
Source
2013 Asia Chengdu Regional Contest
题意:
N个顶点,M条边,每条边或为白色或为黑色( 1 or 0 ),
问有没有用是斐波那契数的数目的白色边构成一棵生成树。
PS:
其实说是并查集更靠谱一点的酱紫!
首先判断整个图是否是连通的,若不连通则直接输出No。
接下来首先仅讨论白边,不要黑边,看最多能加入多少条白边,使得不存在环。这样我们得到了能加入白边的最大值max。(就是所有生成树里白边数量的最大值)。
接下来同理仅讨论黑边,这样我们可以得到可加入白边的最小值min,(也可以认为是所有生成树中白边的最小值)。
然后我们只要判断这两个值之间是否存在斐波那契数就行了。
代码如下:
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;const int maxn = 100017;struct node{ int u, v; int c;} a[maxn];int f[maxn], Fib[maxn];int n, m;int findd(int x){ return x==f[x] ? x : f[x]=findd(f[x]);}int kruskal(int sign){ int k = 0; //sort(a,a+m,cmp); for(int i = 0; i <= n; i++) { f[i] = i; } for(int i = 1; i <= m; i++) { if(a[i].c != sign) { int f1 = findd(a[i].u); int f2 = findd(a[i].v); if(f1 != f2) { f[f1] = f2; k++; } } } return k;}void init(){ Fib[0] = 1, Fib[1] = 2; for(int i = 2; ; i++) { Fib[i] = Fib[i-1]+Fib[i-2]; if(Fib[i] > maxn) break; }}int main(){ int t; int cas = 0; init(); scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i = 1; i <= m; i++) { scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].c); } int ans = kruskal(2); if(ans != n-1)//不能形成树 { printf("Case #%d: No\n",++cas); continue; } int maxx = kruskal(0); int minn = n-1-kruskal(1); int flag = 0; for(int i = 0; ; i++) { if(Fib[i] >=minn && Fib[i]<=maxx) { flag = 1; break; } if(Fib[i] > maxx) { break; } } if(flag) { printf("Case #%d: Yes\n",++cas); } else { printf("Case #%d: No\n",++cas); } } return 0;}
2 0
- HDU 4786(最小生成树 kruskal)
- 最小生成树(kruskal) hdu 1233
- hdu 1301(最小生成树kruskal)
- HDU-4786 Fibonacci Tree(最小生成树[Kruskal])
- HDU 4786(最小生成树kruskal+路径压缩)
- HDU 4786 Fibonacci Tree 最小生成树 kruskal算法
- HDU 4786 Fibonacci Tree kruskal最小生成树应用
- hdu 1863 畅通工程 (最小生成树kruskal 算法)
- hdu 2988(kruskal求最小生成树)
- HDU 1301 Jungle Roads (最小生成树)kruskal
- HDU 1301 Jungle Roads (Kruskal求最小生成树)
- HDU-1162 Eddy's picture(最小生成树[Kruskal])
- HDU 1301 Jungle Roads(最小生成树Kruskal)
- HDU 1233 还是畅通工程(最小生成树Kruskal)
- hdu 1863 畅通工程(最小生成树,kruskal)
- HDU 5253 最小生成树(kruskal)+ 并查集
- HDU 1879 继续畅通工程(最小生成树Kruskal)
- HDU 5624 KK's Reconstruction(最小生成树-Kruskal)
- java环境变量配置
- LeetCode OJ 之 Remove Element (删除指定元素)
- 23种设计模式之--简单工厂模式
- hdu1540 线段树区间合并
- Sum Root to Leaf Numbers
- HDU 4786(最小生成树 kruskal)
- 给予下列一串字符串a,b,c,ab,bb,cb,ac,bc,cc,aab,bab,cab,abb,bbb,cbb...通过编程实现以上字符串实现,并要求有相应字符串的位置信息,假设a的位置为0
- lower_bound()返回值
- QT常用知识
- Win8 下的android 环境的搭建
- gitcafe for Eclipse的环境配置
- redis复制
- 【struts2】拦截器的工作模拟实现
- c#连接SQL Server 数据库的几种方法