For those getting WA for GALACTIK.....

check out the following test cases…

  1. 8 6,1 2,2 3,4 5,5 6,6 7,7 4,1,2,-1,-1,-1,-1,-1,-1 ans=-1
  2. 6 5,1 2,4 5,5 6,6 3,3 1,1,2,3,4,5,6 ans=0
  3. 10 8,1 2,2 3,4 5,5 6,7 8,8 9,3 1,6 2,1,2,3,-1,-1,5,-1,-1,-1,2 ans=-1

my solution is giving right ans for all the above test cases bt Wrong Answer on submission…Here is the link to my solution… CodeChef: Practical coding for everyone …plz help…

#include <bits/stdc++.h>
using namespace std;
int main(){
long long int n,m;std::cin >> n>>m;
std::vectorgraph[n+1] ;
while(m–){
long long int x,y;std::cin >> x>>y;
graph[x].push_back(y);
graph[y].push_back(x);
}
long long int arr[n+1];
for(int i=1;i<=n;i++){
std::cin >> arr[i];
}
long int parent[n+1]={-1};
bool visited[n+1]={false};
for(int i=1;i<=n;i++){
if(visited[i]==false){
visited[i]=true;parent[i]=i;
std::queuetemp ;temp.push(i);
while(!temp.empty()){
long long int p=temp.front();temp.pop();
for(auto it=graph[p].begin() ; it!=graph[p].end() ; it++){
if(visited[*it]==false){
visited[*it]=true;parent[*it]=parent[p];
temp.push(*it);
}
}
}
}
}
std::setkitne ;
for(int i=1;i<=n;i++){
kitne.insert(parent[i]);
}
unordered_map<long long int,long long int>tejus;
for(auto it=kitne.begin();it!=kitne.end() ; it++){
tejus[it]=1000000000;
}
for(int i=1;i<=n;i++){
if(tejus[parent[i]]>arr[i]&&arr[i]>=0){
tejus[parent[i]]=arr[i];
}
}
bool done=true;int min=INT_MAX;int sum=0;
for(auto it=tejus.begin();it!=tejus.end();it++){
if(it->second==1000000000){
done=false;break;
}
sum=sum+it->second;
if(min>it->second){
min=it->second;
}
}
if(done==true){
int jk=kitne.size();jk-=2;
min
=jk;
sum+=min;
std::cout << sum << std::endl;
}
else{
std::cout << -1 << std::endl;
}
return 0;
}

I AM GETTING WA ,but your cases are answered correctly