-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path10147.cpp
More file actions
95 lines (88 loc) · 1.21 KB
/
10147.cpp
File metadata and controls
95 lines (88 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
long i,j,k,t,n,m,p[1000],r[1000],x[1000],y[1000],u,v,fg,s1,s2;
struct ss
{
long u,v;
double cost;
}T[1010000];
struct re
{
long u,v;
}TT[1010000];
bool cmp(ss aa,ss bb)
{
return aa.cost<bb.cost;
}
long find(long x)
{
if(x==p[x])
return x;
else return p[x]=find(p[x]);
}
void link(long x,long y)
{
if(r[x]>=r[y])
p[y]=x,r[x]++;
else
p[x]=y,r[y]++;
}
int main()
{
scanf("%ld",&t);
while(t--)
{
scanf("%ld",&n);
for(i=1;i<=n;i++)
{
p[i]=i;
r[i]=1;
scanf("%ld%ld",&x[i],&y[i]);
}
k=0;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
T[k].u=i;
T[k].v=j;
T[k++].cost=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}
}
sort(T,T+k,cmp);
scanf("%ld",&m);
for(i=1;i<=m;i++)
{
scanf("%ld%ld",&s1,&s2);
u=find(s1);
v=find(s2);
if(u!=v)
link(u,v);
}
fg=j=0;
for(i=0;i<k;i++)
{
u=find(T[i].u);
v=find(T[i].v);
if(u!=v)
{
link(u,v);
TT[j].u=T[i].u;
TT[j++].v=T[i].v;
fg=1;
}
}
if(!fg)
printf("No new highways need\n");
else
{
for(i=0;i<j;i++)
printf("%ld %ld\n",TT[i].u,TT[i].v);
}
if(t)
printf("\n");
}
return 0;
}