junior19

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Fibonacci Tree

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4696    Accepted Submission(s): 1475


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
题意:给n个点,m条边,每条边要么黑色要么白色,判断有无一种方案连通所有点且白色边数目为斐波拉契数。

思路:先排序一次将黑色边放前面,求一次最小生成树用到的白色边数,再排序一次将白色边放前面求一次最小生成树用到的白色边数,最后判断两数之间有无斐波拉契数即可。

# include <stdio.h>
# include <algorithm>
# define MAXN 100000
using namespace std;

int pre[MAXN+1], f[25]={1,1};
struct node
{
    int a, b, c;
}edge[MAXN+1];

bool cmp1(node a, node b)
{
    return a.c < b.c;
}

bool cmp2(node a, node b)
{
    return a.c > b.c;
}

void init(int n)
{
    for(int i=0; i<=n; ++i)
        pre[i] = i;
}

int find(int x)
{
    if(x != pre[x])
        pre[x] = find(pre[x]);
    return pre[x];
}

int main()
{
    int t, n, m, cas=1;
    for(int i=2; i<25; ++i)
        f[i] = f[i-1] + f[i-2];
    scanf("%d",&t);
    while(t--)
    {
        bool flag = false;
        int road=0, white1=0, white2=0;
        scanf("%d%d",&n,&m);
        init(n);
        for(int i=0; i<m; ++i)
            scanf("%d%d%d",&edge[i].a, &edge[i].b, &edge[i].c);
        sort(edge, edge+m, cmp1);
        for(int i=0; i<m; ++i)
        {
            int px = find(edge[i].a);
            int py = find(edge[i].b);
            if(px != py)
            {
                ++road;
                white1 += edge[i].c;
                pre[px] = py;
                if(road == n-1)
                    break;
            }
        }
        if(road < n-1)
        {
            printf("Case #%d: No\n",cas++);
            continue;
        }
        init(n);
        road = 0;
        sort(edge, edge+m, cmp2);
        for(int i=0; i<m; ++i)
        {
            int px = find(edge[i].a);
            int py = find(edge[i].b);
            if(px != py)
            {
                ++road;
                white2 += edge[i].c;
                pre[px] = py;
                if(road == n-1)
                    break;
            }
        }
        for(int i=0; i<25; ++i)
            if(white1 == f[i] || white2==f[i])
            {
                printf("Case #%d: Yes\n",cas++);
                flag = true;;
                break;
            }
        if(flag) continue;
        int pos1 = upper_bound(f, f+25, white1) - f;
        int pos2 = upper_bound(f, f+25, white2) - f;
        if(pos1 == pos2)
            printf("Case #%d: No\n",cas++);
        else
            printf("Case #%d: Yes\n",cas++);
    }
    return 0;
}


posted on 2017-02-16 22:17  junior19  阅读(114)  评论(0编辑  收藏  举报