Fibonacci Tree

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4028    Accepted Submission(s): 1252

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条边,其中有一些边是白边,一些边是黑边,问是否存在一棵树上的白边数量是斐波拉契数列里面的某个数.
题解:很巧妙的思想,先按白边排序将白边最多的树选出来,然后黑边排序将白边最少的树选出来。然后如果有斐波拉契数在两棵树的大小中间(因为如果存在的话,是可以通过删边得到的),如果存在,就Ok,还要判一下连通分量。

#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <queue>
using namespace std;
const int N = ;
int father[N];
struct Edge{
int u,v,color;
}edge[N];
int _find(int x){
if(x!=father[x]) {
father[x] = _find(father[x]);
}
return father[x];
} int n,m;
int cmp(Edge a,Edge b){
return a.color>b.color;
}
int cmp1(Edge a,Edge b){
return a.color<b.color;
}
int kruskal(){
int cost=;
for(int i=;i<m;i++){
int x=_find(edge[i].u);
int y=_find(edge[i].v);
if(x!=y){
father[x] = y;
cost+=edge[i].color;
}
}
return cost;
}
bool vis[N];
void init(){
memset(vis,false,sizeof(vis));
int a=,b=;
vis[]=true,vis[] =true;
while(a+b<N){
vis[a+b]=true;
swap(a,b);
b = a+b;
}
}
int main()
{
int tcase;
scanf("%d",&tcase);
int t = ;
init();
while(tcase--){
scanf("%d%d",&n,&m);
for(int i=;i<m;i++){
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].color);
}
for(int i=;i<=n;i++) father[i] = i;
sort(edge,edge+m,cmp);
int maxn = kruskal();
int ans = ;
for(int i=;i<=n;i++){
if(father[i]==i) ans++;
if(ans>) break;
}
if(ans>) {
printf("Case #%d: No\n",t++);
continue;
}
for(int i=;i<=n;i++) father[i] = i;
sort(edge,edge+m,cmp1);
int minn = kruskal();
ans = ;
for(int i=;i<=n;i++){
if(father[i]==i) ans++;
if(ans>) break;
}
if(ans>) {
printf("Case #%d: No\n",t++);
continue;
}
//printf("%d %d\n",minn,maxn);
bool flag = false;
for(int i=minn;i<=maxn;i++){
if(vis[i]){
flag = true;
break;
}
}
if(flag) printf("Case #%d: Yes\n",t++);
else printf("Case #%d: No\n",t++);
}
return ;
}

hdu 4786(生成树)的更多相关文章

  1. HDU 4786 生成树 并查集+极大极小值 黑白边 确定选择白边的数量

    题意: 给定一个无向图 n 个点 m条无向边 u v val val == 1 表示边(u, v) 为白边 问能否找到n个点的生成树, 使得白边数为斐波那契数 思路: 并查集求图是否连通( 是否存在生 ...

  2. HDU 4786 Fibonacci Tree(生成树,YY乱搞)

    http://acm.hdu.edu.cn/showproblem.php? pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others ...

  3. HDU 4786 Fibonacci Tree 生成树

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4786 题意:有N个节点(1 <= N <= 10^5),M条边(0 <= M <= ...

  4. hdu 4786 最小生成树与最大生成树

    /* 题意 :有一些边权值为1和0,判断是否存在一个生成树使得他的总权值为一个斐波那契数. 解法:建立一个最小生成树向里面加权值为1的边替换为0的边,保证原来的联通.因为权值为1,可直接求出最大生成树 ...

  5. hdu 4786 Fibonacci Tree (最小、最大生成树)

    题意: N个点,M条边.每条边连接两个点u,v,且有一个权值c,c非零即一. 问能否将N个点形成一个生成树,并且这棵树的边权值和是一个fibonacii数. (fibonacii数=1,2,3,5,8 ...

  6. hdu 4786 Fibonacci Tree (2013ACMICPC 成都站 F)

    http://acm.hdu.edu.cn/showproblem.php?pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) ...

  7. HDU 4786 Fibonacci Tree 最小生成树

    Fibonacci Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4786 Description Coach Pang is intere ...

  8. HDU 4786(最小生成树 kruskal)

    题目链接:pid=4786" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=4786 Prob ...

  9. 【HDU 4786 Fibonacci Tree】最小生成树

    一个由n个顶点m条边(可能有重边)构成的无向图(可能不连通),每条边的权值不是0就是1. 给出n.m和每条边的权值,问是否存在生成树,其边权值和为fibonacci数集合{1,2,3,5,8...}中 ...

随机推荐

  1. 18.VUE学习之-v-for操作对象与数值

    一组数组时的循环 二组数组时的循环 另外可以v for 20 可以直接操作数字 <!DOCTYPE html> <html lang="en"> <h ...

  2. web前端的环境配置

    1.1.WEB开发的相关知识 WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. Internet上供外界访问的Web资源分为: 静态web资源(如html 页 ...

  3. python3.7 装饰器

    #!/usr/bin/env python __author__ = "lrtao2010" #python3.7 装饰器 #装饰器 ''' 定义:本质就是一个函数,作用是为其他函 ...

  4. python集成开发环境PyCharm

    环境安装视频介绍:http://pan.baidu.com/s/1gfz6wiZ ppmb 外加几个截图: activate:

  5. RCP 主题切换

    第一步 编写css文件,放到项目目录下 第二步 添加切换主题扩展点 第三步 设置主题   public  void switchTheme(String themeID) {           Bu ...

  6. 开源中国app说什么 旁边的那个图标是什么drawable

    妈的,那就只是一个 ActionBarDrawerToggle: 配合侧滑菜单而生的而已.

  7. 【Python】函数参数类型及用法

     一.函数的参数类型 def hs(a1,a2,a3,...): ****statements 其中a1,a2,a3是函数的参数,函数的参数类型可分为:必须参数.默认参数.可变参数(不定长参数).关键 ...

  8. sc.exe

    sc.exe 编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 服务管理程序:可用sc.exe远程创建 外文名 sc.exe 停止事件服务 sc  stop eventl ...

  9. 输出1到最大的N位数 【微软面试100题 第六十五题】

    题目要求: 输入数字n,按顺序输出从1到最大的n位10进制数. 例如,输入3,则输出1.2.3....999(最大的3位数). 参考资料:剑指offer第12题. 题目分析: 如果我们在数字前面补0的 ...

  10. 项目管理者必知:适用于仪表盘项目的7个优秀JavaScript库

    仪表盘是用于目标或业务流程的视觉指示工具,也用于切割杂乱无章的数据,从而分割出要点的重要工具.它可以帮助评估信息并及时做出正确的决定,一款实时可视化的仪表盘通常由图标.测绘图.图形符号以及数据表格等组 ...