HDU 4786 Fibonacci Tree 最小生成树

来源:互联网 发布:mfc windows pdf下载 编辑:程序博客网 时间:2024/04/28 20:04

题目描述:

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 <= 10 5) and M(0 <= M <= 10 5).
  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

24 41 2 12 3 13 4 11 4 05 61 2 11 3 11 4 11 5 13 5 14 2 1 

Sample Output

Case #1: YesCase #2: No 

题目分析:

一个n个点,m条边的图,边有白色(1表示)黑色(0表示)两种情况,问是否存在这样一个生成树,其中白色边(1)的数量是斐波那契数列中的元素?
斐波那契递推公式:

fibo[i]=fibo[i-1]+fibo[i-2];

这道题是判断有无的,所以首先要做的是判断这个图是否是连通图,不是连通图直接No,然后只看白色边,将尽可能多的白色边都连起来看是否能使之成为树,找到最大值maxx,然后看黑色边,可以找到必须加入白色边(使之成为树)的最小值minn,然后在minn和maxx间寻找是否有斐波那契数。(因为白色边权是1,黑色边权是0,所以用Kruskal返回的权值就是白色边数)

代码如下:

#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;typedef long long ll;const int INF = 0x3f3f3f3f;const int MAXN=100010;struct sa{    int u,v,c;}mp[MAXN];int fi[30];int f[MAXN];int T;int n,m;int getfibo(){    fi[0]=1,fi[1]=2;    for(int i=2; ; i++)    {        fi[i]=fi[i-1]+fi[i-2];        if(fi[i]>100000)        {return i;break;}    }}int findfather(int x){    if (x!=f[x])        return f[x]=findfather(f[x]);    else        return f[x];}int solve(int col){    int num=0;    for(int i=1; i<=n; i++)f[i]=i;    for(int i=1; i<=m; i++)    {        if(mp[i].c!=col)        {            int x=findfather(mp[i].u);int y=findfather(mp[i].v);            if(x!=y)            {                f[x]=y;                num++;            }        }    }    return num;}int main(){    scanf("%d",&T);    for(int t=1; t<=T; t++)    {        scanf("%d%d",&n,&m);        for(int i=1; i<=m; i++)        {            scanf("%d%d%d",&mp[i].u,&mp[i].v,&mp[i].c);        }        printf("Case #%d: ",t);        int maxx=solve(0);        int minn=n-1-solve(1);        int tmp=solve(2);        if (tmp!=n-1)        {            printf("No\n");continue;        }        bool flag=false;        for(int i=0; i<=getfibo(); i++)        {            if (fi[i]>=minn && fi[i]<=maxx) {flag=true;break;}        }        if (flag) printf("Yes\n");        else printf("No\n");    }    return 0;}
0 0
原创粉丝点击