Submission #904147

# Submission time Handle Problem Language Result Execution time Memory
904147 2024-01-11T20:36:05 Z vjudge1 Odašiljači (COCI20_odasiljaci) C++17
70 / 70
992 ms 87400 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

odasiljaci.cpp: In function 'int main()':
odasiljaci.cpp:67:33: warning: narrowing conversion of 'i' from 'int' to 'long double' [-Wnarrowing]
   67 |             edges.push_back({w, i, j});
      |                                 ^
odasiljaci.cpp:67:33: warning: narrowing conversion of 'i' from 'int' to 'long double' [-Wnarrowing]
odasiljaci.cpp:67:36: warning: narrowing conversion of 'j' from 'int' to 'long double' [-Wnarrowing]
   67 |             edges.push_back({w, i, j});
      |                                    ^
odasiljaci.cpp:67:36: warning: narrowing conversion of 'j' from 'int' to 'long double' [-Wnarrowing]
odasiljaci.cpp:86:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long double> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |     for(int i = 0; i < edges.size(); i++){
      |                    ~~^~~~~~~~~~~~~~
odasiljaci.cpp:93:37: warning: narrowing conversion of 'a' from 'int' to 'long double' [-Wnarrowing]
   93 |             edges_mst.push_back({w, a, b});
      |                                     ^
odasiljaci.cpp:93:37: warning: narrowing conversion of 'a' from 'int' to 'long double' [-Wnarrowing]
odasiljaci.cpp:93:40: warning: narrowing conversion of 'b' from 'int' to 'long double' [-Wnarrowing]
   93 |             edges_mst.push_back({w, a, b});
      |                                        ^
odasiljaci.cpp:93:40: warning: narrowing conversion of 'b' from 'int' to 'long double' [-Wnarrowing]
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 3 ms 856 KB Output is correct
5 Correct 4 ms 1368 KB Output is correct
6 Correct 204 ms 22024 KB Output is correct
7 Correct 192 ms 22004 KB Output is correct
8 Correct 566 ms 58968 KB Output is correct
9 Correct 992 ms 86396 KB Output is correct
10 Correct 937 ms 87400 KB Output is correct