# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
904126 | vjudge1 | Odašiljači (COCI20_odasiljaci) | C++17 | 915 ms | 87524 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
struct Dsu {
vector<int> parent, rank, size;
Dsu(int N) {
parent.resize(N);
rank.resize(N);
size.resize(N, 1);
for (int i = 0; i < N; i++){
parent[i] = i;
}
}
bool isSameSet(int i, int j){
return findSet(i) == findSet(j);
}
int findSet(int i) {
if(parent[i] == i){
return i;
}else{
return parent[i] = findSet(parent[i]);
}
}
void unionSet(int i, int j){
int x = findSet(i);
int y = findSet(j);
if (x != y){
if (rank[x] > rank[y]){
parent[y] = x;
size[x] += size[y];
}else if(rank[x] < rank[y]){
parent[x] = y;
size[y] += size[x];
}else{
parent[x] = y;
size[y] += size[x];
rank[y]++;
}
}
}
int getSize(int i){
return size[findSet(i)];
}
};
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cout << fixed << setprecision(12);
int n;
cin >> n;
vector <pair<int,int>> antena;
for (int i = 0; i < n; i++){
int a, b;
cin >> a >> b;
antena.push_back({a,b});
}
vector<vector<long double>> edges, edges_mst;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
long double d = antena[i].first - antena[j].first;
long double e = antena[i].second - antena[j].second;
long double w = (sqrt((d*d)+(e*e))/2.0);
edges.push_back({w, i, j});
}
}
/*
for(int i = 0; i < m; i++){
int a, b, w;
cin >> a >> b;
a--; b--;
edges.push_back({w, a, b});
}
*/
// Ordeno las aristas por peso
// (es importante que el peso sea el primer elemento de cada arista)
sort(edges.begin(), edges.end());
long double maximo;
Dsu dsu(n);
// Kruskal
long double costo_total = 0;
for(int i = 0; i < edges.size(); i++){
long double w = edges[i][0];
int a = edges[i][1];
int b = edges[i][2];
if(!dsu.isSameSet(a, b)){ // Si a y b no estan en la misma componente conexa
dsu.unionSet(a, b); // entonces las uno
costo_total += w;
edges_mst.push_back({w, a, b});
maximo = w;
}
}
// Finalmente se obtiene el costo minimo para conectar todos los nodos
cout << maximo << "\n";
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |