This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// https://oj.uz/problem/view/COCI17_sirni
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<int>
#define vb vector<bool>
#define pii pair<int, int>
#define vpii vector<pii>
#define vvpii vector<vpii>
struct DSU {
vi e;
void init(int n) { e.assign(n, -1); }
int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
bool connected(int a, int b) { return get(a) == get(b); }
bool unite(int a, int b) {
a = get(a), b = get(b); if (a == b) return 0;
if (e[a] > e[b]) swap(a, b);
e[a] += e[b]; e[b] = a;
return 1;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
vi cards(n);
for (int i = 0; i < n; i++) {
cin >> cards[i];
}
sort(cards.begin(), cards.end());
int mincard = cards[0];
vvpii edges(mincard);
int dist;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
dist = cards[j] % cards[i];
if (dist < mincard) edges[dist].push_back(make_pair(i, j));
}
}
DSU uf;
uf.init(n);
ll total = 0;
int a, b;
for (ll i = 0; i < mincard; i++) {
for (pii edge : edges[i]) {
tie(a, b) = edge;
if (uf.unite(a, b)) total += i;
}
}
cout << total << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |