Friday, February 10, 2017

UVa 11747 - Heavy Cycle Edges

// aarifshuvo   ``CSEJU

#include <bits/stdc++.h>
#define ll long long
#define SZ(x) ((int)(x).size())
#define scl(x) scanf("%lld", &x)
#define scll(x,y) scanf("%lld %lld", &x, &y)
#define ALL(x) (x).begin(),(x).end()
#define REP(i,n) for(int i=0;i<n;i++)
#define REV(i,n) for(int i=n-1;i>=0;i--)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define pri(a) cout<<a<<endl
#define prii(a,b) cout<<a<<" "<<b<<endl
using namespace std;

struct edge
{
    int a,b,c;
    edge(int _a, int _b, int _c)
    {
        a = _a;
        b = _b;
        c = _c;
    }
    bool operator < (const edge p) const
    {
        return c < p.c;
    }
};

vector <edge> edges;
int par[26000];

int findr(int r)
{
    if(par[r]==r) return r;
    int x = findr(par[r]);
    par[r]  = x;
    return x;
}

int main()
{
    vector <int> res;
    ll n,e,wt,x,y,z;

    while(scll(n,e)==2 and n+e)
    {
        res.clear();
        edges.clear();

        REP(i,e)
        {
            scll(x,y);
            scl(z);
            edges.push_back(edge(x,y,z));
        }

        sort(ALL(edges));
        REP(i,n+1) par[i]= i;
        REP(i,SZ(edges))
        {
            int u = findr(edges[i].a),
                v = findr(edges[i].b);

            if(u!=v)
            {
                par[v] = u;
            }
            else res.push_back(edges[i].c);
        }

        if(SZ(res)==0) puts("forest");
        else
        {
            bool fl = 0;
            REP(i,SZ(res))
            {
                if(fl) putchar(' '); fl = 1;
                printf(" %d", res[i]);
            }
            puts("");
        }
    }
    return 0;
}


// ideas (unsorted)



what did the problem want?

undirected graph with edge weights,

n(1k) nodes ...  edges(25k).... wt up to 2^31

n0 m0 terminate()

no loop

do mst and print the edges which are not used for avoiding cycle
and print them

No comments:

Post a Comment