HDU 4786(最小生成树 kruskal)
题目链接: pid=4786" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=4786
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, ... )
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).
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
Case #1: Yes
Case #2: No
题意:
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)的更多相关文章
- hdu 2988(最小生成树 kruskal算法)
Dark roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 4786 最小生成树与最大生成树
/* 题意 :有一些边权值为1和0,判断是否存在一个生成树使得他的总权值为一个斐波那契数. 解法:建立一个最小生成树向里面加权值为1的边替换为0的边,保证原来的联通.因为权值为1,可直接求出最大生成树 ...
- HDU 4786 最小生成树变形 kruscal(13成都区域赛F)
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 模板——最小生成树kruskal算法+并查集数据结构
并查集:找祖先并更新,注意路径压缩,不然会时间复杂度巨大导致出错/超时 合并:(我的祖先是的你的祖先的父亲) 找父亲:(初始化祖先是自己的,自己就是祖先) 查询:(我们是不是同一祖先) 路径压缩:(每 ...
- HDU 1233(最小生成树)
HDU 1233(最小生成树 模板) #include <iostream> #include <algorithm> #include <cstdio> usin ...
- 最小生成树——Kruskal与Prim算法
最小生成树——Kruskal与Prim算法 序: 首先: 啥是最小生成树??? 咳咳... 如图: 在一个有n个点的无向连通图中,选取n-1条边使得这个图变成一棵树.这就叫“生成树”.(如下图) 每个 ...
- 【转】最小生成树——Kruskal算法
[转]最小生成树--Kruskal算法 标签(空格分隔): 算法 本文是转载,原文在最小生成树-Prim算法和Kruskal算法,因为复试的时候只用到Kruskal算法即可,故这里不再涉及Prim算法 ...
- HDU 1102 最小生成树裸题,kruskal,prim
1.HDU 1102 Constructing Roads 最小生成树 2.总结: 题意:修路,裸题 (1)kruskal //kruskal #include<iostream> ...
- 最小生成树 kruskal hdu 5723 Abandoned country
题目链接:hdu 5723 Abandoned country 题目大意:N个点,M条边:先构成一棵最小生成树,然后这个最小生成树上求任意两点之间的路径长度和,并求期望 /************** ...
随机推荐
- uncategorized SQLException for SQL []; SQL state [99999]; error code [17004]; 无效的列类型: 1111; nested exception is java.sql.SQLException: 无效的列类型: 1111
WHERE 的条件取值时添加上jdbcType=NUMBER这样的配置 参考[1]:https://blog.csdn.net/fyhjuyol/article/details/45483167
- java反编译工具jad
下好以后解压就可以用了,软件就只有一个exe文件和一个帮助文件.在众多的JAVA反编译工具中,有几种非常著名的工具使用了相同的核心引擎——JAD,其中主要包括:FrontEnd Plus.mDeJav ...
- vue-loader 细节
vue-loader 能根据 .vue 文件,导入一个vue组件.我这里从 vue-cli 的构建项目中抽取了vue-loader 一个小例子出来:vuedemo/demo02 vue-loader ...
- 如何学好C和C++
酷壳上的两篇文章,转载至此,学好C和C++. 我相信,这可能是很多朋友的问题,我以前也有这样的感觉,编程编到一定的时候,发现能力到了瓶颈,既不深,也不扎实,半吊子.比如:你长期地使用Java和.NET ...
- LeetCode(11) Container With Most Water
题目 Given n non-negative integers a1, a2, -, an, where each represents a point at coordinate (i, ai). ...
- php file_get_contents json_decode 输出为NULL
解决办法一:不小心在返回的json字符串中返回了BOM头的不可见字符,某些编辑器默认会加上BOM头,如下处理才能正确解析json数据: $info = json_decode(trim($info,c ...
- Mac系统下VirtualBox装Centos7设置静态IP并连网
用Virtualbox装了三台Centos7,现在需要设置成三台之间可以相互通信,并且三台都可以连外网. 需求如下: 1. 三台内部相互通信 2. 可以上外网 3. 主机可以虚拟机可以相互通信(she ...
- 一个抓取智联招聘数据并存入表格的python爬虫
talk is cheap...show you the code..... import requests import lxml,time,os from bs4 import Beautiful ...
- zoj 2109 FatMouse' Trade
FatMouse' Trade Time Limit: 2 Seconds Memory Limit: 65536 KB FatMouse prepared M pounds of cat ...
- php基础语句 变量 符号
中心主题 标记与注释 // /* */ 输出语句 echo输出 echo可以输出多个字符串,逗号隔开 print输出 print只能输出一个字符串,返回true或false print_r() 可以把 ...