题目链接:

pid=4786" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=4786

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 )。

问有没实用是斐波那契数的数目的白色边构成一棵生成树。

PS:

事实上说是并查集更靠谱一点的酱紫!

首先推断整个图是否是连通的,若不连通则直接输出No。

接下来首先仅讨论白边。不要黑边,看最多能增加多少条白边。使得不存在环。

这样我们得到了能增加白边的最大值max。(就是全部生成树里白边数量的最大值)。

接下来同理仅讨论黑边,这样我们能够得到可增加白边的最小值min。(也能够觉得是全部生成树中白边的最小值)。

然后我们仅仅要推断这两个值之间是否存在斐波那契数即可了。

代码例如以下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn = 100017;
struct node
{
int u, v;
int c;
} a[maxn];
int f[maxn], Fib[maxn];
int n, m;
int findd(int x)
{
return x==f[x] ? x : f[x]=findd(f[x]);
}
int kruskal(int sign)
{
int k = 0;
//sort(a,a+m,cmp);
for(int i = 0; i <= n; i++)
{
f[i] = i;
}
for(int i = 1; i <= m; i++)
{
if(a[i].c != sign)
{
int f1 = findd(a[i].u);
int f2 = findd(a[i].v);
if(f1 != f2)
{
f[f1] = f2;
k++;
}
}
}
return k;
}
void init()
{
Fib[0] = 1, Fib[1] = 2;
for(int i = 2; ; i++)
{
Fib[i] = Fib[i-1]+Fib[i-2];
if(Fib[i] > maxn)
break;
}
}
int main()
{
int t;
int cas = 0;
init();
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].c);
}
int ans = kruskal(2);
if(ans != n-1)//不能形成树
{
printf("Case #%d: No\n",++cas);
continue;
}
int maxx = kruskal(0);
int minn = n-1-kruskal(1);
int flag = 0;
for(int i = 0; ; i++)
{
if(Fib[i] >=minn && Fib[i]<=maxx)
{
flag = 1;
break;
}
if(Fib[i] > maxx)
{
break;
}
}
if(flag)
{
printf("Case #%d: Yes\n",++cas);
}
else
{
printf("Case #%d: No\n",++cas);
}
}
return 0;
}

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

  1. hdu 2988(最小生成树 kruskal算法)

    Dark roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

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

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

  3. HDU 4786 最小生成树变形 kruscal(13成都区域赛F)

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

  4. 模板——最小生成树kruskal算法+并查集数据结构

    并查集:找祖先并更新,注意路径压缩,不然会时间复杂度巨大导致出错/超时 合并:(我的祖先是的你的祖先的父亲) 找父亲:(初始化祖先是自己的,自己就是祖先) 查询:(我们是不是同一祖先) 路径压缩:(每 ...

  5. HDU 1233(最小生成树)

    HDU 1233(最小生成树 模板) #include <iostream> #include <algorithm> #include <cstdio> usin ...

  6. 最小生成树——Kruskal与Prim算法

    最小生成树——Kruskal与Prim算法 序: 首先: 啥是最小生成树??? 咳咳... 如图: 在一个有n个点的无向连通图中,选取n-1条边使得这个图变成一棵树.这就叫“生成树”.(如下图) 每个 ...

  7. 【转】最小生成树——Kruskal算法

    [转]最小生成树--Kruskal算法 标签(空格分隔): 算法 本文是转载,原文在最小生成树-Prim算法和Kruskal算法,因为复试的时候只用到Kruskal算法即可,故这里不再涉及Prim算法 ...

  8. HDU 1102 最小生成树裸题,kruskal,prim

    1.HDU  1102  Constructing Roads    最小生成树 2.总结: 题意:修路,裸题 (1)kruskal //kruskal #include<iostream> ...

  9. 最小生成树 kruskal hdu 5723 Abandoned country

    题目链接:hdu 5723 Abandoned country 题目大意:N个点,M条边:先构成一棵最小生成树,然后这个最小生成树上求任意两点之间的路径长度和,并求期望 /************** ...

随机推荐

  1. Intel CPU参数查询网站

    链接:https://ark.intel.com/#@Processors

  2. XML 解析 & 特殊字符报错

    在xml文件中,有一些符号是具有特殊意义的,如果直接使用会导致xml解析报错,为了避免错误,我们需要将特殊的字符使用其对应的转义实体进行操作.这些字符如下 <  ==  < >  = ...

  3. phpstudy里升级mysql版本到5.7

    phpstudy里没有地方可以设置mysql数据库,很多人都疑惑在phpstudy里怎么升级mysql数据库版本,本文就教你如何在phpstudy中升级mysql的版本. PhpStudy集成环境中的 ...

  4. 22. SCHEMA_PRIVILEGES

    22. SCHEMA_PRIVILEGES SCHEMA_PRIVILEGES表提供有关schema(数据库)特权的信息.它从mysql.db系统表中获取其值. SCHEMA_PRIVILEGES表有 ...

  5. php S3

    转载自:http://www.cnblogs.com/wangxusummer/p/6398772.html <?php /* * To change this license header, ...

  6. 自定义UDF函数应用异常

    自定义UDF函数应用异常 版权声明:本文为yunshuxueyuan原创文章.如需转载请标明出处: http://www.cnblogs.com/sxt-zkys/QQ技术交流群:299142667 ...

  7. NYOJ 237 游戏高手的烦恼

    游戏高手的烦恼时间限制:1000 ms | 内存限制:65535 KB难度:5描述有一位传说级游戏高手,在闲暇时间里玩起了一个小游戏,游戏中,一个n*n的方块形区域里有许多敌人,玩家可以使用炸弹炸掉某 ...

  8. Python 和 Flask实现RESTful services

    使用Flask建立web services超级简单. 当然,也有很多Flask extensions可以帮助建立RESTful services,但是这个例实在太简单了,不需要使用任何扩展. 这个we ...

  9. NIUDAY 11.23 北京站抢票啦 | 看 AI 落地行业 享 AI 时代红利

    2018 年是见证「奇迹」的一年.AI 从多年的热门话题中开始走下神坛,逐渐深入到了各个行业,加速经济结构优化及行业智慧化升级,AI 已不再是难以企及的神话而是可触摸的美好未来. 政策支持加上资本推动 ...

  10. msp430项目编程06

    msp430中项目---设计扫描键盘 1.扫描键盘工作原理 2.电路原理说明 3.代码(显示部分) 4.代码(键盘驱动) 5.项目总结 msp430项目编程 msp430入门学习