Advertisement
Dimaush

Untitled

Dec 25th, 2022
1,011
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. enum EEdgeCost
  5. {
  6.     EC_VERTICAL = 1,
  7.     EC_HORIZONTAL = 2
  8. };
  9.  
  10. class TDisjointSetUnion {
  11. public:
  12.     unsigned Components;
  13.     std::vector<unsigned> Parent;
  14.     std::vector<unsigned> Rank;
  15.  
  16. public:
  17.     TDisjointSetUnion(unsigned c) : Components(c), Parent(Components), Rank(Components, 0) {
  18.         for (unsigned i = 0; i < Components; ++i) {
  19.             Parent[i] = i;
  20.         }
  21.     }
  22.  
  23.     unsigned FindSet(unsigned x) {
  24.         if (Parent[x] == x) {
  25.             return x;
  26.         } else {
  27.             Parent[x] = FindSet(Parent[x]);
  28.             return Parent[x];
  29.         }
  30.     }
  31.  
  32.     void UniteSets(unsigned x, unsigned y) {
  33.         x = FindSet(x);
  34.         y = FindSet(y);
  35.         if (x != y) {
  36.             if (Rank[x] < Rank[y]) {
  37.                 Parent[x] = y;
  38.             } else {
  39.                 Parent[y] = x;
  40.                 if (Rank[x] == Rank[y]) {
  41.                     ++Rank[x];
  42.                 }
  43.             }
  44.             --Components;
  45.         }
  46.     }
  47. };
  48.  
  49. void Input(TDisjointSetUnion& t, unsigned h, unsigned w) {
  50.     for (unsigned i = 0; i < h; ++i) {
  51.         for (unsigned j = 0; j < w; ++j) {
  52.             unsigned v;
  53.             std::cin >> v;
  54.             if (v & 1) {
  55.                 t.UniteSets(i * w + j, (i + 1) * w + j);
  56.             }
  57.             if ((v >> 1) & 1) {
  58.                 t.UniteSets(i * w + j, i * w + (j + 1));
  59.             }
  60.         }
  61.     }
  62. }
  63.  
  64. std::vector<std::pair<unsigned, unsigned>> Connect(TDisjointSetUnion& t, unsigned h, unsigned w) {
  65.     std::vector<std::pair<unsigned, unsigned>> added;
  66.     for (unsigned i = 0; i < h - 1; ++i) {
  67.         for (unsigned j = 0; j < w; ++j) {
  68.             if (t.FindSet(i * w + j) != t.FindSet((i + 1) * w + j)) {
  69.                 added.push_back(std::make_pair(i * w + j, EC_VERTICAL));
  70.                 t.UniteSets(i * w + j, (i + 1) * w + j);
  71.             }
  72.         }
  73.     }
  74.     for (unsigned i = 0; i < h; ++i) {
  75.         for (unsigned j = 0; j < w - 1; ++j) {
  76.             if (t.FindSet(i * w + j) != t.FindSet(i * w + (j + 1))) {
  77.                 added.push_back(std::make_pair(i * w + j, EC_HORIZONTAL));
  78.                 t.UniteSets(i * w + j, i * w + (j + 1));
  79.             }
  80.         }
  81.     }
  82.     return added;
  83. }
  84.  
  85. int main() {
  86.     unsigned n, m;
  87.     std::cin >> n >> m;
  88.     TDisjointSetUnion grid(n * m);
  89.     Input(grid, n, m);
  90.  
  91.     std::vector<std::pair<unsigned, unsigned>> added = Connect(grid, n, m);
  92.     unsigned cost = 0;
  93.     for (auto bridge: added) {
  94.         cost += bridge.second;
  95.     }
  96.  
  97.     std::cout << added.size() << " " << cost << std::endl;
  98.     for (auto bridge: added) {
  99.         std::cout << bridge.first / m + 1 << " " << bridge.first % m + 1 << " " << bridge.second << std::endl;
  100.     }
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement