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

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 <= 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
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
 
Recommend
We have carefully selected several similar problems for
you:  6263 6262 6261 6260 6259 
 
 
和昨天ysy讲的那道题差不多
而且这道题在题目中直接给提示了——》黑边为0,白边为1
这样的话我们做一个最小生成树和一个最大生成树
如果在这两个值的范围内有斐波那契数,就说明满足条件
 
简单证明:
对于最小生成树来说,任意删除一条边,并加入一条没有出现过的边,这样的话权值至多加1,边界为最大生成树
 
 
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=1e6+,INF=1e9+;
inline char nc()
{
static char buf[MAXN],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,MAXN,stdin),p1==p2)?EOF:*p1++;
}
inline int read()
{
char c=nc();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=nc();}
while(c>=''&&c<=''){x=x*+c-'';c=nc();}
return x*f;
}
struct node
{
int u,v,w;
}edge[MAXN];
int num=;
inline void AddEdge(int x,int y,int z)
{
edge[num].u=x;
edge[num].v=y;
edge[num].w=z;num++;
}
int N,M;
int fib[MAXN];
int fa[MAXN];
int comp1(const node &a,const node &b){return a.w<b.w;}
int comp2(const node &a,const node &b){return a.w>b.w;}
int find(int x)
{
if(fa[x]==x) return fa[x];
else return fa[x]=find(fa[x]);
}
void unionn(int x,int y)
{
int fx=find(x);
int fy=find(y);
fa[fx]=fy;
}
int Kruskal(int opt)
{
if(opt==) sort(edge+,edge+num,comp1);
else sort(edge+,edge+num,comp2);
int ans=,tot=;
for(int i=;i<=num-;i++)
{
int x=edge[i].u,y=edge[i].v,z=edge[i].w;
if(find(x) == find(y)) continue;
unionn(x,y);
tot++;
ans=ans+z;
if(tot==N-) return ans;
}
}
int main()
{
#ifdef WIN32
freopen("a.in","r",stdin);
#else
#endif
int Test=read();
fib[]=;fib[]=;
for(int i=;i<=;i++) fib[i]=fib[i-]+fib[i-];
int cnt=;
while(Test--)
{
N=read(),M=read();num=;
for(int i=;i<=N;i++) fa[i]=i;
for(int i=;i<=M;i++)
{
int x=read(),y=read(),z=read();
AddEdge(x,y,z);
AddEdge(y,x,z);
}
int minn=Kruskal();
for(int i=;i<=N;i++) fa[i]=i;
int maxx=Kruskal();
bool flag=;
for(int i=;i<=;i++)
if(minn <= fib[i] && fib[i] <= maxx)
{printf("Case #%d: Yes\n",++cnt);flag=;break;}
if(flag==) printf("Case #%d: No\n",++cnt);
}
return ;
}

HDU 4786Fibonacci Tree(最小生成树)的更多相关文章

  1. hdu 5909 Tree Cutting [树形DP fwt]

    hdu 5909 Tree Cutting 题意:一颗无根树,每个点有权值,连通子树的权值为异或和,求异或和为[0,m)的方案数 \(f[i][j]\)表示子树i中经过i的连通子树异或和为j的方案数 ...

  2. hdu Constructing Roads (最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 /************************************************* ...

  3. HDU 5044 Tree(树链剖分)

    HDU 5044 Tree field=problem&key=2014+ACM%2FICPC+Asia+Regional+Shanghai+Online&source=1&s ...

  4. [HDU 5293]Tree chain problem(树形dp+树链剖分)

    [HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...

  5. HDU 4408 Minimum Spanning Tree 最小生成树计数

    Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  6. HDU 2489 Minimal Ratio Tree 最小生成树+DFS

    Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  7. HDU 4786 Fibonacci Tree 最小生成树

    Fibonacci Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4786 Description Coach Pang is intere ...

  8. HDU 4757 Tree(可持久化Trie+Tarjan离线LCA)

    Tree Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total Su ...

  9. 数据结构与算法分析–Minimum Spanning Tree(最小生成树)

    给定一个无向图,如果他的某个子图中,任意两个顶点都能互相连通并且是一棵树,那么这棵树就叫做生成树(spanning tree). 如果边上有权值,那么使得边权和最小的生成树叫做最小生成树(MST,Mi ...

随机推荐

  1. linux下tomcat开机启动简单配置

    1.个人标记 caicongyang http://blog.csdn.net/caicongyang 2.正文 在linux文件/etc/rc.d/rc.local的末尾加入例如以下行就可以: ex ...

  2. 亚马逊AWS学习——多网络接口下配置EC2实例连接公网的一个“bug”

    转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/47667627 本文出自[我是干勾鱼的博客] 之前在<亚马逊AWS学习--E ...

  3. kentico version history and upgrade

    Version history Kentico 10: November 30, 2016 Kentico 9: November 24, 2015 Kentico 8.2: January 6, 2 ...

  4. ES不设置副本是非常脆弱的,整个文章告诉了你为什么

    Delaying Shard Allocation As discussed way back in Scale Horizontally, Elasticsearch will automatica ...

  5. POJ 2188线段树求逆序对

    题目给的输入是大坑,算法倒是很简单-- 输入的是绳子的编号wire ID,而不是上(或下)挂钩对应下(或上)挂钩的编号. 所以要转换编号,转换成挂钩的顺序,然后再求逆序数. 知道了这个以后直接乱搞就可 ...

  6. PSSecurityException之PowerShell权限设置

    Windows下PowerShell默认的权限级别是Restricted,不允许执行PS脚本(即.ps1文件).如果在Restricted权限级别下运行,会得到错误信息: .\XXXX.ps1 : F ...

  7. (转载) 据说年薪30万的Android程序员必须知道的

    据说年薪30万的Android程序员必须知道的帖子 标签: android 2015-03-12 16:52 28705人阅读 评论(14) 收藏 举报 Android中国开发精英 目前包括: And ...

  8. 51nod 1021 石子归并 (动态规划 简单代码)

    题目: 思路:动态规划,递推式子 dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + sum[j] - sum[i-1]);     dp[i][j]表示 ...

  9. hdu 1257/1800 - 贪心,dp

    1257题目链接 一个序列划分子序列,每个子序列都是非增序列,问最少分成几个子序列 1800题目链接 一堆数分组,每组内数据严格递减,问最少分几组 -------------------------- ...

  10. 运维派 企业面试题4&5 创建10个 用户 ; ping探测主机是否在线

    Linux运维必会的实战编程笔试题(19题) 企业面试题4: 批量创建10个系统帐号oldboy01-oldboy10并设置密码(密码为随机8位字符串). #!/bin/bash # ;i<=; ...