Fibonacci Tree

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

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
 
题意:N个顶点,M条边,每条边或为白色或为黑色( 1 or 0 ),问有没有用是斐波那契数的数目的白色边构成一棵生成树
题解:N个顶点构成的生成树有N-1条边 ,首先判断能否能构成生成树,图是否是联通的,无法构成则输出No
先使用所有的黑边 不断加边,看最多能使用多少条黑边使得不形成环 求得白边的使用的数量的下界
然后再使用所有的白边,不断的加边,看最多能使用多少条白边使得不形成环,求得白边使用的数量的上界
然后是否存在斐波那契数载这个区间

我们等于是要证明对于所有在min和max之间的白边数我们都能够达到。

考虑从最小的min开始,我总可以找到一条黑边,使得将它去掉在补上一条白边保持图联通。为什么呢,如果在某一个状态(设白边数为x)下,不存在一条黑边可以被白边代替,那么现在我们把所有黑边去掉,剩下x条白边,那我们知道,x一定等于max,因为若x<max,那么我们在算max的那个步骤中,先将这x条白边加入,还可以在加入max-x条白边使得不存在环,那么这与没有一条黑边可以被白边代替矛盾,所以这就证明了从min到max我都可以达到

 
 #include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
struct node
{
int u,v,c;
} N[];
int fib[];
int fa[];
int t;
int n,m;
int coun;
int find(int root)
{
if(root==fa[root])
return root;
else
return fa[root]=find(fa[root]);
}
void unin(int a,int b)
{
int aa=find(a);
int bb=find(b);
if(aa!=bb)
fa[aa]=bb;
}
void init()
{
for(int i=; i<=n; i++)
fa[i]=i;
}
void fi()
{
fib[]=;
fib[]=;
for(int i=;; i++)
{
fib[i]=fib[i-]+fib[i-];
if(fib[i]>)
{
coun=i;
break;
}
}
}
int kruscal(int exm)
{
init();
int k=;
for(int i=; i<=m; i++)
{
if(N[i].c!=exm)
{
if(find(N[i].u)!=find(N[i].v))
{
k++;
unin(N[i].u,N[i].v);
}
}
}
return k;
}
int main()
{
fi();
while(scanf("%d",&t)!=EOF)
{
for(int j=; j<=t; j++)
{
scanf("%d %d",&n,&m);
for(int i=; i<=m; i++)
scanf("%d %d %d",&N[i].u,&N[i].v,&N[i].c);
printf("Case #%d: ",j);
int zha;
zha=kruscal();//可以使用白边和黑边
if(zha!=(n-))//判环
{
printf("No\n");
continue;
}
int l=n--kruscal();//构成生成树的白边数量的下限
int r=kruscal();// 构成生成树的白边数量的上限
int flag=;
for(int i=; i<coun; i++)//判断是否存在满足条件的fib
{
if(fib[i]>=l&&fib[i]<=r)
{
printf("Yes\n");
flag=;
break; }
}
if(flag==)
printf("No\n");
}
}
return ;
}

HDU 4786 最小生成树变形 kruscal(13成都区域赛F)的更多相关文章

  1. 36th成都区域赛网络赛 hdoj4039 The Social Network(建图+字符串处理)

    这题是某年成都区域赛网络赛的一题. 这题思路非常easy,可是从时间上考虑,不妨不要用矩阵存储,我用的链式前向星. 採用线上查询.利用map对字符串编号,由于非常方便.要推荐的朋友,事实上就是朋友的朋 ...

  2. HDU 4786 Fibonacci Tree (2013成都1006题)

    Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. HDU 4731 Minimum palindrome (2013成都网络赛,找规律构造)

    Minimum palindrome Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. HDU 4786 Fibonacci Tree (2013成都1006题) 最小生成树+斐波那契

    题意:问生成树里能不能有符合菲波那切数的白边数量 思路:白边 黑边各优先排序求最小生成树,并统计白边在两种情况下数目,最后判断这个区间就可以.注意最初不连通就不行. #include <stdi ...

  5. hdu 4786 最小生成树与最大生成树

    /* 题意 :有一些边权值为1和0,判断是否存在一个生成树使得他的总权值为一个斐波那契数. 解法:建立一个最小生成树向里面加权值为1的边替换为0的边,保证原来的联通.因为权值为1,可直接求出最大生成树 ...

  6. hdu 4081 最小生成树变形

    /*关于最小生成树的等效边,就是讲两个相同的集合连接在一起 先建立一个任意最小生成树,这条边分开的两个子树的节点最大的一个和为A,sum为最小生成树的权值和,B为sum-当前边的权值 不断枚举最小生成 ...

  7. HDU 4802 && HDU 4803 贪心,高精 && HDU 4804 轮廓线dp && HDU 4805 计算几何 && HDU 4811 (13南京区域赛现场赛 题目重演A,B,C,D,J)

    A.GPA(HDU4802): 给你一些字符串对应的权重,求加权平均,如果是N,P不计入统计 GPA Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  8. HDU 6229 Wandering Robots(2017 沈阳区域赛 M题,结论)

    题目链接  HDU 6229 题意 在一个$N * N$的格子矩阵里,有一个机器人. 格子按照行和列标号,左上角的坐标为$(0, 0)$,右下角的坐标为$(N - 1, N - 1)$ 有一个机器人, ...

  9. HDU 4737 A Bit Fun 2013成都 网络赛 1010

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4737 题目大意:给定一系列数,F(i,j)表示对从ai到aj连续求或运算,(i<=j)求F(i, ...

随机推荐

  1. 查看电脑是否安装node.js

    打开命令行

  2. PAT (Basic Level) Practise (中文)- 1001. 害死人不偿命的(3n+1)猜想 (15)

    http://www.patest.cn/contests/pat-b-practise/1001 卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那 ...

  3. 谭浩强 c++程序设计第一章课后习题 第7题

    #include <iostream> using namespace std; int main() { int a,b,c; int f(int x,int y,int z);//这是 ...

  4. JavaScript深拷贝与浅拷贝的理解

    个人是这么理解深拷贝和浅拷贝的:就是假设B复制了A,当修改A时,看B是否会发生变化,如果B也跟着变了,说明这是浅拷贝,拿人手短,如果B没变,那就是深拷贝,自食其力. 一起看看我举的浅拷贝栗子: let ...

  5. Android驱动开发读书笔记六

    第六章 Linux 驱动的工作和访问方式是 Linux 的亮点之一,Linux 系统将每一个驱动都映射成一个文件.这些文件称为设备文件或驱动文件,都保存在/dev目录中,由于大多数Linux驱动都有与 ...

  6. 第26题:LeetCode572:Subtree of Another Tree另一个树的子树

    题目描述 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 s 的一个节点和这个节点的所有子孙.s 也可以看做它自身的一棵子树. 示例 1: ...

  7. Uva 组装电脑 12124 - Assemble

    主要运用二分法查找最优解 #include<iostream> #include<string> #include<vector> #include<map& ...

  8. Swiper.js手动滑动之后,不再自动滑动问题

    var swiper = new Swiper('.swiper-container', {        pagination: '.swiper-pagination',        autop ...

  9. tcl之内容

  10. 50 道 CSS 基础面试题及答案

    1 介绍一下标准的CSS的盒子模型?与低版本IE的盒子模型有什么不同的? 标准盒子模型:宽度=内容的宽度(content)+ border + padding + margin 低版本IE盒子模型:宽 ...