Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef long double ld;
- typedef string str;
- //typedef __int128 ultraint;
- #define endl "\n"
- #define sqrt sqrtl
- //#define pow fast_pow
- const ll inf = (ll)1e18 + 7;
- ld eps = 1e-6;
- ld Pi = 3.14159265358979323846;
- struct edge {
- ll u, v, c;
- };
- vector <edge> ans;
- vector <vector <ll> > a;
- void rec(vector <ll> &v) {
- if (v.size() < 2) {
- return;
- }
- sort(v.begin(), v.end());
- vector <ll> v2;
- vector <ll> v1;
- ll x = v.front(), i, j, y, mx = inf;
- for (j = 0; j < a.size(); j++) {
- if (j != x && a[x][j] < mx && binary_search(v.begin(), v.end(), j)) {
- y = j;
- mx = a[x][j];
- }
- }
- v1.push_back(x);
- v2.push_back(y);
- ans.push_back({ x,y,mx });
- for (i = 0; i < v.size(); i++) {
- if (v[i] != x && v[i] != y) {
- if (a[v[i]][x] < a[v[i]][y]) {
- v1.push_back(v[i]);
- }
- else {
- v2.push_back(v[i]);
- }
- }
- }
- rec(v1);
- rec(v2);
- }
- signed main() {
- ios_base::sync_with_stdio(0);
- cin.tie(NULL);
- cout.tie(NULL);
- ll t = 1;
- //cin >> t;
- while (t--) {
- ll n, i, j;
- cin >> n;
- vector <ll> path(n);
- a.resize(n, vector <ll>(n));
- for (i = 0; i < n; i++) {
- path[i] = i;
- for (j = 0; j < n; j++) {
- cin >> a[i][j];
- }
- }
- rec(path);
- cout << n << endl;
- for (i = 0; i < n - 1; i++) {
- cout << ans[i].u + 1 << " " << ans[i].v + 1<< " " << ans[i].c << endl;
- }
- }
- }
- //痛みを受け入れる。 痛みを知っています。 痛みを感じる。 痛みを参照してください。 真の痛みを知らなければ、真の世界を理解することは不可能です。 今、あなたは痛みを知るでしょう。 神の罰!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement