zoukankan      html  css  js  c++  java
  • Fibonacci Tree 最小生成树

    Fibonacci Tree

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


    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
     
    Recommend
    We have carefully selected several similar problems for you:  4789 4788 4787 4785 4784 
    const int INF = 1000000000;
    const double eps = 1e-8;
    const int maxn = 300000;
    vector<int> g[maxn];
    struct Edge
    {
        int a;
        int b;
        int c;
        Edge(int _a,int _b,int _c):a(_a),b(_b),c(_c){}
    };
    void init()
    {
        repf(i,1,100010)
            g[i].clear();
    }
    vector<Edge> edges;
    int p[maxn];
    int find(int x)
    {
        if(p[x] == x)
            return x;
        else
        {
            p[x] = find(p[x]);
            return p[x];
        }
    }
    
    int kruskal()
    {
        int ans = 0;
        rep(i,0,edges.size())
        {
            int x = find(edges[i].a);
            int y = find(edges[i].b);
            if(x != y)
            {
                p[x] = y;
                ans += edges[i].c;
            }
        }
        return ans;
    }
    
    bool cmp1(Edge e1,Edge e2)
    {
        return e1.c > e2.c;
    }
    bool cmp2(Edge e1,Edge e2)
    {
        return e1.c < e2.c;
    }
    
    int vis[maxn];
    
    int vis1[maxn];
    void dfs(int x)
    {
        if(vis1[x]) return ;
        vis1[x] = 1;
        rep(i,0,g[x].size())
            dfs(g[x][i]);
    }
    int main() 
    {
        //freopen("in.txt","r",stdin);
        int T;
        scanf("%d",&T);
        clr(vis);
        vis[1] = 1;
        int a = 1;
        int b = 1;
        while(a + b <= 100000)
        {
            vis[a + b] = 1;
            int t = a;
            a = b;
            b = t + b;
        }
        int k = 1;
        while(T--)
        {
            edges.clear();
            init();
            clr(vis1);
            int n,m;
            scanf("%d%d",&n,&m);
            repf(i,1,n) p[i] = i;
            repf(i,1,m)
            {
                int a,b,c;
                scanf("%d%d%d",&a,&b,&c);
                g[a].push_back(b);
                g[b].push_back(a);
                edges.push_back(Edge(a,b,c));
            }
            dfs(1);
            int tag = 1;
            repf(i,1,n) if(vis1[i] == 0) 
            {
                tag = 0;break;
            }
            if(tag == 0)
            {
                printf("Case #%d: ",k++);
                cout<<"No"<<endl;
                continue;
            }
            sort(edges.begin(),edges.end(),cmp1);
            
            int  Max = kruskal();
            repf(i,1,n) p[i] = i;
            sort(edges.begin(),edges.end(),cmp2);
            
            int Min = kruskal();
            
            int flag = 0;
            repf(i,Min,Max)
                if(vis[i])
                {
                    flag =1;
                    break;
                }
            printf("Case #%d: ",k++);
            if(flag)
                cout<<"Yes"<<endl;
            else
                cout<<"No"<<endl;
        }
        return 0;
    }
  • 相关阅读:
    【C++】Lambda表达式
    使用velodyne16线激光雷达跑loam-velodyne
    IMU(LPMS-B2) ROS下使用教程
    【C++】关键字inline
    OpenCV中feature2D——BFMatcher和FlannBasedMatcher
    CUDA 编程
    进程(process)和线程(thread)的区别
    【C++】源自指针的报错
    【C++】如何接收函数return返回来的数组元素
    远程访问服务器tensorboard
  • 原文地址:https://www.cnblogs.com/DreamHighWithMe/p/3446035.html
Copyright © 2011-2022 走看看