首页 > 其他 > 详细

Hdoj 4786 Fibonacci Tree 【kruskal】

时间:2015-03-30 23:05:51      阅读:466      评论:0      收藏:0      [点我收藏+]

Fibonacci Tree

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

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
2013 Asia Chengdu Regional Contest
就是找出一条连通图的长度是FIBONACCI数的一个图。
我们找出连通图的长度最长的一条,和最短的一条,只要中间有FIBONACCI数,就一定能够达到。
代码:

#include <vector>
#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
const int M = 1e5+5;
using namespace std;

bool vis[M];
int fat[M], n, m, p[M], tot;
struct node{
    int u, v, val;
}e[M];

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

bool cmp1(node a, node b){
    return a.val > b.val;
}

bool cmp(node a, node b){
    return b.val > a.val;
}

void f(){
    p[0] = 1;p[1] = 2;
    tot = 2;
    while(p[tot-1]+p[tot-2] < M){
        p[tot] = p[tot-1]+p[tot-2];
        tot++;
    }
}

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

int kruscal(int f){
    init();
    if(f) sort(e, e+m, cmp1);
    else sort(e, e+m, cmp);
    int ans = 0;
    for(int i = 0; i < m; ++i){
        int x = find(e[i].u);
        int y = find(e[i].v);
        if(x != y){
            fat[x] = y;
            ans += e[i].val;
        }
    }
    return ans;
}

int main(){
    int t;
    f();
    cin >> t;
    for(int s = 1; s <= t; ++s){
        cin >> n>> m;
        for(int i = 0; i <m; ++ i)
            scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].val);
            //cin >>e[i].u>>e[i].v>>e[i].val;
        //init();
        int a = kruscal(1);
        int b = kruscal(0);
        bool flag = 0;
        for(int i = 2; i <= n; ++i)
        if(find(i) != find(1)){
            flag = 1; break;
        }
        printf("Case #%d: ", s);
        if(flag){
            cout << "No\n"; continue;
        }
        if(a > b) swap(a, b);
        for(int i = 0; i < tot&&p[i] <= b; ++ i){
            if(a <= p[i]&&b >= p[i]){
                flag = 1;
                break;
            }
        }
        if(flag) cout << "Yes\n";
        else cout << "No\n";
    }
    return 0;
}

Hdoj 4786 Fibonacci Tree 【kruskal】

原文:http://blog.csdn.net/shengweisong/article/details/44759651

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!