POJ

来源:互联网 发布:矩阵的雅可比矩阵 编辑:程序博客网 时间:2024/06/05 05:46

The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a number of highways connecting some of the most important towns. However, there are still some towns that you can’t reach via a highway. It is necessary to build more highways so that it will be possible to drive between any pair of towns without leaving the highway system.
玄学,C++没过TLE了,G++过了。
用了kruskal。
把已连接的点先放到树里就可以了。

#include <stdio.h>#include <string.h>#include <iostream>#include<algorithm>#include <vector>#include <queue>#include <string>#include <math.h>#include <stdlib.h>using namespace std;#define INF 0x3f3f3f3f#define mem(arr,a) memset(arr,a,sizeof(arr))#define V 800#define LL long long int#define E 320000#define pow(a) ((a)*(a))struct edge{    LL u, v;    LL w;};struct node{    int x, y;};node vet[V];edge es[E];bool cmp(edge a, edge b){    if (a.w!=b.w)    return a.w < b.w;    if (a.u != b.u)        return a.u < b.u;    return a.v < b.v;}int n, m;int par[V];int e = 0;int find(int x){    if (par[x] == x)return x;    return par[x] = find(par[x]);}int main(){    cin >> n;    for (int i = 1; i <= n; i++)par[i] = i;    for (int i = 1; i <= n; i++){        scanf("%lld%lld", &vet[i].x, &vet[i].y);    }    for (int i = 1; i <= n; i++){        for (int j = 1; j < i; j++){            es[e].u = i, es[e].v = j, es[e++].w = pow(vet[i].x - vet[j].x) + pow(vet[i].y - vet[j].y);        }    }    cin >> m;    for (int i = 0; i < m; i++){        int a, b; scanf("%d%d", &a, &b);        a = find(a); b = find(b);        par[a] = b;    }    sort(es, es + e, cmp);    for (int i = 0; i < e; i++){        int u = find(es[i].u);        int v = find(es[i].v);        if (u!=v){            par[u] = v;            printf("%lld %lld\n", es[i].u, es[i].v);        }    }}
原创粉丝点击