首页 技术 正文
技术 2022年11月10日
0 收藏 311 点赞 2,769 浏览 1395 个字

裸最小生成树。用kauskal做方便一些。

不得不说这么大数据用cin cout 真是作死。。活该T那么多次。。。

/*************************************************
Problem: 1751User: G_lory
Memory: 4640KTime: 594MS
Language: C++Result: Accepted
*************************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>using namespace std;const int MAXN = 755;
const int MAXM = MAXN * MAXN;int par[MAXN];
int rk[MAXN];struct Edge
{
int u, v;
double w;
bool operator<(const Edge a) const
{
return w < a.w;
}
} edge[MAXM];int tol;void addedge(int u, int v, double w)
{
edge[tol].u = u;
edge[tol].v = v;
edge[tol++].w = w;
}void init(int n)
{
for (int i = 0; i <= n; ++i)
{
rk[i] = 0;
par[i] = i;
}
}int find(int x)
{
if (par[x] == x) return x;
return par[x] = find(par[x]);
}void unite(int x, int y)
{
x = find(x);
y = find(y);
if (x == y) return ;
if (rk[x] < rk[y]) par[x] = y;
else par[y] = x;
if (rk[x] == rk[y]) rk[x]++;
}void Kruskal(int n, int m)
{
sort(edge, edge + tol);
int cnt = 0;
for (int i = 0; i < tol; i++)
{
int t1 = find(edge[i].u);
int t2 = find(edge[i].v);
if (t1 != t2)
{
cout << edge[i].u << " " << edge[i].v << endl;
unite(t1, t2);
cnt++;
}
if (cnt == n - 1 - m) break;
}
}struct Point {
double x, y;
double dis(Point a)
{
return sqrt( (x - a.x) * (x - a.x) + (y - a.y) * (y - a.y) );
}
} p[MAXN];int main()
{
int n, m;
scanf("%d", &n);
tol = 0;
for (int i = 1; i <= n; ++i)
scanf("%lf%lf", &p[i].x, &p[i].y);
for (int i = 1; i <= n; ++i)
for (int j = i + 1; j <= n; ++j)
addedge(i, j, p[i].dis(p[j]));
init(n);
scanf("%d", &m);
int a, b;
int p = 0;
for (int i = 0; i < m; ++i)
{
scanf("%d%d", &a, &b);
if (find(a) != find(b))
{
unite(a, b);
++p;
}
}
Kruskal(n, p);
return 0;
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902