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条边的图,给定边的权值为1(白色)或2(黑色),问是否存在一个生成树,使得其中白边的数量为斐波那契数?

题解:

首先判断这个图是否为连通图,若不是直接输出No。

然后只要用白边优先(最大生成树)的总权值减去黑边优先(最小生成树)的总权值,就可以得到一个白边数量的区间,然后枚举斐波那契数即可。

题目链接:HDOJ 4786

#include<bits/stdc++.h>
#define MAX 100000
using namespace std;
int n,m,p1[MAX+],p2[MAX+],fib[];
struct edge{
int u,v,w;
}e[MAX+];
int find(int r,int p[])
{
if(p[r]!=r)
p[r]=find(p[r],p);
return p[r];
}
void init(int p[])
{
for(int i=;i<=MAX;i++)
p[i]=i;
}
bool cmp1(edge a,edge b){return a.w>b.w;}
bool cmp2(edge a,edge b){return a.w<b.w;}
int KurskalMax(int p[])
{
init(p);
sort(e,e+m,cmp1);
int cnt=,cost=,i;
for(i=;i<m;i++)
{
int fu=find(e[i].u,p),fv=find(e[i].v,p);
if(fu!=fv)
{
p[fu]=fv;
cost+=e[i].w;
cnt++;
}
if(cnt==n-)break;
}
return cost;
}
int KurskalMin(int p[])
{
init(p);
sort(e,e+m,cmp2);
int cnt=,cost=,i;
for(i=;i<m;i++)
{
int fu=find(e[i].u,p),fv=find(e[i].v,p);
if(fu!=fv)
{
p[fu]=fv;
cost+=e[i].w;
cnt++;
}
if(cnt==n-)break;
}
if(cnt!=n-)return -;
return cost;
}
int main()
{
int i;
fib[]=;fib[]=;
for(int i=;i<=;i++)
fib[i]=fib[i-]+fib[i-];
int T;
scanf("%d",&T);
for(int cas=;cas<=T;cas++)
{
scanf("%d%d",&n,&m);
for(i=;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
e[i].u=u;e[i].v=v;e[i].w=w;
}
int L=KurskalMin(p1),R=KurskalMax(p2),flag=;
if(L!=-)
{
for(i=L;i<=R;i++)
{
if(fib[lower_bound(fib+,fib++,i)-fib]==i)
{
flag=;
break;
}
}
}
printf("Case #%d: ",cas);
if(flag)printf("Yes\n");
else printf("No\n");
}
return ;
}

【2013 ICPC亚洲区域赛成都站 F】Fibonacci Tree(最小生成树+思维)的更多相关文章

  1. 【2018 ICPC亚洲区域赛沈阳站 L】Tree(思维+dfs)

    Problem Description Consider a un-rooted tree T which is not the biological significance of tree or ...

  2. 【2017 ICPC亚洲区域赛沈阳站 K】Rabbits(思维)

    Problem Description Here N (N ≥ 3) rabbits are playing by the river. They are playing on a number li ...

  3. 2014ACM/ICPC亚洲区域赛牡丹江站汇总

    球队内线我也总水平,这所学校得到了前所未有的8地方,因为只有两个少年队.因此,我们13并且可以被分配到的地方,因为13和非常大的数目.据领队谁oj在之上a谁去让更多的冠军.我和tyh,sxk,doub ...

  4. 2013 ACM-ICPC亚洲区域赛南京站C题 题解 轮廓线DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4804 题目大意 给你一个 \(n \times m\) 的矩形区域.你需要用 \(1 \times 1 ...

  5. 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-A ( ZOJ 3819 ) Average Score

    Average Score Time Limit: 2 Seconds      Memory Limit: 65536 KB Bob is a freshman in Marjar Universi ...

  6. 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-K ( ZOJ 3829 ) Known Notation

    Known Notation Time Limit: 2 Seconds      Memory Limit: 65536 KB Do you know reverse Polish notation ...

  7. 【2018 ICPC亚洲区域赛南京站 A】Adrien and Austin(博弈)

    题意: 有一排n个石子(注意n可以为0),每次可以取1~K个连续的石子,Adrien先手,Austin后手,若谁不能取则谁输. 思路: (1) n为0时的情况进行特判,后手必胜. (2) 当k=1时, ...

  8. 【2018 ICPC亚洲区域赛徐州站 A】Rikka with Minimum Spanning Trees(求最小生成树个数与总权值的乘积)

    Hello everyone! I am your old friend Rikka. Welcome to Xuzhou. This is the first problem, which is a ...

  9. 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-I ( ZOJ 3827 ) Information Entropy

    Information Entropy Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Information ...

随机推荐

  1. MongoDB 删除集合

    drop() 方法 MongoDB 的 db.collection.drop() 是用来从数据库中删除一个集合. 语法: drop() 命令的基本语法如下 db.COLLECTION_NAME.dro ...

  2. Spark企业级应用开发和调优

    1.Spark企业级应用开发和调优 Spark项目编程优化历程记录,主要介绍了Spark企业级别的开发过程中面临的问题和调优方法.包含合理分配分片,避免计算中间结果(大数据量)的collect,合理使 ...

  3. Java设计模式—观察者模式

    观察者模式(Observer Pattern)也叫做发布订阅模式(Publish/subscribe). 其定义如下: 定义对象间一种一对多的依赖关系,使得每当一个对象改变状态,则所有依赖于它的对象都 ...

  4. python mqtt client publish操作

    使用Python库paho.mqtt.client 模拟mqtt client 连接broker,publish topic. #-*-coding:utf-8-*- import paho.mqtt ...

  5. SQL点点滴滴_常用函数

    该文章转载自http://www.cnblogs.com/jiajiayuan/archive/2011/06/16/2082488.html 别人的总结,很详细. 以下所有例子均Studnet表为例 ...

  6. IIS 无法安装URL重写模块的解决办法 UrlReWrite (.NET`SQL技术交流 群号206656202)

    下载和安装URL Rewrite IIS8默认是没有安装URL重写工具的,必须要自己下载安装. 如果IIS上默认有安装Web平台安装程序,我们可以使用平台自动安装URL Rewrite重写工具,打开I ...

  7. MySQL存储过程和临时表

    MySQL创建存储过程 MySQL中,创建存储过程的基本形式如下: CREATE PROCEDURE sp_name ([proc_parameter[,...]]) [characteristic ...

  8. 微软在线 VSTS/TFS 使用简介,如何删除项目,帐号,获取git地址等

    名称:微软 VSTS 全称: Visual Studio Team Services 地址:https://www.visualstudio.com/zh-hans/ 说明:注册就可以了使用了(如何使 ...

  9. 在一个应用中如果同一个Spring 的IOC容器被实例化两次就会出现 CannotAcquireResourceException 异常

    现象描述:我在一个Junit 的测试类中实例化IOC容器 : ac = new ClassPathXmlApplicationContext("applicationContext.xml& ...

  10. luogu3368树状数组模板2

    题目链接:https://www.luogu.org/problemnew/show/P3368 题意:与模板1不同的是这题的操作是树状数组并不在行的区间更新和单点查找,如果按照模板1那样写肯定会T. ...