zoukankan      html  css  js  c++  java
  • uva 11747,kruskal 并查集

    Problem F: Heavy Cycle Edges

    Given an undirected graph with edge weights, a minimum spanning tree is a subset of edges of minimum total weight such that any two nodes are connected by some path containing only these edges. A popular algorithm for finding the minimum spanning tree T in a graph proceeds as follows:

    • let T be initially empty
    • consider the edges e1, ..., em in increasing order of weight
      • add ei to T if the endpoints of ei are not connected by a path in T

    An alternative algorithm is the following:

    • let T be initially the set of all edges
    • while there is some cycle C in T
      • remove edge e from T where e has the heaviest weight in C

    Your task is to implement a function related to this algorithm. Given an undirected graph G with edge weights, your task is to output all edges that are the heaviest edge in some cycle of G.

    Input Format

    The first input of each case begins with integers n and m with 1 ≤ n ≤ 1,000 and 0 ≤ m ≤ 25,000 where n is the number of nodes and m is the number of edges in the graph. Following this are m lines containing three integers u, v, and w describing a weight w edge connecting nodes u and v where 0 ≤ u, v < n and 0 ≤ w < 231. Input is terminated with a line containing n = m = 0; this case should not be processed. You may assume no two edges have the same weight and no two nodes are directly connected by more than one edge.

    Output Format

    Output for an input case consists of a single line containing the weights of all edges that are the heaviest edge in some cycle of the input graph. These weights should appear in increasing order and consecutive weights should be separated by a space. If there are no cycles in the graph then output the text forest instead of numbers.

    Sample Input

    3 3
    0 1 1
    1 2 2
    2 0 3
    4 5
    0 1 1
    1 2 2
    2 3 3
    3 1 4
    0 2 0
    3 1
    0 1 1
    0 0
    

    Sample Output

    3
    2 4
    forest
    

    Zachary Friggstad

    参考了网上题解。

    并查集,kruskal变形,很巧妙的算法。。。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    #include<map>
    #include<stack>
    
    using namespace std;
    
    #define LL long long
    #define UINT unsigned int
    #define MAX_INT 0x7fffffff
    #define cint const int
    
    #define MAXN 1111
    #define MAXM 25555
    
    struct edge{
        int u, v, w, nxt;
        bool operator < (const edge &rhs)const{
            return w<rhs.w;
        }
    }e[MAXM];
    int h[MAXN], cc, n, m;
    
    void add(int u, int v, int w){
        e[cc]=(edge){u, v, w, h[u]};
        h[u]=cc++;
    }
    
    bool ve[MAXM];
    int p[MAXN];
    
    int finds(int x){
        if(p[x]==-1) return x;
        else return (p[x] = finds(p[x]));
    }
    
    void mst(){
        sort(e, e+cc);
        int i, j;
        fill_n(p, n, -1);
        fill_n(ve, cc, false);
        for(i=j=0; i<cc; i++){
            int fx = finds(e[i].u), fy = finds(e[i].v);
            if(fx!=fy){
                p[fx] = fy;
                ve[i] = true;
            }
        }
    }
    
    int main(){
    //    freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        while(scanf(" %d %d", &n, &m)==2 && (n || m)){
            if(!n || !m){
                printf("forest
    ");        continue;
            }
            int i, u, v, w;
            fill_n(h, n, -1);       cc=0;
            for(i=0; i<m; i++){
                scanf(" %d %d %d", &u, &v, &w);
                add(u, v, w);
            }
    
            mst();
            vector<int> ans;
            for(i = 0; i < cc; i++) if(!ve[i])
                ans.push_back(e[i].w);
            if(!ans.size()){
                printf("forest
    ");     continue;
            }
            printf("%d", ans[0]);
            for(i=1; i<ans.size(); i++)
                printf(" %d", ans[i]);
            printf("
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    深入正则表达式(0):正则表达式概述
    讲透学烂二叉树(二):图中树的定义&各类型树的特征分析
    讲透学烂二叉树(一):图的概念和定义—各种属性特征浅析
    Gzip之后继者Brotli浅析之CDN厂商的智能压缩,服务器Brotli设置
    ECMAScript进化史(1):​话说Web脚本语言王者JavaScript的加冕历史
    nginx网站限速限流配置——网站被频繁攻击,nginx上的设置limit_req和limit_conn
    linux添加用户,修改用户密码,修改用户权限,设置root用户操作
    nginx 限制ip访问,禁止非法域名指向本机ip——防止被别人绑定域名到自己IP的方法
    centos8 新增ssh自定义端口与屏蔽默认22端口。
    1g云主机升级centos8不满足centos 8 至少2g内存要求,linux虚拟内存来凑
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3375336.html
Copyright © 2011-2022 走看看