Advertisement
skimono

Untitled

Jun 21st, 2022
1,008
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef unsigned long long ull;
  7. typedef long double ld;
  8. typedef string str;
  9. //typedef __int128 ultraint;
  10. #define endl "\n"
  11. #define sqrt sqrtl
  12. //#define pow fast_pow
  13.  
  14. const ll inf = (ll)1e18 + 7;
  15. ld eps = 1e-6;
  16. ld Pi = 3.14159265358979323846;
  17.  
  18. struct edge {
  19.     ll u, v, c;
  20. };
  21.  
  22. vector <edge> ans;
  23. vector <vector <ll> > a;
  24.  
  25. void rec(vector <ll> &v) {
  26.     if (v.size() < 2) {
  27.         return;
  28.     }
  29.     sort(v.begin(), v.end());
  30.     vector <ll> v2;
  31.     vector <ll> v1;
  32.     ll x = v.front(), i, j, y, mx = inf;
  33.     for (j = 0; j < a.size(); j++) {
  34.         if (j != x && a[x][j] < mx && binary_search(v.begin(), v.end(), j)) {
  35.             y = j;
  36.             mx = a[x][j];
  37.         }
  38.     }
  39.     v1.push_back(x);
  40.     v2.push_back(y);
  41.     ans.push_back({ x,y,mx });
  42.     for (i = 0; i < v.size(); i++) {
  43.         if (v[i] != x && v[i] != y) {
  44.             if (a[v[i]][x] < a[v[i]][y]) {
  45.                 v1.push_back(v[i]);
  46.             }
  47.             else {
  48.                 v2.push_back(v[i]);
  49.             }
  50.         }
  51.     }
  52.     rec(v1);
  53.     rec(v2);
  54. }
  55.  
  56. signed main() {
  57.     ios_base::sync_with_stdio(0);
  58.     cin.tie(NULL);
  59.     cout.tie(NULL);
  60.     ll t = 1;
  61.     //cin >> t;
  62.     while (t--) {
  63.         ll n, i, j;
  64.         cin >> n;
  65.         vector <ll> path(n);
  66.         a.resize(n, vector <ll>(n));
  67.         for (i = 0; i < n; i++) {
  68.             path[i] = i;
  69.             for (j = 0; j < n; j++) {
  70.                 cin >> a[i][j];
  71.             }
  72.         }
  73.         rec(path);
  74.         cout << n << endl;
  75.         for (i = 0; i < n - 1; i++) {
  76.             cout << ans[i].u + 1 << " " << ans[i].v + 1<< " " << ans[i].c << endl;
  77.         }
  78.     }
  79. }
  80. //痛みを受け入れる。 痛みを知っています。 痛みを感じる。 痛みを参照してください。 真の痛みを知らなければ、真の世界を理解することは不可能です。 今、あなたは痛みを知るでしょう。 神の罰!
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement