i m getting TLE for this problem...can nebody tell me how can i improve the efficiency

here is the code

Code: Select all

```
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<set>
using namespace std;
struct edges{
int v1,v2;
int w;
};
struct point{
int x,y;
};
bool compare(const edges& e1,const edges& e2)
{
return e1.w<e2.w;
}
int find_set(int v,vector< set<int> >vset)
{
for(int i=0;i<vset.size();i++)
{
set<int>::iterator p;
p = vset[i].find(v);
if(p != vset[i].end())
return i;
}
return -1;
}
int dist(int x1,int y1,int x2,int y2)
{
int d,dx,dy;
dx=x1-x2; dy=y1-y2;
d=(dx*dx)+(dy*dy);
return d;
}
int main()
{
int kases;
cin>>kases;
bool first=true;
while(kases--)
{
if(!first)
cout<<endl;
first=false;
int n;
cin>>n;
vector< set<int> > vset(n);
for(int i=0;i<n;i++)
{
set<int> s;
s.insert(i);
vset[i]=s;
}
vector<edges> E;
vector<point> P;
edges e;
point p;
for(int i=0;i<n;i++)
{
cin>>p.x>>p.y;
for(int j=0;j<P.size();j++)
{
int w=dist(P[j].x,P[j].y,p.x,p.y);
e.v1=j; e.v2=i; e.w=w;
E.push_back(e);
}
P.push_back(p);
}
sort(E.begin(),E.end(),&compare);
//Already built highways
int m,hw=0;
cin>>m;
for(int i=0;i<m;i++)
{
int x,y;
cin>>x>>y;
x--; y--;
if(x!=y)
{
int mn=min(x,y),mx=max(x,y);
vset[mn].insert(vset[mx].begin(),vset[mx].end());
vset[mx].clear();
hw++;
}
}
//New highways to be built
bool newh=false;
for(int i=0;i<E.size();i++)
{
int x=find_set(E[i].v1,vset);
int y=find_set(E[i].v2,vset);
if(x!=y)
{
newh=true;
cout<<E[i].v1+1<<" "<<E[i].v2+1<<endl;
int mn=min(x,y),mx=max(x,y);
vset[mn].insert(vset[mx].begin(),vset[mx].end());
vset[mx].clear();
hw++;
}
if(hw==n-1)
break;
}
if(!newh)
cout<<"No new highways need"<<endl;
}
return 0;
}
```

before using sets, i tried vectors n still got TLE