HDU-4786-Fibonacci Tree

来源:互联网 发布:mac safari视频下载 编辑:程序博客网 时间:2024/05/17 08:49

HDU-4786-Fibonacci Tree


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

题目链接:HDU-4786

题目思路:最小生成树,求出1最少的情况a和最多的情况b,在判断在在[a,b]区间是否存在至少一个斐波那契数列的值。

以下是代码:

#include <vector>#include <map>#include <set>#include <algorithm>#include <cstdio>#include <cmath>#include <cstdlib>#include <string>#include <cstring>using namespace std;struct node{    int l,r;    int c;  }p[100010];int fa[100010];int n,m;bool cmp1(node a,node b){    if (a.c != b.c)        return a.c < b.c;    return a.l < b.l;}bool cmp2(node a,node b){    if (a.c != b.c)        return a.c > b.c;    return a.l < b.l;}int find(int x)     //并查集{    return fa[x] == x ? x : fa[x] = find(fa[x]);}int solve()    //最小生成树{    for (int i = 0; i <= n; i++)    fa[i] = i;    int cnt = 0;    for (int i = 0; i < m; i++)    {        int u = find(fa[p[i].l]);        int v = find(fa[p[i].r]);        if (p[i].c == 0)        {            if (u != v)            {                fa[v] = u;            }                       }        else        {            if (u != v)            {                fa[v] = u;                cnt++;            }                       }    }    return cnt; }int main(){    int t;    scanf("%d",&t);    long long f[105] = {0};    f[0] = 1;    f[1] = 1;    f[2] = 2;    for (int i = 3; i < 100; i++)    {        f[i] = f[i - 1] + f[i - 2];         }    int ret = 1;    while(t--)    {        scanf("%d%d",&n,&m);        for (int i = 0; i < m; i++)        {            scanf("%d%d%d",&p[i].l,&p[i].r,&p[i].c);        }        sort(p,p + m,cmp1);        int cnt1 = solve();    // 1最少的情况,即先把0全部排完,再接入1        int cou1 = 0;        for (int i = 1;i <= n; i++)  //判断是否连通        {            if (fa[i] == i)                cou1++;        }           sort(p,p + m,cmp2);        int cnt2 = solve();   //1最多的情况,先接1再补充0的情况        int cou2 = 0;        for (int i = 1;i <= n; i++)        {            if (fa[i] == i)                cou2++;        }        if (cou2 != 1 || cou1 != 1)        {            printf("Case #%d: No\n",ret++);        }        else        {            int flag = 0;            for (int i = 0; i < 50; i++)  //判断在[cnt1,cnt2]范围内是否存在至少一个斐波那契的数            {                if (f[i] >= cnt1 && f[i] <= cnt2)                {                    printf("Case #%d: Yes\n",ret++);                    flag = 1;                    break;                }                if (f[i] > cnt2)                    break;            }            if (!flag)                printf("Case #%d: No\n",ret++);        }    }            return 0;}
0 0
原创粉丝点击