Fibonacci Tree

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

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
 
Source
 

只要白边优先和黑边优先两种顺序做两次最小生成树。

得到白边数量的区间,然后枚举斐波那契数列就可以了。

注意如果一开始是非连通的,输出NO

 /* ***********************************************
Author :kuangbin
Created Time :2013-11-16 14:14:50
File Name :E:\2013ACM\专题强化训练\区域赛\2013成都\1006.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; int f[]; struct Edge
{
int u,v,c;
}edge[];
int F[];
int find(int x)
{
if(F[x] == -)return x;
return F[x] = find(F[x]);
} bool cmp1(Edge a,Edge b)
{
return a.c < b.c;
}
bool cmp2(Edge a,Edge b)
{
return a.c > b.c;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int tot = ;
f[] = ;
f[] = ;
while(f[tot] <= )
{
f[tot+] = f[tot] + f[tot-];
tot++;
}
int T;
int iCase = ;
int n,m;
scanf("%d",&T);
while(T--)
{
iCase++;
scanf("%d%d",&n,&m);
for(int i = ;i < m;i++)
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].c);
sort(edge,edge+m,cmp1);
memset(F,-,sizeof(F));
int cnt = ;
for(int i = ;i < m;i++)
{
int t1 = find(edge[i].u);
int t2 = find(edge[i].v);
if(t1 != t2)
{
F[t1] = t2;
if(edge[i].c == )cnt++;
}
}
int Low = cnt;
memset(F,-,sizeof(F));
sort(edge,edge+m,cmp2);
cnt = ;
for(int i = ;i < m;i++)
{
int t1 = find(edge[i].u);
int t2 = find(edge[i].v);
if(t1 != t2)
{
F[t1] = t2;
if(edge[i].c == )cnt++;
}
}
int High = cnt;
bool ff = true;
for(int i = ;i <= n;i++)
if(find(i) != find())
{
ff = false;
break;
}
if(!ff)
{
printf("Case #%d: No\n",iCase);
continue;
}
bool flag = false;
for(int i = ;i <= tot;i++)
if(f[i] >= Low && f[i] <= High)
flag = true;
if(flag)
printf("Case #%d: Yes\n",iCase);
else printf("Case #%d: No\n",iCase); }
return ;
}

HDU 4786 Fibonacci Tree (2013成都1006题)的更多相关文章

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

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

  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 最小生成树

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

  4. hdu 4786 Fibonacci Tree(最小生成树)

    Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  5. HDU 4786 Fibonacci Tree

    Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) P ...

  6. HDU 4786 Fibonacci Tree (2013成都1006题) 最小生成树+斐波那契

    题意:问生成树里能不能有符合菲波那切数的白边数量 思路:白边 黑边各优先排序求最小生成树,并统计白边在两种情况下数目,最后判断这个区间就可以.注意最初不连通就不行. #include <stdi ...

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

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

  8. HDU 4786 Fibonacci Tree 生成树

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

  9. hdu 4786 Fibonacci Tree 乱搞 智商题目 最小生成树

    首先计算图的联通情况,如果图本身不联通一定不会出现生成树,输出"NO",之后清空,加白边,看最多能加多少条,清空,加黑边,看能加多少条,即可得白边的最大值与最小值,之后判断Fibo ...

随机推荐

  1. 通俗易懂之Tensorflow summary类 & 初识tensorboard

    前面学习的cifar10项目虽小,但却五脏俱全.全面理解该项目非常有利于进一步的学习和提高,也是走向更大型项目的必由之路.因此,summary依然要从cifar10项目说起,通俗易懂的理解并运用sum ...

  2. 第9月第30天 MVP

    1. import UIKit struct Person { // Model let firstName: String let lastName: String } protocol Greet ...

  3. Python输出9*9 乘法表

    for i in range(1,10): for j in range(1,i+1): print(str(j) + str("*") + str(i)+"=" ...

  4. [转]OpenMP 入门指南

    简介 这门课作为 ECE 中少有的跟计算机科学相关的课,自然是必上不可.不过无论是 OpenMP 还是 CUDA,对于平时极少接触并行编程的我来说,都是十分吃力的,第一次作业的 OpenMP 编程已经 ...

  5. Kali社会工程学攻击--powershell 攻击(无视防火墙)

    1.打开setoolkit 输入我们反弹shell的地址与端口 2.修改我的shellcode 3.攻击成功

  6. 解决getJSON跨域登录Session丢失的问题

    最近在做项目中发现,我用下面的代码异步请求到login.ashx: var memberUrl = rooturl + 'member.ashx?r=' + Math.random() + '& ...

  7. php ++测试

    2014年4月27日 12:17:47 结论暂时没有组织语言去表述,但是看看测试结果大家都会明白的 $x = 1; $y = empty($x) ? 3 : $x++; var_dump($x,$y) ...

  8. @PostConstruct和@PreConstruct

    详情参见:https://www.cnblogs.com/landiljy/p/5764515.html 1.@PostConstruct说明 被@PostConstruct修饰的方法会在服务器加载S ...

  9. vue项目里的日期格式化

    在项目中,我们经常需要把后台传回的日期进行格式化,可以在common里定义一个公共的js export function formatDate (date, fmt) { if (/(y+)/.tes ...

  10. 2011TG初赛

    一.单项选择题(共20题,每题1.5分,共计30分,每题有且仅有一个正确选项.) 1. 在二进制下,1011001+( )=1100110. A.1011 B.1101 C.1010 D.1111 B ...