QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#119202#6560. Broken Minimum Spanning TreetovarischWA 1ms3412kbC++172.5kb2023-07-05 04:12:092023-07-05 04:12:10

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-05 04:12:10]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3412kb
  • [2023-07-05 04:12:09]
  • 提交

answer

// Too many mind, no mind.
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
#include <iomanip>

using namespace std;

#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define COMP(x) sort(ALL(x)); x.resize(unique(ALL(x)) - (x).begin())
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)

using pii = pair <int, int>;
using vi = vector <int>;
using vpi = vector <pii>;
using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using ld = long double;
using vld = vector<ld>;

using pp = pair<pii, pii>;
struct DSU {
	int n;
	vi s, f;
	DSU (int n): n(n) {
		s.assign(n+5, 1);
		f.resize(n+5); iota(ALL(f), 0);
	}
	int find(int a) { return f[a] = f[a] == a ? a : find(f[a]); }
	void merge(int a, int b) {
		a = find(a), b = find(b);
		if (a == b) return;
		if (s[a] < s[b]) swap(a, b);
		s[a] += s[b];
		f[b] = a;
	}
};
int main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n, m; cin >> n >> m;
	vector <pp> edges, tree;
	forn(i, n-1) {
		int u, v, w; cin >> u >> v >> w;
		u--, v--;
		tree.pb(mp(mp(w, i), mp(u, v)));
	}
	fore(i, n-1, m-1) {
		int u, v, w; cin >> u >> v >> w;
		u--, v--;
		edges.pb(mp(mp(w, i), mp(u, v)));
	}

	sort(ALL(edges));
	for(auto& p: edges) {
		tree.pb(p);
		sort(ALL(tree));
		DSU dsu(n);

		int out = -1;
		for(auto& it : tree) {
			int i = it.fi.se;
			int u = it.se.fi, v = it.se.se;

			if (dsu.find(u) == dsu.find(v)) out = i;
			else dsu.merge(u, v);
		}

		vector <pp> aux;
		for(auto& it : tree) {
			int i = it.fi.se;
			if (i != out) aux.pb(it);
		}
		tree = aux;
	}

	vector <bool> in(m);
	for(auto& it : tree) {
		int i = it.fi.se;
		in[i] = 1;
	}
	vpi ans;
	for(int i=0, j=n-1; i < n-1; i++) {
		if (in[i]) continue;
		while (j < m && !in[j]) j++;
		ans.pb(mp(i+1, j+1));
	}
	cout << ans.size() << endl;
	for(auto& it : ans) cout << it.fi << ' ' << it.se << endl;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3412kb

input:

4 4
1 2 10
2 3 3
3 4 1
1 4 4

output:

1
1 4

result:

ok correct!

Test #2:

score: -100
Wrong Answer
time: 1ms
memory: 3412kb

input:

6 8
1 2 10
2 3 10
3 4 10
4 5 10
5 6 10
6 1 10
1 3 1
4 6 1

output:

2
2 7
5 7

result:

wrong answer bad swap 5 7