Language: C++

#include <cassert>
#include <cstddef>
#include <utility>
#include <vector>

class bipartite_union_find
{
    class node
    {
        node *link = nullptr;
        bool mark = 0;
        size_t cnt[2] = {1, 0};
    public:
        size_t size() const { return cnt[0] + cnt[1]; }
        size_t size(const bool color) const { return cnt[color]; }
        node *find()
        {
            if(!link) return this;
            node *const root = link->find();
            mark ^= link->mark;
            return link = root;
        }
        bool color() { return find(), mark; }
        void merge(node *other, const bool flip)
        {
            other->link = this, other->mark ^= flip;
            for(bool i : {0, 1}) cnt[i] += other->cnt[i ^ flip];
        }
    };

    std::vector<node> tree;

    bool unite(node *x, node *y, bool diff_color)
    {
        diff_color ^= x->color() ^ y->color();
        x = x->find(), y = y->find();
        if(x == y) return !diff_color;
        if(x->size() < y->size()) std::swap(x, y);
        x->merge(y, diff_color);
        return true;
    }

public:
    explicit bipartite_union_find(const size_t _n) : tree(_n) {}

    size_t find(const size_t x)
    {
        assert(x < size());
        return tree[x].find() - &*tree.begin();
    }

    size_t size() const { return tree.size(); }

    size_t size(const size_t x)
    {
        assert(x < size());
        return tree[x].find()->size();
    }

    size_t size(const size_t x, const bool color)
    {
        assert(x < size());
        return tree[x].find()->size(color);
    }

    bool color(const size_t x)
    {
        assert(x < size());
        return tree[x].color();
    }

    bool same(const size_t x, const size_t y)
    {
        assert(x < size() && y < size());
        return tree[x].find() == tree[y].find();
    }

    // return if the condition given is possible.
    bool unite(const size_t x, const size_t y, const bool diff_color)
    {
        assert(x < size() && y < size());
        return unite(&tree[x], &tree[y], diff_color);
    }
}; // class bipartite_union_find

using namespace std;

int main(int argc, char *argv[])
{
    std::ios::sync_with_stdio(false), std::cin.tie(nullptr);

    int n;
    cin >> n;
    vector<string> g(n);
    for(auto &s:g) cin>>s;

    int dist[222][222];
    for(int i=0; i<n; ++i)
        for(int j=0; j<n; ++j)
            dist[i][j]=n;

    bipartite_union_find uf(n);
    for(int i = 0; i < n; ++i)
    {
        for(int j = 0; j < n; ++j)
        {
            if(g[i][j] - '0')
            {
                if(!uf.unite(i,j))
                {
                    cout << -1 << "\n";
                    return 0;
                }
                dist[i][j] = 1;
            }
            else
            {
                if(i==j)
                {
                    dist[i][j]=0;
                }
            }
        }
    }
    for(int k = 0; k < n; ++k)
    {
        for(int i = 0; i < n; ++i)
        {
            for(int j = 0; j < n; ++j)
            {
                dist[i][j]=min(dist[i][j], dist[i][k] + dist[k][j]);
            }
        }
    }

    int ans=0;
    for(int i = 0; i < n; ++i)
    {
        vector<int> comp;
        for(int j = 0; j < n; ++j)
        {
            if(uf.find(j)==i)
            {
                comp.emplace_back(j);
            }
        }
        int k=0;
        for(int a : comp)
        {
            for(int b : comp)
            {
                if(k<=dist[a][b])
                {
                    k=dist[a][b]+1;
                }
            }
        }
        ans+=k;
    }
    cout << ans << "\n";
}


Compiler output:
a.cc: In function 'int main(int, char**)':
a.cc:90:10: error: 'std::ios' has not been declared
   90 |     std::ios::sync_with_stdio(false), std::cin.tie(nullptr);
      |          ^~~
a.cc:90:44: error: 'cin' is not a member of 'std'
   90 |     std::ios::sync_with_stdio(false), std::cin.tie(nullptr);
      |                                            ^~~
a.cc:5:1: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
    4 | #include <vector>
  +++ |+#include <iostream>
    5 | 
a.cc:93:5: error: 'cin' was not declared in this scope
   93 |     cin >> n;
      |     ^~~
a.cc:93:5: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:94:12: error: 'string' was not declared in this scope
   94 |     vector<string> g(n);
      |            ^~~~~~
a.cc:5:1: note: 'std::string' is defined in header '<string>'; this is probably fixable by adding '#include <string>'
    4 | #include <vector>
  +++ |+#include <string>
    5 | 
a.cc:94:18: error: template argument 1 is invalid
   94 |     vector<string> g(n);
      |                  ^
a.cc:94:18: error: template argument 2 is invalid
a.cc:95:17: error: 'begin' was not declared in this scope; did you mean 'std::begin'?
   95 |     for(auto &s:g) cin>>s;
      |                 ^
      |                 std::begin
In file included from /usr/include/c++/14/vector:69,
                 from a.cc:4:
/usr/include/c++/14/bits/range_access.h:114:37: note: 'std::begin' declared here
  114 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
      |                                     ^~~~~
a.cc:95:17: error: 'end' was not declared in this scope; did you mean 'std::end'?
   95 |     for(auto &s:g) cin>>s;
      |                 ^
      |                 std::end
/usr/include/c++/14/bits/range_access.h:116:37: note: 'std::end' declared here
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
a.cc:107:17: error: invalid types 'int[int]' for array subscript
  107 |             if(g[i][j] - '0')
      |                 ^
a.cc:109:29: error: no matching function for call to 'bipartite_union_find::unite(int&, int&)'
  109 |                 if(!uf.unite(i,j))
      |                     ~~~~~~~~^~~~~
a.cc:33:10: note: candidate: 'bool bipartite_union_find::unite(node*, node*, bool)'
   33 |     bool unite(node *x, node *y, bool diff_color)
      |          ^~~~~
a.cc:33:10: note:   candidate expects 3 arguments, 2 provided
a.cc:79:10: note: candidate: 'bool bipartite_union_find::unite(size_t, size_t, bool)'
   79 |     bool unite(const size_t x, const size_t y, const bool diff_color)
      |          ^~~~~
a.cc:79:10: note:   candidate expects 3 arguments, 2 provided
a.cc:111:21: error: 'cout' was not declared in this scope
  111 |                     cout << -1 << "\n";
      |                     ^~~~
a.cc:111:21: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:160:5: error: 'cout' was not declared in this scope
  160 |     cout << ans << "\n";
      |     ^~~~
a.cc:160:5: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'