QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#359562 | #6560. Broken Minimum Spanning Tree | LaStataleBlue# | RE | 0ms | 0kb | C++23 | 1.9kb | 2024-03-20 19:03:33 | 2024-03-20 19:03:33 |
answer
#pragma ide diagnostic ignored "misc-no-recursion"
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef long double db;
#define TESTCASE 0
static constexpr int MOD = 998'244'353;
static constexpr int INF = 1.1e9;
static constexpr int MAX_N = 4000;
static constexpr db EPS = 1e-9;
static vector<tuple<int, int, int>> graph[MAX_N];
static bool taken[MAX_N];
static pair<int, int> dfs(int u, int p, int z) {
for (auto [v, w, i]: graph[u]) {
if (i == p || !taken[i]) continue;
if (v == z) return {i, w};
auto [ri, rw] = dfs(v, u, z);
if (ri != -1) return rw > w ? pair(ri, rw) : pair(i, w);
}
return {-1, -1};
}
static void solve([[maybe_unused]] int tc) {
ll N, M;
cin >> N >> M;
vector<tuple<int, int, int, int>> edges(M);
int c = 0;
for (auto &[u, v, w, i]: edges) {
i = c++;
cin >> u >> v >> w;
graph[u].emplace_back(v, w, i);
graph[v].emplace_back(u, w, i);
taken[i] = i < N - 1;
}
sort(edges.begin(), edges.end(), [&](const auto &a, const auto &b) {
return get<2>(a) < get<2>(b);
});
vector<pair<int, int>> moves;
for (auto [u, v, w, i]: edges) {
if (taken[i]) continue;
auto [ri, rw] = dfs(u, i, v);
if (ri != -1 && rw > w) {
moves.emplace_back(ri, i);
taken[ri] = false;
taken[i] = true;
}
}
cout << moves.size() << '\n';
for (auto [a, b]: moves) {
cout << (a + 1) << ' ' << (b + 1) << '\n';
}
}
int main() {
ios::sync_with_stdio(false);
if (const char *f = getenv("REDIRECT_STDOUT"); f) {
freopen(f, "w", stdout);
}
int T = 1;
#if TESTCASE
cin >> T;
#endif
for (int t = 1; t <= T; t++) {
solve(t);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
4 4 1 2 10 2 3 3 3 4 1 1 4 4