Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #define debug(tl) cerr<<#tl<<' '<<tl<<'\n';
- #include "bits/stdc++.h"
- using namespace std;
- #define all(d) d.begin(), d.end()
- typedef long long ll;
- typedef pair<ll, ll> pll;
- typedef pair<int, int> pii;
- typedef long double ld;
- const int N = 2000;
- int p[N];
- void init(int n) {
- fill(p, p + n, -1);
- }
- int find(int x) {
- return p[x] < 0 ? x : p[x] = find(p[x]);
- }
- bool join(int a, int b) {
- a = find(a), b = find(b);
- if (a == b)
- return false;
- if (p[a] > p[b])
- swap(a, b);
- p[a] += p[b];
- p[b] = a;
- return true;
- }
- signed main() {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int n;
- cin >> n;
- struct edge {
- int u, v, w;
- bool operator<(const edge &other) {
- return w < other.w;
- }
- };
- vector<edge> e;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- int x;cin >> x;
- if (x)e.push_back({ i,j, x });
- }
- }
- sort(all(e));
- init(n);
- vector<edge> ans;
- for (int i = 0; i < (int)e.size(); i++) {
- if (join(e[i].u, e[i].v)) {
- ans.emplace_back(e[i]);
- if ((int)ans.size() == n - 1)break;
- }
- }
- cout << n << '\n';
- for (const edge& i : ans) {
- cout << i.u + 1 << ' ' << i.v + 1 << ' ' << i.w << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement