Fibonacci Tree

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

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
题意:n个点m条边,其中有一些边是白边,一些边是黑边,问是否存在一棵树上的白边数量是斐波拉契数列里面的某个数.
题解:很巧妙的思想,先按白边排序将白边最多的树选出来,然后黑边排序将白边最少的树选出来。然后如果有斐波拉契数在两棵树的大小中间(因为如果存在的话,是可以通过删边得到的),如果存在,就Ok,还要判一下连通分量。

#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <queue>
using namespace std;
const int N = ;
int father[N];
struct Edge{
int u,v,color;
}edge[N];
int _find(int x){
if(x!=father[x]) {
father[x] = _find(father[x]);
}
return father[x];
} int n,m;
int cmp(Edge a,Edge b){
return a.color>b.color;
}
int cmp1(Edge a,Edge b){
return a.color<b.color;
}
int kruskal(){
int cost=;
for(int i=;i<m;i++){
int x=_find(edge[i].u);
int y=_find(edge[i].v);
if(x!=y){
father[x] = y;
cost+=edge[i].color;
}
}
return cost;
}
bool vis[N];
void init(){
memset(vis,false,sizeof(vis));
int a=,b=;
vis[]=true,vis[] =true;
while(a+b<N){
vis[a+b]=true;
swap(a,b);
b = a+b;
}
}
int main()
{
int tcase;
scanf("%d",&tcase);
int t = ;
init();
while(tcase--){
scanf("%d%d",&n,&m);
for(int i=;i<m;i++){
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].color);
}
for(int i=;i<=n;i++) father[i] = i;
sort(edge,edge+m,cmp);
int maxn = kruskal();
int ans = ;
for(int i=;i<=n;i++){
if(father[i]==i) ans++;
if(ans>) break;
}
if(ans>) {
printf("Case #%d: No\n",t++);
continue;
}
for(int i=;i<=n;i++) father[i] = i;
sort(edge,edge+m,cmp1);
int minn = kruskal();
ans = ;
for(int i=;i<=n;i++){
if(father[i]==i) ans++;
if(ans>) break;
}
if(ans>) {
printf("Case #%d: No\n",t++);
continue;
}
//printf("%d %d\n",minn,maxn);
bool flag = false;
for(int i=minn;i<=maxn;i++){
if(vis[i]){
flag = true;
break;
}
}
if(flag) printf("Case #%d: Yes\n",t++);
else printf("Case #%d: No\n",t++);
}
return ;
}

hdu 4786(生成树)的更多相关文章

  1. HDU 4786 生成树 并查集+极大极小值 黑白边 确定选择白边的数量

    题意: 给定一个无向图 n 个点 m条无向边 u v val val == 1 表示边(u, v) 为白边 问能否找到n个点的生成树, 使得白边数为斐波那契数 思路: 并查集求图是否连通( 是否存在生 ...

  2. HDU 4786 Fibonacci Tree(生成树,YY乱搞)

    http://acm.hdu.edu.cn/showproblem.php? pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others ...

  3. HDU 4786 Fibonacci Tree 生成树

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4786 题意:有N个节点(1 <= N <= 10^5),M条边(0 <= M <= ...

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

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

  5. hdu 4786 Fibonacci Tree (最小、最大生成树)

    题意: N个点,M条边.每条边连接两个点u,v,且有一个权值c,c非零即一. 问能否将N个点形成一个生成树,并且这棵树的边权值和是一个fibonacii数. (fibonacii数=1,2,3,5,8 ...

  6. hdu 4786 Fibonacci Tree (2013ACMICPC 成都站 F)

    http://acm.hdu.edu.cn/showproblem.php?pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) ...

  7. HDU 4786 Fibonacci Tree 最小生成树

    Fibonacci Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4786 Description Coach Pang is intere ...

  8. HDU 4786(最小生成树 kruskal)

    题目链接:pid=4786" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=4786 Prob ...

  9. 【HDU 4786 Fibonacci Tree】最小生成树

    一个由n个顶点m条边(可能有重边)构成的无向图(可能不连通),每条边的权值不是0就是1. 给出n.m和每条边的权值,问是否存在生成树,其边权值和为fibonacci数集合{1,2,3,5,8...}中 ...

随机推荐

  1. GoF23种设计模式之结构型模式之桥接模式

    一.概述         将类的抽象部分与实现分部分离开来,使它们都可以独立地变化. 二.适用性 1.你不希望在抽象和实现之间有一个固定的绑定关系的时候.例如:在程序运行时实现部分应可以被选择或切换. ...

  2. python模块之collections模块

    计数器 Counter 计数元素迭代器 elements() 计数对象拷贝 copy() 计数对象清空 clear() from collections import Counter #import ...

  3. TCP/IP网络编程之多播与广播

    多播 多播方式的数据传输是基于UDP完成的,因此,与UDP服务端/客户端的实现非常接近.区别在于,UDP数据传输以单一目标进行,而多播数据同时传递到加入(注册)特定组的大量主机.换言之,采用多播方式时 ...

  4. linux学习(四) -- supervisor守护进程

      supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启.   1.安装 apt-get install ...

  5. Oracle数据库迁移--->从Windows到Linux

    I did a practice to migrate the oracle database from windows to linux operation system. The followin ...

  6. 35、键盘布局的tableLayout备份

    <TableLayout android:layout_width="wrap_content" android:layout_height="wrap_conte ...

  7. python - 接口自动化测试 - TestRecharge - 充值接口测试用例

    # -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: test_recharge.py @ide: PyChar ...

  8. Java类和对象 详解(一)---写的很好通俗易懂---https://blog.csdn.net/wei_zhi/article/details/52745268

    https://blog.csdn.net/wei_zhi/article/details/52745268

  9. SpringBoot中Async异步方法和定时任务介绍

    1.功能说明 Spring提供了Async注解来实现方法的异步调用. 即当调用Async标识的方法时,调用线程不会等待被调用方法执行完成即返回继续执行以下操作,而被调用的方法则会启动一个独立线程来执行 ...

  10. 获取表的字段例如 col1,col2,col3

    create function [dbo].[f_getcolsByName](@tableName varchar(50)) returns varchar(1000)asbegin declare ...