# Need help with MST Queries problem. I am getting WA for few test cases.

``````Question link: https://www.codechef.com/problems/MSTQS. I am trying to solve it using Kruskal's Algorithm. I am getting WA for a few test cases and AC for a few. Any help would be highly appreciated. Below is my C++ code. Thank you in advance.

#include<bits/stdc++.h>
using namespace std;

int parent[2005];
int Ranking[2005];

void initialize(int n){
for(int i=1;i<=n;i++)
parent[i]=i;
for(int i=1;i<=n;i++)
Ranking[i]=0;
}

int findParent(int n){
if(parent[n]==n)
return n;
return parent[n]=findParent(parent[n]);
}

void Join(int x,int y){
int u=findParent(x);
int v=findParent(y);
if(Ranking[u]>Ranking[v])
parent[v]=u;
else if(Ranking[u]<Ranking[v])
parent[u]=v;
else{
parent[u]=v;
Ranking[v]++;
}
}

int main(){
int n,m,q;
cin>>n>>m>>q;
set<pair<int,pair<int,int>>> edges,mstedges;
for(int i=1;i<=m;i++){
int w,u,v;
cin>>u>>v>>w;
edges.insert({w,{u,v}});
}
initialize(n);
int mst=0;
for(auto x:edges){
int weight=x.first;
int u=x.second.first;
int v=x.second.second;
if(findParent(u)!=findParent(v)){
mst+=weight;
Join(u,v);
mstedges.insert({weight,{u,v}});
}
}
//cout<<mst<<endl;
//for(auto x:mstedges)
//cout<<x.first<<","<<x.second.first<<","<<x.second.second<<" ";
while(q--){
int qtype;
cin>>qtype;
if(qtype==1){
int u,v;
cin>>u>>v;
if(v>u)
swap(v,u);
mstedges.insert({0,{u,v}});
}
if(qtype==2){
int u,v;
cin>>u>>v;
if(v>u)
swap(v,u);
mstedges.erase({0,{u,v}});
}
if(qtype==3){
initialize(n);
int ans=0;
for(auto x:mstedges){
int weight=x.first;
int u=x.second.first;
int v=x.second.second;
if(findParent(u)!=findParent(v)){
ans+=weight;
Join(u,v);
}
}
cout<<ans<<endl;
}
}
return 0;
}
``````