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. MyEclipse打包可运行的jar包

    详细步骤: Export... -> java -> Runnable JAR file Launch configuration:选择main方法所在的文件/类 Export desti ...

  2. HDU 5375 Gray code(2015年多校联合 动态规划)

    题目连接 : 传送门 题意: 给定一个长度为的二进制串和一个长度为n的序列a[],我们能够依据这个二进制串得到它的Gray code. Gray code中假设第i项为1的话那么我们就能够得到a[i] ...

  3. 实现一个类似360的button

    通过改写一个buttonst类,实现360效果的button. 主要可以完成:frame,hover,face效果,并且支持menu,tooltips 1)派生新的类QButton.添加虚函数,设置自 ...

  4. [jzoj 4722] [NOIP2016提高A组模拟8.21] 跳楼机 解题报告 (spfa+同余)

    题目链接: http://172.16.0.132/senior/#main/show/4722 题目: DJL为了避免成为一只咸鱼,来找srwudi学习压代码的技巧.Srwudi的家是一幢h层的摩天 ...

  5. HTML5学习笔记(二):用于构建页面的语义元素

    1.语义元素 大多数HTML5语义元素的用途是标识页面中的一个内容区块,为标注的内容赋予额外的含义,不真正做任何事. 2.使用语义元素的原因 容易修改和维护: 无障碍性(现代Web设计的一个重要主题) ...

  6. IE6 css fixed

    .fixed-top{position:fixed;bottom:auto;top:0px;} .fixed-bottom{position:fixed;bottom:0px;top:auto;} . ...

  7. [NOIP补坑计划]NOIP2013 题解&做题心得

    场上预计得分:100+100+100+100+100+60=560(省一分数线410) 五道傻逼题+一道大搜索题…… 题解: D1T1 转圈游戏 题面 水题送温暖~ #include<algor ...

  8. vue-cli3+typescript+路由懒加载报错问题

    vue-cli3的版本是3.4.1 出现的情况是网页显示正常,但是终端一直提示找不到模块: 如果去掉路由懒加载的方式,就没有报错: 原因是以前我们习惯直接写文件名而不加后缀, 现在使用ts时就需要写v ...

  9. BZOJ 2154/2693 Crash的数字表格/jzptab (莫比乌斯反演)

    题目大意:求$\sum_{i=1}^{n}\sum_{j=1}^{m}lcm(i,j)$的和 易得$\sum_{i=1}^{n}\sum_{j=1}^{m}\frac{ij}{gcd(i,j)}$ 套 ...

  10. 仿射变换(Affine Transformation)

    转自:https://www.cnblogs.com/bnuvincent/p/6691189.html http://www.cnblogs.com/ghj1976/p/5199086.html 变 ...