【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 ...
随机推荐
- Activiti学习之HelloWorld程序
流程图 部署流程定义 /** * 部署流程定义 */ @Test public void deploymentProcessDefinition() { ProcessEngine processEn ...
- eclipse 内存溢出
2011年02月22日 星期二 11:14 eclipse.exe -vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:MaxPermSize=128M ec ...
- Android 触发Button按钮事件的三种方式
1.新创建一个类 2.使用内部类 3.当多个button按钮时,为简化代码而创建的实例listener 贴代码: MainActivity.Java 文件: package com.android. ...
- select下拉框默认不能选择第一个选项的问题
如题,现在有个js的功能:用户选择下拉框的同时,把选择的下拉框显示出来.同时选择的不能有重复的.刚开始 使用的是 select的onchange事件: $("#liveType") ...
- python三次输入错误验证登录
# login.py# 提示用户输入用户名和密码# 验证用户名和密码# 如果v错误,则输出用户名或密码错误# 如果成功,则输出欢迎,xxxnum = 0while True: name = input ...
- SSIS ->> Environment Variables
SQL Server Integration Services(SSIS) 在2012版本引入了Environment Variables这个新特性.它允许我们为一个环境创建出一套变量用于为项目内的包 ...
- Java接口与多态
接口 可以理解为一种特殊的类,里面全部是由全局常量(static final)和公共的抽象方法所组成 接口的定义格式 接口的数据成员,只允许被public, static, final修饰. 接口的方 ...
- 【转】PBOC3.0和PBOC2.0标准规范异同分析
2013年2月,中国人民银行发布了<中国金融集成电路(IC)卡规范(V3.0)>(以下简称PBOC3.0),PBOC3.0是在中国人民银行2005年颁布的<中国金融集成电路(IC)卡 ...
- 移动端fixed的元素抖动的问题
工作中发现,给一个元素添加fixed属性,让它固定在窗口某个位置,直接加fposition:fixed属性就能实现这个效果: 在安卓手机上的效果都比较好,但是ios系统的个别浏览器兼容性就不好,如QQ ...
- mongodb---js脚本操作速记
之前写一些mongodb的同步或操作程序,往往使用perl,甚至c实现,这样程序很繁琐,而且逻辑不好控制,甚至一些功能和命令什么的,在这些语言的mongo驱动中就没有实现.后来发现mongodb 的s ...