最大生成树与最小生成树
来源:互联网 发布:python游戏开发教程 编辑:程序博客网 时间:2024/05/16 17:06
hdu4786
Fibonacci Tree
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1127 Accepted Submission(s): 324
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
题意:给出一个无向连通图,边有两种颜色黑色和白色,黑色的权值是0,白色的权值是1,问是否存在一棵树,该树的边权和是斐波那契数;
分析:首先求出最小生成树(权值和s1)和最大生成树(权值和s2),则一定存在所有的可能(s)满足s1<=s<=s2,只要在s1和s2范围内有斐波那契数,就行,至于怎样理解,也很好说,其实可以这样来理解,对于一棵树由于黑色和白色边两种,我们在任意一种状态,我们可以取出一条黑边然后用一条白边来取代这条黑边并且保证这棵树是联通的。
程序:
#include"stdio.h"#include"string.h"#include"stdlib.h"#define M 200000struct st{ int u,v,w;}edge[M];int f[M];int cmp1(const void *a,const void *b){ return (*(struct st*)a).w-(*(struct st *)b).w;}int cmp2(const void *a,const void *b){ return (*(struct st*)b).w-(*(struct st *)a).w;}int finde(int x){ if(x!=f[x]) f[x]=finde(f[x]); return f[x];}void make(int a,int b){ int x=finde(a); int y=finde(b); if(x!=y) f[x]=y;}int main(){ int h[44]; h[1]=1; h[2]=2; int i; for(i=3;i<=30;i++) h[i]=h[i-1]+h[i-2]; int w; scanf("%d",&w); int kk=1; while(w--) { int n,m; scanf("%d%d",&n,&m); for(i=1;i<=n;i++) f[i]=i; for(i=0;i<m;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); make(a,b); edge[i].u=a; edge[i].v=b; edge[i].w=c; } int k=0; for(i=1;i<=n;i++) { if(f[i]==i) k++; } if(k>1) { printf("Case #%d: No\n",kk++); continue; } qsort(edge,m,sizeof(edge[0]),cmp1); for(i=1;i<=n;i++) f[i]=i; int sum1=0; for(i=0;i<m;i++) { int u=edge[i].u; int v=edge[i].v; if(finde(u)!=finde(v)) { sum1+=edge[i].w; make(u,v); } } qsort(edge,m,sizeof(edge[0]),cmp2); for(i=1;i<=n;i++) f[i]=i; int sum2=0; for(i=0;i<m;i++) { int u=edge[i].u; int v=edge[i].v; if(finde(u)!=finde(v)) { sum2+=edge[i].w; make(u,v); } } //printf("%d %d\n",sum1,sum2); int flag=0; for(i=1;i<=28;i++) { if(h[i]>=sum1&&h[i]<=sum2) flag++; } if(flag) printf("Case #%d: Yes\n",kk++); else printf("Case #%d: No\n",kk++); }}
0 0
- 最大生成树与最小生成树
- hdu 4786 最小生成树与最大生成树
- 最小/最大生成树笔记
- POJ 3522 最大边与最小边差值最小的生成树(最小生成树的性质)
- 图---生成树与最小生成树
- UESTC 1635 最大最小生成树
- 最小生成树的最大边poj2395
- bzoj2561: 最小生成树 最大流
- poj2395(最小生成树最大边)
- poj 2377 最大生成树 最小生成树的变形
- Civil and Evil Engineer(最小生成树+最大生成树)
- poj 1797 Heavy Transportation 最小生成树 最大生成树
- hdoj 4786 Fibonacci Tree 【最小生成树&&最大生成树】
- hdoj 4786 Fibonacci Tree 【最小生成树&&最大生成树】
- POJ 3522 最大边与最小边差值最小的生成树
- POJ3522Slim Span(最大边与最小边差值最小的生成树)
- Slim Span 求一颗生成树 使得最大边与最小边的差值最小
- 最小生成生成树计数
- maven 仓库 地址。 ---
- HDU_4519 郑厂长系列故事——体检
- 从抽象谈起(三):AOP编程和ASP.NET MVC
- LeetCode Edit Distance
- hdoj2056 Rectangles
- 最大生成树与最小生成树
- MySQL语句之or/and
- 管式输出
- Mybatis插件-mybatipse
- 树状数组
- 对比iOS网络组件:AFNetworking 和 ASIHTTPRequest
- ASP.NET MVC3 异常处理
- Window命令集锦
- 【”IT程序猿”是怎样练成的系列】之提升内驱力的7大秘籍