ENG  RUS Timus Online Judge
Online Judge
Problems
Authors
Online contests
Site news
Webboard
Problem set
Submit solution
Judge status
Guide
Register
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

## Discussion of Problem 1982. Electrification Plan

What is wrong in this?
Posted by Aditya Singh 4 Jan 2018 04:11
#include <stdio.h>
#include <iostream>
#include <limits>
#include <cstdint>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <array>
#include <string.h>
#include <stack>
#include <queue>
#include <stdint.h>
#include <sstream>
#include <map>
#include <set>
#define mem(x, y) memset(x, y, sizeof x);
#define __STDC_LIMIT_MACROS
typedef long long ll;
using namespace std;

struct Edge{
int s,d,w;
Edge(){
this->s = this->d = this->w = NULL;
}
Edge(int s, int d, int w){
this->s = s;
this->d = d;
this->w = w;
}
};

bool myComp(struct Edge a, struct Edge b){
return a.w<b.w;
}

int mfind(int parent[], int u){
if(parent[u]==-1){
return u;
}

parent[u] = mfind(parent,parent[u]);

return parent[u];
}

void Union(int parent[], int rank[], int u, int v){
int x = mfind(parent,u);
int y = mfind(parent,v);

if(rank[x]<rank[y]){
parent[x] = y;
}
else if(rank[y]< rank[x]){
parent[y] = x;
}
else{
parent[x] = y;
rank[y]++;
}
}

int main() {

vector<Edge> edges;
int n,k,tot=0;
cin >> n >> k;
int check[n];
mem(check,0);
for(int i=0; i<k; i++){
int x;
cin >> x;
check[x-1] = 1;
}

for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
int inp;
cin >> inp;
if(i>j){
struct Edge ed(i,j,inp);
edges.push_back(ed);
}
}
}

sort(edges.begin(), edges.end(), myComp);

// KRUSKAL MST
int parent[n], rank[n];
mem(parent,-1);
mem(rank,0);
int e=0;

while(e<n-1){
struct Edge next_edge = edges[e++];

int x = mfind(parent, next_edge.s);
int y = mfind(parent, next_edge.d);

if(x!=y){
tot+=next_edge.w;
if(check[next_edge.s]==1 && check[next_edge.d]==1) tot-=next_edge.w;
e++;
Union(parent,rank,x,y);
}

}

cout << tot << endl;

return 0;

}