# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
904144 | 2024-01-11T20:34:35 Z | vjudge1 | Odašiljači (COCI20_odasiljaci) | C++17 | 1000 ms | 87540 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 | 0 ms | 348 KB | Output is correct |
3 | Correct | 1 ms | 604 KB | Output is correct |
4 | Correct | 3 ms | 860 KB | Output is correct |
5 | Correct | 4 ms | 1368 KB | Output is correct |
6 | Correct | 211 ms | 21980 KB | Output is correct |
7 | Correct | 217 ms | 21892 KB | Output is correct |
8 | Correct | 555 ms | 59888 KB | Output is correct |
9 | Execution timed out | 1002 ms | 87540 KB | Time limit exceeded |
10 | Correct | 933 ms | 87048 KB | Output is correct |