Submission #715155

# Submission time Handle Problem Language Result Execution time Memory
715155 2023-03-26T06:12:06 Z ismayil Sirni (COCI17_sirni) C++17
42 / 140
339 ms 786432 KB
#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct edge{
    int u, v, w;
    bool operator<(edge other){
        return w < other.w;
    }
};
const int MAX = 1e3 + 1;
int _parent[MAX], _size[MAX];
void make(int i){
    _parent[i] = i;
    _size[i] = 1;
}
int find(int i){
    if(_parent[i] == i) return i;
    return _parent[i] = find(_parent[i]);
}
bool  merge(int u, int v){
    u = find(u), v = find(v);
    if(u == v) return false;
    if(_size[u] < _size[v]) swap(u, v);
    _parent[v] = u;
    _size[u] += _size[v];
    return true;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n;
    cin>>n;
    vector<int> P(n + 1);
    int g[n + 1][n + 1];
    for(int i = 1; i <= n; i++) cin>>P[i], make(i);
    for(int i = 1; i <= n; i++){
        for(int j = i + 1; j <= n; j++){
            g[i][j] = min(P[i] % P[j], P[j] % P[i]);
        }
    }
    vector<edge> edges;
    for(int i = 1; i <= n; i++){
        for(int j = i + 1; j <= n; j++){
            edges.push_back({i, j, g[i][j]});
        }
    }
    sort(edges.begin(), edges.end());
    int ans = 0;
    for(auto e : edges){
        if(merge(e.u, e.v)){
            ans += e.w;
            n--;
        }
    }
    cout<<ans<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 55 ms 9924 KB Output is correct
2 Correct 51 ms 10308 KB Output is correct
3 Correct 68 ms 10416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 10308 KB Output is correct
2 Correct 42 ms 10464 KB Output is correct
3 Correct 59 ms 10564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 10512 KB Output is correct
2 Correct 47 ms 9780 KB Output is correct
3 Correct 59 ms 10516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 339 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 287 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 288 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 308 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 289 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 310 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 298 ms 786432 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -