【2013 ICPC亚洲区域赛成都站 F】Fibonacci Tree(最小生成树+思维)
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
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
题意:
有一个n个点,m条边的图,给定边的权值为1(白色)或2(黑色),问是否存在一个生成树,使得其中白边的数量为斐波那契数?
题解:
首先判断这个图是否为连通图,若不是直接输出No。
然后只要用白边优先(最大生成树)的总权值减去黑边优先(最小生成树)的总权值,就可以得到一个白边数量的区间,然后枚举斐波那契数即可。
#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(最小生成树+思维)的更多相关文章
- 【2018 ICPC亚洲区域赛沈阳站 L】Tree(思维+dfs)
Problem Description Consider a un-rooted tree T which is not the biological significance of tree or ...
- 【2017 ICPC亚洲区域赛沈阳站 K】Rabbits(思维)
Problem Description Here N (N ≥ 3) rabbits are playing by the river. They are playing on a number li ...
- 2014ACM/ICPC亚洲区域赛牡丹江站汇总
球队内线我也总水平,这所学校得到了前所未有的8地方,因为只有两个少年队.因此,我们13并且可以被分配到的地方,因为13和非常大的数目.据领队谁oj在之上a谁去让更多的冠军.我和tyh,sxk,doub ...
- 2013 ACM-ICPC亚洲区域赛南京站C题 题解 轮廓线DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4804 题目大意 给你一个 \(n \times m\) 的矩形区域.你需要用 \(1 \times 1 ...
- 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-A ( ZOJ 3819 ) Average Score
Average Score Time Limit: 2 Seconds Memory Limit: 65536 KB Bob is a freshman in Marjar Universi ...
- 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-K ( ZOJ 3829 ) Known Notation
Known Notation Time Limit: 2 Seconds Memory Limit: 65536 KB Do you know reverse Polish notation ...
- 【2018 ICPC亚洲区域赛南京站 A】Adrien and Austin(博弈)
题意: 有一排n个石子(注意n可以为0),每次可以取1~K个连续的石子,Adrien先手,Austin后手,若谁不能取则谁输. 思路: (1) n为0时的情况进行特判,后手必胜. (2) 当k=1时, ...
- 【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 ...
- 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-I ( ZOJ 3827 ) Information Entropy
Information Entropy Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge Information ...
随机推荐
- 使用JS完成首页轮播图效果
获取document.getElementById("id名称"); 事件onload 定时操作setInterval("changeImg()",3000); ...
- 密码存储中MD5的安全问题与替代方案
md5安全吗?有多么地不安全?如何才能安全地存储密码?... md5安全吗? 经过各种安全事件后,很多系统在存放密码的时候不会直接存放明文密码了,大都改成了存放了 md5 加密(hash)后的密码,可 ...
- 把IDEA中新建的项目提交到Github仓库中
对于一个没有进行任何版本控制设置的idea工程,使其支持Github,设置步骤如下 到Git官网下载Git的安装包,安装好以后,Git的安装目录下的文件结构应该如下图所示 在IDEA开发工具中配置Gi ...
- SQL Server ->> CONCAT函数
这是一个SQL Server 2012后引进的新函数.作用就如同它名字的意思.它对NULL值得处理是空字符串.当然它能做的不仅是对字符的支持.它支持N个列输入,列的类型支持更加完善.不过其实它的原理不 ...
- Eclipse编码格式
来源:http://e-ant.javaeye.com/blog/177579 如果要使插件开发应用能有更好的国际化支持,能够最大程度的支持中文输出,则最好使 Java文件使用UTF-8编码.然而,E ...
- a标签在实际工作中的应用
学习的时候,我们对a标签的认知: 1.href属性添加一个地址,可进行页面的跳转 2.用锚点,制作页面内跳转和跨页面跳转(之前有写过一篇关于锚点的随笔:http://www.cnblogs.com/q ...
- spring初始化完成后执行初始化数据方法
Spring提供的解决方案三种: 1.InitializingBean package com.foriseland.fsoa.fabricca; import com.foriseland.fsoa ...
- Json.Net 中Linq to JSON的操作
Linq to JSON是用来操作JSON对象的.可以用于快速查询,修改和创建JSON对象.当JSON对象内容比较复杂,而我们仅仅需要其中的一小部分数据时,可以考虑使用Linq to JSON来读取和 ...
- SAP S/4HANA使用ABAP获得生产订单的状态
在S/4HANA里,我们如何根据一个销售订单的行项目,查看对应的生产订单状态? 双击行项目: 点击Schedule line: 这里就能看到生产订单的ID和状态了. 其中订单的状态存储在表vsaufk ...
- 020hashlib模块
#里面内容没有见过,可能会比较难懂,需要找资料.我只是记录了视频中的用法,其他理解的东西,我直接理解,就没有写下来了.下面内容是视频演示过程 import hashlib m = hashlib ...