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. 使用U盘引导安装CentOS

    一.制作linux引导盘 1. 格式化U盘:格式成FAT32格式 2. 安装syslinux https://www.kernel.org/pub/linux/utils/boot/syslinux/ ...

  2. 使用PinYin4j,获取汉字的拼音字母

    需要导入的文件 <!-- 引入pinyin4J的依赖 --> <dependency> <groupId>com.belerweb</groupId> ...

  3. 14.3-ELK重难点总结和整体优化配置

    本文收录在Linux运维企业架构实战系列 做了几周的测试,踩了无数的坑,总结一下,全是干货,给大家分享~ 一.elk 实用知识点总结 1.编码转换问题(主要就是中文乱码) (1)input 中的cod ...

  4. 二十六、MySQL 临时表

    MySQL 临时表 MySQL 临时表在我们需要保存一些临时数据时是非常有用的.临时表只在当前连接可见,当关闭连接时,Mysql会自动删除表并释放所有空间. 临时表在MySQL 3.23版本中添加,如 ...

  5. php短网址生成算法

    <?php //短网址生成算法 class ShortUrl { //字符表 public static $charset = "0123456789ABCDEFGHIJKLMNOPQ ...

  6. 多线程并发测试(apache ad)

    1.配置 ThreadPoolTaskExecutor bean   <?xml version="1.0" encoding="UTF-8"?> ...

  7. Django配置邮箱登录

    1.settings下配置 # AUTH 方法(支持邮箱登录) AUTHENTICATION_BACKENDS = ('users.views.CustomBackend',) 2.views下逻辑如 ...

  8. C++从键盘读入数组并存储

    C++从键盘读取任意长度的数组,现总结如下: //读取指定长度的数组 int main() { int n = 0; cin >> n; vector<int> p(n); f ...

  9. HDU - 5017 Ellipsoid(模拟退火)

    题意 给一个三维椭球面,求球面上距离原点最近的点.输出这个距离. 题解 模拟退火. 把\(z = f(x, y)\)函数写出来,这样通过随机抖动\(x\)和\(y\)坐标就能求出\(z\). 代码 / ...

  10. [Poj3133]Manhattan Wiring (插头DP)

    Description 题目大意:给你个N x M(1≤N, M≤9)的矩阵,0表示空地,1表示墙壁,2和3表示两对关键点.现在要求在两对关键点之间建立两条路径,其中两条路径不可相交或者自交(就是重复 ...