Problem C:

Nicholas Y. Alford was a cat lover. He had a garden in a village and kept many cats in his garden. The cats were so cute that people in the village also loved them.

One day, an evil witch visited the village. She envied the cats for being loved by everyone. She drove magical piles in his garden and enclosed the cats with magical fences running between the piles. She said “Your cats are shut away in the fences until they become ugly old cats.” like a curse and went away.

Nicholas tried to break the fences with a hummer, but the fences are impregnable against his effort. He went to a church and asked a priest help. The priest looked for how to destroy the magical fences in books and found they could be destroyed by holy water. The Required amount of the holy water to destroy a fence was proportional to the length of the fence. The holy water was, however, fairly expensive. So he decided to buy exactly the minimum amount of the holy water required to save all his cats. How much holy water would be required?

Input

The input has the following format:

N M
x1 y1
.
.
.
xN yN
p1 q1
.
.
.
pM qM

The first line of the input contains two integers N (2 ≤ N ≤ 10000) and M (1 ≤ M). N indicates the number of magical piles and M indicates the number of magical fences. The following N lines describe the coordinates of the piles. Each line contains two integers xi and yi (-10000 ≤ xi, yi ≤ 10000). The following M lines describe the both ends of the fences. Each line contains two integers pj and qj (1 ≤ pj, qjN). It indicates a fence runs between the pj-th pile and the qj-th pile.

You can assume the following:

  • No Piles have the same coordinates.
  • A pile doesn’t lie on the middle of fence.
  • No Fences cross each other.
  • There is at least one cat in each enclosed area.
  • It is impossible to destroy a fence partially.
  • A unit of holy water is required to destroy a unit length of magical fence.

Output

Output a line containing the minimum amount of the holy water required to save all his cats. Your program may output an arbitrary number of digits after the decimal point. However, the absolute error should be 0.001 or less.

题意就是给你一个无向图,把图里所有的环都拆掉,求拆掉边的最小权值和。
刚开始想复杂了,试图各种搜索过,后来发现这题有说边不会交叉,也就是说边不会超过点的个数的5倍(不会证明,反正画出来最多4倍的样子),然后就并查集暴力了。
把边从大到小排序,依次把边加入集合,如果边两个端点在一个集合里就说明有环,且当前边为环的最小边,要拆掉,把全部环的最小边加起来就完事,下面贴代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<stack>
#include<map>
#include<queue>
#include<vector>
using namespace std;
#define ll long long
#define INF 0x3fffffff
#define maxn 10005
#define mod 998244353
struct ac{
    int x,y;
}a[maxn];
struct ac2{
    int x,y;
    double len;
}b[maxn*10];
bool cmp(ac2 aa,ac2 bb){return aa.len>bb.len;}
int n,m,t,x,y;
int fa[maxn];
int fi(int x){
    if(fa[x]==x)
        return x;
    return fa[x]=fi(fa[x]);
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        fa[i]=i;
    for(int i=1;i<=n;i++)
        scanf("%d%d",&a[i].x,&a[i].y);
    for(int i=1;i<=m;i++){
        scanf("%d%d",&x,&y);
        double len=sqrt((a[x].x-a[y].x)*(a[x].x-a[y].x)+(a[x].y-a[y].y)*(a[x].y-a[y].y));
        b[i].x=x;
        b[i].y=y;
        b[i].len=len;
    }
    sort(b+1,b+1+m,cmp);
    double tot=0;
    for(int i=1;i<=m;i++){
        int fx=fi(b[i].x),fy=fi(b[i].y);
        if(fx!=fy)
            fa[fy]=fx;
        else
            tot+=b[i].len;
    }
    printf("%.4f\n",tot);
}