# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
904181 | 2024-01-11T21:06:35 Z | vjudge1 | Odašiljači (COCI20_odasiljaci) | C++17 | 900 ms | 87488 KB |
#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
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 344 KB | Output is correct |
2 | Correct | 1 ms | 348 KB | Output is correct |
3 | Correct | 2 ms | 604 KB | Output is correct |
4 | Correct | 2 ms | 856 KB | Output is correct |
5 | Correct | 5 ms | 1372 KB | Output is correct |
6 | Correct | 193 ms | 21972 KB | Output is correct |
7 | Correct | 204 ms | 21996 KB | Output is correct |
8 | Correct | 520 ms | 59624 KB | Output is correct |
9 | Correct | 900 ms | 86752 KB | Output is correct |
10 | Correct | 897 ms | 87488 KB | Output is correct |