以前做过的题···重新做一遍之后怎么做怎么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的更多相关文章

  1. HDU 4786 Fibonacci Tree

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

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

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

  3. hdoj 4786 Fibonacci Tree【并查集+最小生成树(kruskal算法)】

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

  4. Fibonacci Tree(最小生成树,最大生成树)

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

  5. HDU 4786 Fibonacci Tree 最小生成树

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

  6. HDU 4786 Fibonacci Tree (2013成都1006题)

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

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

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

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

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

  9. POJ 4786 Fibonacci Tree

    Fibonacci Tree Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ...

随机推荐

  1. 为cocos2d-x实现安卓输入框。非全屏,无dialog,绑定到lua

    cocos2d-x官方自带的输入框,简直惨不忍睹,在ios还好,在安卓简直了..用过的都知道... 所以为了用户体验,我们自己搞一个吧.输入框这种东西比较特殊,不像按钮.列表框之类的很容易实现,因为涉 ...

  2. ParentChildTest.java

    public class ParentChildTest { public static void main(String[] args) { Parent parent=new Parent(); ...

  3. eclipse(Version: Neon Release (4.6.0))安装hibernate tools

    这里需要说明的是,hibernate tools是jboss推出的. eclipse——>Eclipse Marketplace... 输入jboss-tools进行搜索 选择jboss too ...

  4. @Repository、@Service、@Controller 和 @Component(转)

    鸣谢:http://blog.csdn.net/ye1992/article/details/19971467 @Repository.@Service.@Controller 和 @Componen ...

  5. oracle新建用户

    说明:以下命令在PLSQL中运行 一.以管理员身份登录PLSQL scott/root as sysdba 二.创建新用户 create user extjsTest1 identified by r ...

  6. js注册登录审核

    <script type="text/javascript"> $(function(){ $("#sendSms").click(function ...

  7. HTTP协议的几个重要概念

    转自:http://ice-cream.iteye.com/blog/77549 1.连接(Connection):一个传输层的实际环流,它是建立在两个相互通讯的应用程序之间. 2.消息(Messag ...

  8. ASP + ACCESS保存图片文件之实现

    con.execute "CREATE tblImg (lngId COUNTER PRIMARY KEY, binImg IMAGE)" set ads=createobject ...

  9. mac忘记密码的解决办法

    开机, 启动时按"cmd+S".这时,你会进入Single User Model,出现像DOS一样的提示符 #root>.请在#root>下 输入 (注意空格, 大小写 ...

  10. Google chrome的字体设置

    http://blog.sina.com.cn/s/blog_a3b863da01016sv3.html 谷歌浏览器(Google chrome)速度很快,很好用.问题是字体显示有时候不对:用英文版的 ...