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. AngularJS 历经实例

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  2. linux主机状态检测方式

    之前写过一个简单的脚本检测当前网段中主机状态的脚本,内容如下: #! /bin/bash #ping check host status trap "exit" 2 sping() ...

  3. 在kali上安装谷歌浏览器

    在kali上安装谷歌浏览器的时候,遇到了很多问题,经过不懈努力,终于解决,现在把方法总结一下,希望对遇到同样问题的人能有一定帮助.这是给最白的小白参考的,大牛勿喷哈. 首先去这个网站www.googl ...

  4. Java中的finally

    基础用法: int f1() { try{ return 1; }finally { System.out.println("finall执行"); } } @Test publi ...

  5. Keywords Search HDU - 2222 ( ac自动机)模版题

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  6. 并查集:HDU5326-Work(并查集比较简单灵活的运用)

    Work HDU原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=5326 Time Limit: 2000/1000 MS (Java/Others) M ...

  7. hadoop 启动or运行mr错误

    hadoop 错误:Incorrect configuration: namenode address dfs.namenode.servicerpc-address or dfs.namenode. ...

  8. A JavaScript Image Gallery

    childNodes property:  The childNodes property is a way of getting information about the children of ...

  9. synchronized 基本用法

    常见三种使用方式 1)普通同步方法,锁是当前实例:2)静态同步方法,锁是当前类的Class实例,Class数据存在永久代中,是该类的一个全局锁:3)对于同步代码块,锁是synchronized括号里配 ...

  10. 关于tree节点的刷新

    1.刷新节点分为刷新整个树和刷新指定节点 (1)刷新整个树 $("#tree").tree("reload"); (2)刷新指定节点(方法:传入需要刷新节点的父 ...