HDU4736-Fibonacci Tree
来源:互联网 发布:现货分时指标公式源码 编辑:程序博客网 时间:2024/06/11 03:37
Fibonacci Tree
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2509 Accepted Submission(s): 801
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).
24 41 2 12 3 13 4 11 4 05 61 2 11 3 11 4 11 5 13 5 14 2 1
Case #1: YesCase #2: No
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#define maxn 100000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
bool cmp(int a,int b){
return a>b;
}
int fb[30];
int fa[maxn];
void f()
{
fb[1]=1;
fb[2]=1;
for(int i=3;i<=30;i++)
fb[i]=fb[i-1]+fb[i-2];
}
void init(int n)
{
for(int i=1;i<=n;i++)
fa[i]=i;
}
int findfa(int x)
{
if(x==fa[x])return x;
else return fa[x]=findfa(fa[x]);
}
void Union(int x,int y)
{
int fa_x=findfa(x);
int fa_y=findfa(y);
fa[fa_x]=fa_y;
}
struct node
{
int u,v,w;
}edge[maxn];
bool cmp1(node a,node b)
{
return a.w<b.w;
}
bool cmp2(node a,node b)
{
return a.w>b.w;
}
int n,m;
int Kruskal(int k)
{
init(n);
if(k==1)sort(edge+1,edge+1+m,cmp1);
if(k==2)sort(edge+1,edge+1+m,cmp2);
int cnt=1;
int ans=0;
for(int i=1;i<=m;i++)
{
if(findfa(edge[i].u)!=findfa(edge[i].v))
{
Union(edge[i].u,edge[i].v);
ans+=edge[i].w;
cnt++;
}
}
if(cnt==n)
{
return ans;
}
else return 0;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t;
cin>>t;
f();
for(int i=1;i<=t;i++)
{
cout<<"Case #"<<i<<": ";
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
int t1=Kruskal(1);
int t2=Kruskal(2);
int mark=0;
for(int i=1;fb[i]<=t2;i++)
{
if(t1<=fb[i]&&fb[i]<=t2){mark=1;cout<<"Yes"<<endl;break;}
}
if(!mark)cout<<"No"<<endl;
}
return 0;
}
- HDU4736-Fibonacci Tree
- Fibonacci Tree
- Fibonacci Tree
- Fibonacci Tree
- [HDU4786]Fibonacci Tree
- HDU 4786 Fibonacci Tree
- hdu 4786 Fibonacci Tree
- HDOJ 4786 Fibonacci Tree
- hdu 4786 Fibonacci Tree
- HDU 4786 Fibonacci Tree
- hdu 4786 Fibonacci Tree
- hdu 4786 Fibonacci Tree
- LA 6540 Fibonacci Tree
- hdu 4786 Fibonacci Tree
- HDU 4786 Fibonacci Tree
- HDU-4786-Fibonacci Tree
- HDU-4786 Fibonacci Tree
- Fibonacci Tree 4786
- A Mathematical Curiosity
- HDU2771 Stealing Harry Potter\'s Precious(bfs+dfs)
- do-while随机求十个数的和
- HDU4493 Tutor
- HDU4496 D-City
- HDU4736-Fibonacci Tree
- 快速排序的递归形式与非递归形式(C++版)
- PCA的数学原理
- HDU4802 GPA
- HDU 5059 help him
- ACM牡丹江区域赛
- 英语演讲
- HDU1241 Oil Deposits
- #267B. Fedor and New Game-----每日两道水(1)