题目
题解思路
差点看错题目,我一开始以为是建树的限制浪费了挺多时间的。
结果是建边的限制,直接套模板即可。
坑点
AC代码
#include <iostream>
#include <cstdio>
#include <queue>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
struct node
{
int q,z;
double w;
}f[10100];
struct dian
{
int x,y;
}d[150];
int a[150];
int m ;
int n ;
double js(int x1,int y1,int x2,int y2 )
{
return sqrt(1.0*(x1-x2)*(x1-x2)+1.0*(y1-y2)*(y1-y2));
}
bool cmp(node A ,node B )
{
return A.w < B.w;
}
int find2(int x)
{
if ( x != a[x] )
{
a[x] = find2(a[x]);
}
return a[x];
}
int u(int x,int y)
{
int fx = find2(x);
int fy = find2(y);
if ( fx != fy )
{
a[fx] = fy;
return 1;
}
return 0;
}
void ku()
{
double ans = 0;
int pit = 0;
for (int i = 1 ; i <= m ; i++ )
{
if (u(f[i].q,f[i].z))
{
ans+=f[i].w;
pit++;
}
if ( pit == n -1 )
{
break;
}
}
// cout<<ans<<"\n";
if ( ans != 0 )
printf("%.1f\n",ans*100);
else
printf("oh!\n");
}
int main ()
{
int t;
scanf("%d",&t);
while(t--)
{
m = 0;
scanf("%d",&n);
for (int i = 1 ;i <= n ; i++ )
a[i] = i;
for (int i = 1 ; i <= n ; i++ )
{
int b,c;
scanf("%d%d",&b,&c);
d[i].x = b;
d[i].y = c;
for (int k = 1 ;k <= i-1 ; k++ )
{
double tmp = js(b,c,d[k].x,d[k].y);
// cout<<tmp<<"\n";
if ( tmp >=10.000000 && tmp<=1000.000001)
{
m++;
f[m].q = i;
f[m].z = k;
f[m].w = tmp ;
}
}
}
// cout<<m<<"\n";
sort(f+1,f+1+m,cmp);
ku();
}
return 0;
}