HDU 4786 Fibonacci Tree(生成树,YY乱搞)
http://acm.hdu.edu.cn/showproblem.php?
pid=4786
Fibonacci Tree
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1733 Accepted Submission(s): 543
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, ... )
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).
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
Case #1: Yes
Case #2: No
/*
*
* Author : fcbruce
*
* Time : Mon 06 Oct 2014 01:06:30 PM CST
*
*/
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10 #ifdef _WIN32
#define lld "%I64d"
#else
#define lld "%lld"
#endif #define maxm 100007
#define maxn 100007 using namespace std; int fib[maxn];
struct _edge
{
int u,v,w;
bool operator < (const _edge &_)const
{
return w<_.w;
}
}edge[maxm]; int pre[maxn]; int root(int x)
{
if (x==pre[x]) return x;
return pre[x]=root(pre[x]);
} bool same(int x,int y)
{
return root(x)==root(y);
} void _merge(itn x,int y)
{
pre[root(x)]=root(y);
} int cnt,fib_cnt; int main()
{
#ifdef FCBRUCE
freopen("/home/fcbruce/code/t","r",stdin);
#endif // FCBRUCE int T_T,__=0;
scanf("%d\n",&T_T); fib[0]=1;
fib[1]=1;
fib_cnt=2;
for (int i=2;;i++)
{
fib[i]=fib[i-1]+fib[i-2];
fib_cnt++;
if (fib[i]>100000) break;
} while (T_T--)
{
printf("Case #%d: ",++__);
int n,m;
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++) pre[i]=i;
cnt=n;
for (int i=0,u,v,w;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
if (!same(u,v)) {_merge(u,v);cnt--;}
edge[i]=(_edge){u,v,w};
} if (cnt!=1)
{
printf("No\n");
continue;
} sort(edge,edge+m); for (int i=1;i<=n;i++) pre[i]=i;
int MIN=0;
cnt=n;
for (int i=0,u,v,w;i<m;i++)
{
u=edge[i].u;v=edge[i].v;w=edge[i].w;
if (!same(u,v))
{
_merge(u,v);
MIN+=w;
cnt--;
if (cnt==1) break;
}
} for (int i=1;i<=n;i++) pre[i]=i;
int MAX=0;
cnt=n;
for (int i=m-1,u,v,w;i>=0;i--)
{
u=edge[i].u;v=edge[i].v;w=edge[i].w;
if (!same(u,v))
{
_merge(u,v);
MAX+=w;
cnt--;
if (cnt==1) break;
}
} int idmin=lower_bound(fib,fib+fib_cnt,MIN)-fib;
int idmax=lower_bound(fib,fib+fib_cnt,MAX)-fib; if (fib[idmin]!=MIN && fib[idmax]!=MAX && idmin==idmax)
puts("No");
else
puts("Yes"); } return 0;
}
HDU 4786 Fibonacci Tree(生成树,YY乱搞)的更多相关文章
- HDU 4786 Fibonacci Tree 生成树
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4786 题意:有N个节点(1 <= N <= 10^5),M条边(0 <= M <= ...
- hdu 4786 Fibonacci Tree (2013ACMICPC 成都站 F)
http://acm.hdu.edu.cn/showproblem.php?pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) ...
- HDU 4786 Fibonacci Tree 最小生成树
Fibonacci Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4786 Description Coach Pang is intere ...
- HDU 4786 Fibonacci Tree
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) P ...
- HDU 4786 Fibonacci Tree (2013成都1006题)
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 4786 Fibonacci Tree(最小生成树)
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 4786 Fibonacci Tree 乱搞 智商题目 最小生成树
首先计算图的联通情况,如果图本身不联通一定不会出现生成树,输出"NO",之后清空,加白边,看最多能加多少条,清空,加黑边,看能加多少条,即可得白边的最大值与最小值,之后判断Fibo ...
- hdu 4786 Fibonacci Tree (最小、最大生成树)
题意: N个点,M条边.每条边连接两个点u,v,且有一个权值c,c非零即一. 问能否将N个点形成一个生成树,并且这棵树的边权值和是一个fibonacii数. (fibonacii数=1,2,3,5,8 ...
- 【HDU 4786 Fibonacci Tree】最小生成树
一个由n个顶点m条边(可能有重边)构成的无向图(可能不连通),每条边的权值不是0就是1. 给出n.m和每条边的权值,问是否存在生成树,其边权值和为fibonacci数集合{1,2,3,5,8...}中 ...
随机推荐
- 【转】密码学 :CSP的概念
转:[密码学]CSP的概念 CSP加密服务提供者(Cryptographic Service Provider)具有一下几个特点: CSP是真正执行密码运算的独立模块 物理上一个CSP由两部分组成:一 ...
- Java编程:常见问题汇总
每天在写Java程序,其实里面有一些细节大家可能没怎么注意,这不,有人总结了一个我们编程中常见的问题.虽然一般没有什么大问题,但是最好别这样做. AD: 每天在写Java程序,其实里面有一些细节大家可 ...
- luogu P1618 三连击(升级版)
题目描述 将1,2,…,9共9个数分成三组,分别组成三个三位数,且使这三个三位数的比例是A:B:C,试求出所有满足条件的三个三位数,若无解,输出“No!!!”. //感谢黄小U饮品完善题意 输入输出格 ...
- fork 和 exec
https://blog.csdn.net/disadministrator/article/details/39347333 进程创建方法:fork.exec.clone,父进程等待子进程结束是用w ...
- c++_方格填数(最新方法)
方格填数 如下的10个格子 +--+--+--+ | | | |+--+--+--+--+| | | | |+--+--+--+--+| | | |+--+--+--+ (如果显示有问题,也可以参 ...
- Spider-天眼查字体反爬
字体反爬也就是自定义字体反爬,通过调用自定义的woff文件来渲染网页中的文字,而网页中的文字不再是文字,而是相应的字体编码,通过复制或者简单的采集是无法采集到编码后的文字内容! 1.思路 近期在爬取天 ...
- POJ 2631 Roads in the North (树的直径)
题意: 给定一棵树, 求树的直径. 分析: 两种方法: 1.两次bfs, 第一次求出最远的点, 第二次求该点的最远距离就是直径. 2.同hdu2196的第一次dfs, 求出每个节点到子树的最长距离和次 ...
- Vijos 1308 埃及分数(迭代加深搜索)
题意: 输入a.b, 求a/b 可以由多少个埃及分数组成. 埃及分数是形如1/a , a是自然数的分数. 如2/3 = 1/2 + 1/6, 但埃及分数中不允许有相同的 ,如不可以2/3 = 1/3 ...
- LeetCode 309. Best Time to Buy and Sell Stock with Cooldown (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- shelve -- 用来持久化任意的Python对象
这几天接触了Python中的shelve这个module,感觉比pickle用起来更简单一些,它也是一个用来持久化Python对象的简单工具.当我们写程序的时候如果不想用关系数据库那么重量级的东东去存 ...