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 ;
}

最新文章

  1. 邻接矩阵有向图(二)之 C++详解
  2. AOP动态代理解析5-cglib代理的实现
  3. checkbox 的全选与全不选
  4. Android自动化压力测试快速入门教程(图解)——MonkeyRunner
  5. Camera 图像处理原理分析
  6. 数据库中GUID的生成
  7. c#中格式化导出Excel数据
  8. Network Link Conditioner模拟不同网络环境
  9. android开发之Fragment加载到一个Activity中
  10. Android一次退出所有Activity的方法(升级版)
  11. [ACM] poj 1064 Cable master (二进制搜索)
  12. Windows 2008 安装Sql server 2005
  13. TortoiseGit连接gitlab,一直要求输入密码
  14. 100以内的质数(for和if)
  15. SpringBoot拦截器中无法注入bean的解决方法
  16. 〖Linux〗安装和使用virtualenv,方便多个Python版本中切换
  17. x64枚举DPC定时器
  18. XShell通过中转服务器直接连接目标服务器
  19. hdu 5701 中位数计数 思路题
  20. vim编辑器操作汇总

热门文章

  1. 判断线段之间的关系(D - Intersecting Lines POJ - 1269 )
  2. 基于I2C总线的0.96寸OLED显示屏驱动
  3. Dream_Spark版本定制第一课
  4. 解决chrome运行报错unknown error: cannot get automation extension
  5. docker 要点学习
  6. 使用 Application Loader提交IPA文件到苹果市场
  7. jQuery下的onChange事件在某些情况下无效
  8. Cap+Exceptionless实现日志消息发布订阅异常情况日志处理及Cap DashBoard授权处理
  9. JavaScript中变量的相互引用
  10. NET 异步多线程,THREAD,THREADPOOL,TASK,PARALLEL