You are on page 1of 3

KRUSKALS ALGORITHM IMPLEMENTATION IN O(E*ackerman(V))

POR: ALEI, REYES


Problem: Bytelandian Blingors Network
We have discovered the fastest communication medium Bytelandian scientists announced, and they called it
blingors. The blingors are incomparably better than other media known before. Many companies in Byteland started
to build blingors networks, so the information society in the kingdom of Bytes is fact! The priority is to build the
core of the blingors network, joinig main cities in the country. Assume there is some number of cities that will be
connected at the beginning. The cost of building blingors connection between two cities depends on many elements,
but it has been successfully estimated. Your task is to design the blingors network connections between some cities
in this way that between any pair of cities is a communication route. The cost of this network should be as small as
possible.
Remarks:
The name of the city is a string of at most 10 letters from a,...,z.
The cost of the connection between two cities is a positive integer.
The sum of all connections is not greater than 232-1.
The number of cities is not greater than 10 000.
Input
s [number of test cases <= 10]
n [number of cities <= 10 000]
NAME [city name]
p [number of neigbouring cities to the city NAME]
neigh cost
[neigh - the unique number of city from the main list
cost - the cost of building the blingors connection from NAME to neigh]
[empty line between test cases]
Output
[separate lines] cost [the minimum cost of building the blingors network]
Solution implemented in C++ 4.6
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<list>
#include<utility>
#include<vector>
using namespace std;
const int MXW = 10+5;
const int MX = 1e4+10;
char wrd[MXW];
typedef long long int uli;
typedef pair<uli,pair<int,int> >edge;
int E;
int parent[MX];
int size[MX];

int find(int u){


int node = u;
while(node!=parent[node])
node = parent[node];
return node;
}
int join(int u, int v){
if(size[u]>size[v]){
parent[v] = u;
size[u]+=size[v];
}
else{
parent[u] = v;
size[v]+=size[u];
}
}
int main(){
//
freopen("data.in","r",stdin);
int T,n,childs,u,ans,up,vp;
uli v;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=0;i<n;i++){
parent[i] = i;
size[i] = 1;
}
E = 0;
vector<edge>edges;
for(int i=0;i<n;i++){
scanf("%s",wrd);
scanf("%d",&childs);
for(int j=0;j<childs;j++){
scanf("%d %lld",&u,&v);
edges.push_back(make_pair(v,make_pair(i,u-1)));
E++;
}
}
sort(edges.begin(),edges.end());
edge e;
uli ans = 0;
for(int i=0;i<E;i++){
e = edges[i];
u = e.second.first;
v = e.second.second;
up = find(u);
vp = find(v);
if(up!=vp){
join(up,vp);
ans += e.first;
}
}
printf("%lld\n",ans);
}
return 0;

You might also like