LA 6540 Fibonacci Tree
以前做过的题···重新做一遍之后怎么做怎么wa···后来GG了···第二天看不知道为啥A了···难道我失忆了?
题意:无向图,边有黑色和白色两种颜色,求是否存在一个生成树中白边的个数是斐波那契数。
解法:并查集。对边按颜色进行排序,白边在前用并查集计算生成树中白边个数,再倒着算一遍,得到生成树的白边的最大值和最小值,判断其中有没有斐波那契数,注意要判断是否能构成生成树。
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
int father[100005], f[100010] = {0};
struct node
{
int u, v, c;
} edge[100005];
bool cmp(node a, node b)
{
return a.c > b.c;
}
int FIND(int a)
{
if(father[a] != a)
father[a] = FIND(father[a]);
return father[a];
}
int main()
{
f[1] = 1;
int f1 = 1, f2 = 1, f3 = 2;
while(f3 < 100010)
{
f[f3] = 1;
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}//斐波那契数打表
int T;
while(~scanf("%d", &T))
{
int cse = 1;
while(T--)
{
int n, m, ans = 1;
scanf("%d%d", &n, &m);
for(int i = 0; i < m; i++)
scanf("%d%d%d", &edge[i].u, &edge[i].v, &edge[i].c);
sort(edge, edge + m, cmp);
int l = 0, r = 0;
for(int i = 0; i <= n; i++)
father[i] = i;
for(int i = 0; i < m; i++)
{
int a, b;
a = FIND(edge[i].u);
b = FIND(edge[i].v);
if(a != b)
{
r += edge[i].c;
father[a] = b;
}
}//计算白边最大值r
int flag = FIND(1);
for(int i = 2; i <= n; i++)
if(FIND(i) != flag)
{
ans = 0;
break;
}//判断是否有生成树
if(ans)
{
ans = 0;
for(int i = 0; i <= n; i++)
father[i] = i;
for(int i = m-1; i >= 0; i--)
{
int a, b;
a = FIND(edge[i].u);
b = FIND(edge[i].v);
if(a != b)
{
l += edge[i].c;
father[a] = b;
}
}//计算白边最小值l
for(int i = l; i <= r; i++)
if(f[i])
{
ans = 1;
break;
}
}
if(ans)
cout << "Case #" << cse++ << ": Yes" << endl;
else
cout << "Case #" << cse++ << ": No" << endl;
}
}
return 0;
}
最近想改变一下代码风格···结果连字都快不会打了orz
LA 6540 Fibonacci Tree的更多相关文章
- 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 (2013ACMICPC 成都站 F)
http://acm.hdu.edu.cn/showproblem.php?pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) ...
- hdoj 4786 Fibonacci Tree【并查集+最小生成树(kruskal算法)】
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Fibonacci Tree(最小生成树,最大生成树)
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 4786 Fibonacci Tree 最小生成树
Fibonacci Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4786 Description Coach Pang is intere ...
- 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(生成树,YY乱搞)
http://acm.hdu.edu.cn/showproblem.php? pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others ...
- POJ 4786 Fibonacci Tree
Fibonacci Tree Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ...
随机推荐
- P1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会
裸的强连通 ; type node=record f,t:longint; end; var n,m,dgr,i,u,v,num,ans:longint; bfsdgr,low,head,f:arra ...
- 【BZOJ 1202】 [HNOI2005]狡猾的商人
Description 刁姹接到一个任务,为税务部门调查一位商人的账本,看看账本是不是伪造的.账本上记录了n个月以来的收入情况,其中第i 个月的收入额为Ai(i=1,2,3...n-1,n), .当 ...
- 1020: [SHOI2008]安全的航线flight - BZOJ
Description在设计航线的时候,安全是一个很重要的问题.首先,最重要的是应采取一切措施确保飞行不会发生任何事故,但同时也需要做好最坏的打算,一旦事故发生,就要确保乘客有尽量高的生还几率.当飞机 ...
- ExtJS4.2学习(20)动态数据表格之前几章总结篇1(转)
鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2014-02-18/196.html --------------- ...
- 弹窗开关js
// var guanbi = false; // $("#testbtn").click(function(){ // if(guanbi){ // $("#tan&q ...
- 【leetcode】3Sum Closest(middle)
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- cocos2d-x 扩充引擎基类功能 引起的头文件重复包含问题的分析
c++ 头文件包含 原因的分析: c++ 头文件的循环引用是指: .h 里面的里面的头文件的相互包含的,引起的重复引用的问题.cpp 里面包含头文件是不存在重复引用的问题(因为CPP没有#ifn ...
- 设置window窗口的背景色为护眼色
win7设置:桌面右键 -> 个性化 -> 窗口颜色 -> 高级外观设置 -> '项目'下拉菜单 -> '窗口'
- Servlet课程0424(一) 通过实现Servlet接口来开发Servlet
//这是我的第一个Servlet,使用实现Servlet接口的方式来开发 package com.tsinghua; import javax.servlet.*; import java.io.*; ...
- MyBatis的动态SQL操作--删除
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUYAAAC/CAIAAAANX+LCAAAYvElEQVR4nO2dWWycV9nHDyC6UEGBGy