【HDU - 4786 】Fibonacci Tree (最小生成树变形,上下界贪心,tricks)

题干:

 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 <= 10 5) and M(0 <= M <= 10 5). 
  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代表白边,0代表黑边,求能否建一个生成树,使得白边的数量是一个斐波那契数列

解题报告:

关于输出YesNo的问题,用界的方法去判定往往可以简化问题,比如这题我们虽然构造出这棵树来很难,但是判断是否存在却很简单,只需要看最少需要多少条白边和最多可以构造出多少条白边就可以了,然后中间所有白色边的数量的生成树就都可以构造出来了。

关于这一点好像没有很严谨的证明,但是也不难想通。因为可以抽象为白色边权为1,黑色边权为0,已知最小生成树为minn,最大生成树为maxx,那么我们一定可以通过+1(加白边减黑边),-1(减白边加黑边)的方式在不改变生成树性质的前提下将minn转移到maxx,所以这其中的任何一个权值都可以被凑出来。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int n,m,tot;
int f[MAX],F[MAX],ff[MAX];
struct Edge {int u,v;int c;
} e[MAX];
bool cmp(Edge a,Edge b) {return a.c < b.c;
}
bool cmpp(Edge a,Edge b) {return a.c > b.c;
}
void add(int u,int v,int c) {e[++tot].u = u;e[tot].v = v;e[tot].c = c;
}
int getf(int v) {return f[v] == v ? v : f[v] = getf(f[v]);
}void merge(int u,int v) {int t1 = getf(u),t2 = getf(v);f[t2] = t1;
}
bool check() {//返回1代表是一个连通图 int boss = getf(1);for(int i = 1; i<=n; i++) {if(getf(i) != boss) {return 0;}}return 1;
}
int fib[44],xuan[MAX];
bool isfib(int x) {for(int i = 1; i<=28; i++) {if(x == fib[i]) return 1;}return 0;
}
int cal() {int res = 0;for(int i = 1; i<=n; i++) f[i] = i;for(int i = 1; i<=m; i++) {if(getf(e[i].u) != getf(e[i].v)) {merge(e[i].u,e[i].v);if(e[i].c == 1) res++;}}return res;
}
int main()
{fib[1] = 1;fib[2] = 2;for(int i = 3; i<=40; i++) {fib[i] = fib[i-1] + fib[i-2];}int t,iCase=0;cin>>t;while(t--) {tot=0;scanf("%d%d",&n,&m);for(int i = 1; i<=n; i++) f[i] = i;for(int u,v,c,i = 1; i<=m; i++) scanf("%d%d%d",&u,&v,&c),add(u,v,c),xuan[i] = 0;printf("Case #%d: ",++iCase);for(int i = 1; i<=m; i++) {merge(e[i].u,e[i].v);}if(check() == 0) printf("No\n");else {sort(e+1,e+m+1,cmp);int flag = 0,low = cal();sort(e+1,e+m+1,cmpp);int up = cal();for(int i = 1; i<=28; i++) {if(fib[i] >= low && fib[i] <= up) flag = 1;}if(flag == 1) printf("Yes\n");else printf("No\n");}} return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440244.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

网络编程懒人入门(四):快速理解TCP和UDP的差异

转自即时通讯网&#xff1a;http://www.52im.net/ 原作者&#xff1a;MeloDev&#xff0c;本文由即时通讯网重新修订发布&#xff0c;感谢原作者的无私分享。 1、前言 对于即时通讯开发者新手来说&#xff0c;在开始着手编写IM或消息推送系统的代码前&#xff0c;最头疼的问…

【HDU - 5455】Fang Fang(水题,有坑)

题干&#xff1a; Fang Fang says she wants to be remembered. I promise her. We define the sequence FF of strings. F0 ‘‘f",F0 ‘‘f", F1 ‘‘ff",F1 ‘‘ff", F2 ‘‘cff",F2 ‘‘cff", Fn Fn−1 ‘‘f", for n &g…

npm安装与卸载和cordova及ionic项目打包调试等相关命令总结归纳

一、安装和卸载node和npm的命令 1、Mac系统彻底卸载npmsudo rm -rf /usr/local/{bin/{node,npm},lib/node_modules/npm,lib/node,share/man/*/node.*}2、因网络问题导致包安装失败的安全解决方法cd到对应项目之下&#xff0c;如果有权限问题前加sudorm -rf node_modules/npm c…

Apollo自动驾驶入门课程第⑤讲 — 感知(下)

目录 1. 卷积神经网络 2. 检测与分类 3. 跟踪 4. 分割 5. Apollo感知 6. 传感器数据比较 7. 感知融合策略 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a; 阿波君 Apollo开发者社区 8月29日 上一篇文章中&#xff0c;我们发布了无人驾驶技术的 感知篇…

【 HDU - 5459】Jesus Is Here(dp)

题干&#xff1a; Ive sent Fang Fang around 201314 text messages in almost 5 years. Why cant she make sense of what I mean? But Jesus is here!" the priest intoned. Show me your messages." Fine, the first message is s1‘‘c"s1‘‘c" a…

TextRank算法原理和提取关键词的主要过程详解 计算句子相似度 计算句子重要性公式

1、TextRank计算句子相似度和句子重要性的公式 2、TextRank算法提取关键词的过程 3、TextRank建立关键词无向图

Apollo自动驾驶入门课程第⑥讲 — 预测

目录 1. 简介 2. 不同的预测方式 3. 基于车道序列的预测 4. 障碍物状态 5. 预测目标车道 6. 递归神经网络 7. 递归神经网络在目标车道预测的应用 8. 轨迹生成 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a;涛涛CV Apollo开发者社区 9月6日 上一篇文…

使用PDF.js实现前端和手机端网页预览PDF文件(可定制,支持本地文件、Base64编码和远程URL跨域方式)

1.插件下载地址&#xff1a;https://mozilla.github.io/pdf.js/ 下载后解压pdfjs-1.10.88-dist.zip文件后得到&#xff1a; 2.把pdfjs-1.10.88-dist放到项目静态资源中&#xff0c;在自己的页面中通过iframe链接到pdfjs-1.10.88-dist/web/viewer.html文件中。 3.访问自己的页…

【HDU - 5878】I Count Two Three(打表)

题干&#xff1a; I will show you the most popular board game in the Shanghai Ingress Resistance Team. It all started several months ago. We found out the home address of the enlightened agent Icount2three and decided to draw him out. Millions of missile…

移动互联网浩荡十年 有的升腾,有的陨落

原创&#xff1a; 颜西龙 猎云网 &#xff08;ilieyun&#xff09;1周前 中国移动互联网的十年&#xff0c;是波澜壮阔、荡气回肠的十年。本文回溯了这段历史&#xff0c;在这十年间里&#xff0c;有的企业升腾&#xff0c;有的企业陨落。 2011年8月16日&#xff0c;北京798艺术…

原生JS动态计算输入框文本内容的宽度,当内容宽度超过输入框的宽度时可控

需求场景&#xff1a;左边输入框输入内容&#xff0c;右边输入框用placeholder展示&#xff0c;当placeholder的内容宽度超过右边输入框的宽度时&#xff0c;placeholder强行替换为“请选择” 注意事项&#xff1a;1、左右输入框的大小、样式都无关&#xff1b; 2、实际业务中…

【HDU - 5881】Tea(思维,找规律)

题干&#xff1a; Tea is good. Tea is life. Tea is everything. The balance of tea is a journey of pursuing balance of the universe. Alice knows that. Alice wants to teach you the art of pouring tea. Alice has a pot of tea. The exact volume of tea is…

Apollo自动驾驶入门课程第⑦讲 — 规划(上)

目录 1. 规划简介 2. 将地图转为图形 3. 路径查找算法&#xff1a;A* 4. 轨迹生成 5. Fernet坐标系 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a; 阿波君 Apollo开发者社区 9月13日 上周我们发布了无人驾驶技术的 预测篇&#xff0c;简要介绍了预测的…

JS正则表达式常见场景下的用法总结

&#xff08;一&#xff09;前置知识总结&#xff1a; 1. 正则表达式 /xxxx/[标识] 其中的标识含义 •g &#xff08;全文查找&#xff09; •i &#xff08;忽略大小写&#xff09; •m &#xff08;多行查找&#xff09; 2. 正则表达式创建的两种方式&#xff08;等价&#…

【HDU - 5882】Balanced Game (找规律,思维)

题干&#xff1a; Rock-paper-scissors is a zero-sum hand game usually played between two people, in which each player simultaneously forms one of three shapes with an outstretched hand. These shapes are "rock", "paper", and "scisso…

Apollo自动驾驶入门课程第⑧讲 — 规划(下)

目录 1. 路径-速度解耦规划 2. 路径生成与选择 3. ST图 4. 速度规划 5. 优化 6. 路径-速度规划的轨迹生成 7. Lattice规划 8. ST轨迹的终止状态 9. SL轨迹的终止状态 10. Lattice规划的轨迹生成 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a; 阿…

网络编程懒人入门(五):快速理解为什么说UDP有时比TCP更有优势

转自即时通讯网&#xff1a;http://www.52im.net/ 本文观点仅作参考&#xff0c;请根据自已系统的应用场景合理地选择数据传输层协议即可&#xff0c;无需盲目崇拜大牛言论。 1、前言 对于即时通讯开者新手来说&#xff0c;在开始着手编写IM或消息推送系统的代码前&#xff…

CSS定位总结:position=static/relative/absolute/fixed时的区别、top/bottom/left/right与margin外边距的运用

准备布局&#xff1a; <!DOCTYPE html> <html> <head> <style> *{ margin:0; padding:0; } .base-container{width: 500px;height: 500px;background-color:lightgray;border:1px dashed red; } .base-compare {width:200px;height:100px;background…

【HDU - 5883】The Best Path(判断欧拉回路)

题干&#xff1a; Alice is planning her travel route in a beautiful valley. In this valley, there are NN lakes, and MM rivers linking these lakes. Alice wants to start her trip from one lake, and enjoys the landscape by boat. That means she need to set up …

网络编程懒人入门(六):史上最通俗的集线器、交换机、路由器功能原理入门

转自即时通讯网&#xff1a;http://www.52im.net/ 本文引用了知乎网友“薛定谔不在家”的部分文字内容&#xff0c;感谢原作者的分享。 1、前言 即时通讯网整理了大量的网络编程类基础文章和资料&#xff0c;包括《TCP/IP协议 卷1》、《[通俗易懂]深入理解TCP协议》系列、《…