Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

HackerEarth

#include<bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
 
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)
#define testcase int T; cin >> T; for (int tc = 1; tc <= T; tc++)
#define MAX 1000000007
#define Limit 1000000000
#define modulo 998244353
#define INF 4000000000000000005
#define vi vector<int>
#define vl vector<long long>
#define vpi vector<pair<int,int> >
#define pi pair<int,int>
#define pl pair<long long,long long>
#define ti tuple<int,int,int>
#define tl tuple<long long,long long,long long>
#define PI 2*acos(0.0)
#define pb push_back
#define mkp make_pair
 
 
const double threshold = (1.0/1000000.0);
const int nmax = 1e6+5;
 
ll parents[nmax];
ll degree[nmax];
 
void init(ll n)
{
    for(ll i=0;i<=n;i++)
    {
        parents[i]=i;degree[i]=0;
    }
}
 
ll find_parent(ll node)
{
    if(parents[node] == node)
    {
        return node;
    }
    else
    {
        return parents[node] = find_parent(parents[node]);
    }
}
 
bool union_parent(ll nodeA,ll nodeB)
{
    ll a = find_parent(nodeA);
    ll b = find_parent(nodeB);
 
    if(a!=b)
    {
       parents[b] = a;
 
       degree[nodeA]++;
       degree[nodeB]++;
 
       return true;
    }
    else
    {
        return false;
    }
}
 
int main()
{
    ll i,j,k,l,n,m,limit;
 
    testcase{
 
    cin>>n>>m;
 
    init(n);
 
    priority_queue<tl, vector<tl>, greater<tl> >qu;
 
    for(i=0;i<m;i++)
    {
        cin>>j>>k>>l;
        qu.push(make_tuple(l,-min(j,k),-max(j,k)));
    }
 
    tl node;ll sum=0LL;
    while(!qu.empty())
    {
        node = qu.top();qu.pop();
 
        tie(l,j,k) = node;j*=-1;k*=-1;
 
        if(union_parent(j,k)==true)
        {
            //cout<<j<<" "<<k<<endl;
           sum+=l;
        }
 
    }
 
    cout<<sum<<endl;
 
    for(i=1;i<=n;i++)
    {
        cout<<degree[i]<<" ";
    }
    cout<<endl;
 
    }
 
    return 0;
}
Source by pastebin.ubuntu.com #
 
PREVIOUS NEXT
Tagged: #HackerEarth
ADD COMMENT
Topic
Name
5+3 =