Submission #904060

# Submission time Handle Problem Language Result Execution time Memory
904060 2024-01-11T19:03:59 Z vjudge1 Odašiljači (COCI20_odasiljaci) C++17
70 / 70
191 ms 34768 KB
#include<bits/stdc++.h>
using namespace std;
#define double long double
#define int long long
struct coordenada{
    int x, y;
};
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)];
    }
};

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    cout<<fixed<<setprecision(7);
    int m;
    cin >> m;

    vector<pair<double,pair<int,int>>> edges, edges_mst;
    vector<coordenada>puntos(m);
    for(int i = 0; i < m; i++){
        coordenada c;
        cin>>c.x>>c.y;
        puntos[i]=c;
    }
    for(int i=0;i<m;i++){
        for(int j=0;j<m;j++){
            if(i==j)continue;
            double cost=(double)sqrt((abs(puntos[i].x-puntos[j].x)*abs(puntos[i].x-puntos[j].x))+(abs(puntos[i].y-puntos[j].y)*abs(puntos[i].y-puntos[j].y)));
            edges.push_back({cost,{i,j}});
        }
    }
    sort(edges.begin(), edges.end());

    Dsu dsu(m);

    // Kruskal
    double costo_total = 0;
    for(int i = 0; i < edges.size(); i++){
        double w=edges[i].first;
        int a = edges[i].second.first;
        int b = edges[i].second.second;
        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}});
        }
    }

    // Finalmente se obtiene el costo minimo para conectar todos los nodos
    cout << (costo_total/=2) << "\n";

    return 0;
}

Compilation message

odasiljaci.cpp: In function 'int main()':
odasiljaci.cpp:77:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long double, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |     for(int i = 0; i < edges.size(); i++){
      |                    ~~^~~~~~~~~~~~~~
# 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 1 ms 604 KB Output is correct
4 Correct 1 ms 736 KB Output is correct
5 Correct 2 ms 992 KB Output is correct
6 Correct 50 ms 10440 KB Output is correct
7 Correct 46 ms 10712 KB Output is correct
8 Correct 109 ms 33744 KB Output is correct
9 Correct 191 ms 34768 KB Output is correct
10 Correct 188 ms 33232 KB Output is correct