11747 - Heavy Cycle Edges

#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAX 25000
using namespace std;

int par[1009];
vector<int>cost;

struct node
{
    int x,y,weight;
} arr[MAX];

int khoj_rep(int r)
{
    if(par[r]==r)
    {
        return r;
    }
    else
    {
        return par[r]=khoj_rep(par[r]);
    }
}

bool cmp(node lhs,node rhs)
{
    return lhs.weight<rhs.weight;
}

int main()
{
    int koyta_node,koyta_edge,i,j,k,counter,u,v,sum;
    while(cin>>koyta_node>>koyta_edge)
    {
        if(koyta_node==0 && koyta_edge==0)
        {
            break;
        }

        for(i=0; i<koyta_node; i++)
        {
            par[i]=i;
        }

        for(i=0; i<koyta_edge; i++)
        {
            cin>>arr[i].x>>arr[i].y>>arr[i].weight;
        }

        sort(arr,arr+koyta_edge,cmp);

        counter=0;
        sum=0;
        for(j=0; j<i; j++)
        {
            u=khoj_rep(arr[j].x);
            v=khoj_rep(arr[j].y);
            if(u!=v)
            {
                par[u]=v;
                sum+=arr[j].weight;
            }
            if(u==v)
            {
                counter++;
                cost.push_back(arr[j].weight);
            }
        }

        if(counter>0)
        {
            for(k=0; k<counter; k++)
            {
                cout<<cost[k];
                if(k<counter-1)
                {
                    cout<<" ";
                }
            }
        }
        else
        {
            cout<<"forest";
        }
        cout<<endl;
        memset(par,0,sizeof(par));
        cost.clear();
    }
    return 0;
}

0 comments: (+add yours?)