【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 ...
随机推荐
- Java Struts2 (三)
一.国际化概念(了解) 1.什么是国际化 软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的.符合来访者阅读习惯的页面或数据. 2.什么需要国际 ...
- Sql-exec
--显示sql server现有的所有数据库 exec sp_helpdb --查看数据表设置的约束 exec sp_helpconstraint SubjectType --update selec ...
- codeforces之始
很早就听说acmer界的CF嘞!还记得刚开始听到神犇们在讨论CF的时候我还以为是网游CF(穿越火线)呢... 今年刚开学的时候就打算开始打cf的,由于一些事情耽搁了.之后又要准备省赛所以就一直拖到现在 ...
- Visual C++编程实现摄像头视频捕捉
原文:http://blog.csdn.net/nemojiang/article/details/653033?locationNum=7&fps=1 前言 DirectShow是微软公司提 ...
- js if语句只写一个参数是什么意思?
如 var a=0:if(!a){...}; avascript中以下值会被转换为false false undefined null 0 -0 NaN ""
- JDBC mysql驱动
在用JDBC连接MySQL数据库时,需要使用驱动 mysql-connector-java-5.1.41-bin.jar 在本地java应用程序中,只需将jar包导入到项目library中即可, 而在 ...
- javascript 时间日期处理相加,减操作方法js
javascript 时间日期处理相加,减操作方法js function dateAddDays(dataStr,dayCount){ var strdate = dataStr; // 2017年0 ...
- .net正在终止线程异常
try{sting host = context.Request.UrlReferrer.Host;if ( 程序判断){ context.Response.Clear();context.Respo ...
- Virtualenv-windows
1.下载 pip3 install virtualenv 2.创建虚拟化环境 3. 进入虚拟化目录 4.推出虚拟化环境 5.指定python版本 二.virtualenvwrapper的使用 1.下载 ...
- 如何深入理解一套MQ消息中间件
怎样算是理解了一套MQ中间件呢?原来一知半解的我列了几个维度:demo跑起来,理解其投递次数的语义,理解其事务的特性等等.这是一种角度,但总有种看山不是山的一知半解的感觉.再问一层,比如为什么Kafk ...