# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1095876 | TAhmed33 | timeismoney (balkan11_timeismoney) | C++98 | 509 ms | 912 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
#define int long long
int n, m;
array <int, 4> e[10002];
pair <int, int> mn;
vector <pair <int, int>> ee;
struct DSU {
int sze[201], par[201];
void init () {
for (int i = 1; i <= n; i++) {
sze[i] = 1; par[i] = i;
}
}
int find (int x) {
return par[x] == x ? x : par[x] = find(par[x]);
}
bool merge (int x, int y) {
x = find(x); y = find(y);
if (x == y) return 0;
if (sze[x] > sze[y]) {
swap(x, y);
}
sze[y] += sze[x]; par[x] = y;
return 1;
}
} cur;
pair <int, int> get (int a, int b) {
sort(e + 1, e + m + 1, [&] (auto x, auto y) {
return a * x[2] + b * x[3] < a * y[2] + b * y[3];
});
cur.init();
int sum1 = 0, sum2 = 0;
vector <pair <int, int>> ii;
for (int i = 1; i <= m; i++) {
if (cur.merge(e[i][0], e[i][1])) {
sum1 += e[i][2];
sum2 += e[i][3];
ii.push_back({e[i][0], e[i][1]});
}
}
if (sum1 * sum2 < mn.first * mn.second) {
mn = {sum1, sum2};
ee = ii;
}
return {sum1, sum2};
}
void recurse (pair <int, int> a, pair <int, int> b) {
//cout << a.first << " " << a.second << " || " << b.first << " " << b.second << '\n';
int x = a.second - b.second, y = a.first - b.first;
int g = __gcd(x, y);
x /= g; y /= g;
if (y < 0) {
y *= -1; x *= -1;
}
//cout << x << " " << y << '\n';
//cout << "OK\n";
if (x == 0 || y == 0) {
return;
}
x *= -1;
auto h = get(x, y);
if (h == a || h == b || (h.second - a.second) * (h.first - b.first) == (h.second - b.second) * (h.first - a.first)) {
return;
}
recurse(a, h); recurse(h, b);
}
void solve () {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
for (int j = 0; j < 4; j++) {
cin >> e[i][j];
}
e[i][0]++; e[i][1]++;
}
mn = {(int)1e9, (int)1e9};
auto x = get(0, 1), y = get(1, 0);
recurse(x, y);
cout << mn.first << " " << mn.second << '\n';
for (auto [x, y] : ee) {
cout << x - 1 << " " << y - 1 << '\n';
}
}
signed main () {
ios::sync_with_stdio(0); cin.tie(0);
int tc = 1; //cin >> tc;
while (tc--) solve();
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |