답안 #904188

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
904188 2024-01-11T21:10:36 Z vjudge1 Odašiljači (COCI20_odasiljaci) C++17
70 / 70
200 ms 35008 KB
#include <bits/stdc++.h>

using namespace std;
typedef long double ld;
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(){
    int n;
    cin >> n;
    pair<long double, long double> ubicaciones[n];
    long double x,y;
        for (int i = 0; i<n; i++){
            cin >> x >> y;
            ubicaciones[i].first = x;
            ubicaciones[i].second = y;
        }

    vector<pair< long double,pair<int,int>>> edges;
    for (int i = 0; i<n; i++){
        for(int j = 0; j<n; j++){
                if(i == j) continue;
            ld dx = ubicaciones[i].first - ubicaciones[j].first;
            ld dy = ubicaciones[i].second - ubicaciones[j].second;
            long double distancia = sqrt(dx * dx + dy * dy);
             edges.push_back({distancia,{i,j}});
        }
    }

    sort(edges.begin(), edges.end());

    Dsu dsu(n);

    // Kruskal
    long double costo_total = 0;
    for(int i = 0; i < edges.size(); i++){
        ld 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;
        }
    }
    costo_total /= 2.0L;
    cout << fixed<< setprecision(7) << costo_total << endl;


    return 0;
}

Compilation message

odasiljaci.cpp: In function 'int main()':
odasiljaci.cpp:81:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long double, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |     for(int i = 0; i < edges.size(); i++){
      |                    ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 728 KB Output is correct
5 Correct 2 ms 984 KB Output is correct
6 Correct 50 ms 10184 KB Output is correct
7 Correct 48 ms 10184 KB Output is correct
8 Correct 116 ms 34240 KB Output is correct
9 Correct 194 ms 34992 KB Output is correct
10 Correct 200 ms 35008 KB Output is correct