Submission #1095879

# Submission time Handle Problem Language Result Execution time Memory
1095879 2024-10-03T11:10:19 Z TAhmed33 timeismoney (balkan11_timeismoney) C++
100 / 100
566 ms 1104 KB
#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] ? x[2] < y[2] : 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

timeismoney.cpp: In function 'void solve()':
timeismoney.cpp:81:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   81 |  for (auto [x, y] : ee) {
      |            ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 4 ms 860 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 6 ms 476 KB Output is correct
15 Correct 3 ms 348 KB Output is correct
16 Correct 59 ms 344 KB Output is correct
17 Correct 63 ms 540 KB Output is correct
18 Correct 59 ms 544 KB Output is correct
19 Correct 551 ms 860 KB Output is correct
20 Correct 566 ms 1104 KB Output is correct