Language: C++

#define _CRT_SECURE_NO_WARNINGS

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <array>
#include <cassert>
#include <bitset>
#include <cstdint>
using namespace std;
using LL = long long;

int N;
int A[1234][1234];

int main() {
	cin >> N;
	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < N - 1; ++j) {
			cin >> A[i][j];
			--A[i][j];
		}
	}
	unordered_map<pair<int, int>, int,boost::hash<pair<int,int>>>M;
	set<pair<int, pair<int, int>>>S;
	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < i; ++j) {
			S.insert({ 0,{j,i} });
			M[{j, i}] = 0;
		}
	}
	auto w = [&](int i, int j) {
		return (i < j) ? pair<int, int>(i, j) : pair<int, int>(j, i);
	};
	vector<int>upd;
	for (int i = 0; i < N; ++i)upd.push_back(i);
	vector<int>mark(N, 0);
	int ans = 0;
	for (;;) {
		bool br = true;
		for (int i : upd) {
			if (mark[i] != N - 1) {
				br = false;
				int j = A[i][mark[i]];
				S.erase(S.find({ M[w(i,j)] ,w(i, j) }));
				M[w(i, j)]--;
				S.insert({ M[w(i,j)], w(i,j) });
			}
		}
		if (br)
			break;
		upd.clear();
		ans++;
		while (!S.empty()) {
			auto big = *S.begin();
			if (big.first > -2) {
				break;
			}
			mark[big.second.first]++;
			if (mark[big.second.first] < N - 1)upd.push_back(big.second.first);
			upd.push_back(big.second.second);
			if (mark[big.second.second] < N - 1)mark[big.second.second]++;
			S.erase(S.begin());
		}
	}
	for (int i = 0; i < N; ++i) {
		if (mark[i] != N - 1) {
			ans = -1;
		}
	}
	cout << ans << endl;
	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:43:43: error: 'boost' was not declared in this scope
   43 |         unordered_map<pair<int, int>, int,boost::hash<pair<int,int>>>M;
      |                                           ^~~~~
a.cc:43:69: error: template argument 3 is invalid
   43 |         unordered_map<pair<int, int>, int,boost::hash<pair<int,int>>>M;
      |                                                                     ^
a.cc:48:26: error: invalid types 'int[<brace-enclosed initializer list>]' for array subscript
   48 |                         M[{j, i}] = 0;
      |                          ^
a.cc:64:51: error: no match for 'operator[]' (operand types are 'int' and 'std::pair<int, int>')
   64 |                                 S.erase(S.find({ M[w(i,j)] ,w(i, j) }));
      |                                                   ^
a.cc:64:47: error: no matching function for call to 'std::set<std::pair<int, std::pair<int, int> > >::find(<brace-enclosed initializer list>)'
   64 |                                 S.erase(S.find({ M[w(i,j)] ,w(i, j) }));
      |                                         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/set:63,
                 from a.cc:11:
/usr/include/c++/14/bits/stl_set.h:806:9: note: candidate: 'template<class _Kt> decltype (std::set<_Key, _Compare, _Alloc>::iterator{((std::set<_Key, _Compare, _Alloc>*)this)->std::set<_Key, _Compare, _Alloc>::_M_t._M_find_tr(__x)}) std::set<_Key, _Compare, _Alloc>::find(const _Kt&) [with _Key = std::pair<int, std::pair<int, int> >; _Compare = std::less<std::pair<int, std::pair<int, int> > >; _Alloc = std::allocator<std::pair<int, std::pair<int, int> > >]'
  806 |         find(const _Kt& __x)
      |         ^~~~
/usr/include/c++/14/bits/stl_set.h:806:9: note:   template argument deduction/substitution failed:
a.cc:64:47: note:   couldn't deduce template parameter '_Kt'
   64 |                                 S.erase(S.find({ M[w(i,j)] ,w(i, j) }));
      |                                         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:812:9: note: candidate: 'template<class _Kt> decltype (std::set<_Key, _Compare, _Alloc>::const_iterator{((const std::set<_Key, _Compare, _Alloc>*)this)->std::set<_Key, _Compare, _Alloc>::_M_t._M_find_tr(__x)}) std::set<_Key, _Compare, _Alloc>::find(const _Kt&) const [with _Key = std::pair<int, std::pair<int, int> >; _Compare = std::less<std::pair<int, std::pair<int, int> > >; _Alloc = std::allocator<std::pair<int, std::pair<int, int> > >]'
  812 |         find(const _Kt& __x) const
      |         ^~~~
/usr/include/c++/14/bits/stl_set.h:812:9: note:   template argument deduction/substitution failed:
a.cc:64:47: note:   couldn't deduce template parameter '_Kt'
   64 |                                 S.erase(S.find({ M[w(i,j)] ,w(i, j) }));
      |                                         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:796:7: note: candidate: 'std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::find(const key_type&) [with _Key = std::pair<int, std::pair<int, int> >; _Compare = std::less<std::pair<int, std::pair<int, int> > >; _Alloc = std::allocator<std::pair<int, std::pair<int, int> > >; iterator = std::_Rb_tree<std::pair<int, std::pair<int, int> >, std::pair<int, std::pair<int, int> >, std::_Identity<std::pair<int, std::pair<int, int> > >, std::less<std::pair<int, std::pair<int, int> > >, std::allocator<std::pair<int, std::pair<int, int> > > >::const_iterator; key_type = std::pair<int, std::pair<int, int> >]'
  796 |       find(const key_type& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_set.h:796:28: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const std::set<std::pair<int, std::pair<int, int> > >::key_type&' {aka 'const std::pair<int, std::pair<int, int> >&'}
  796 |       find(const key_type& __x)
      |            ~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_set.h:800:7: note: candidate: 'std::set<_Key, _Compare, _Alloc>::const_iterator std::set<_Key, _Compare, _Alloc>::find(const key_type&) const [with _Key = std::pair<int, std::pair<int, int> >; _Compare = std::less<std::pair<int, std::pair<int, int> > >; _Alloc = std::allocator<std::pair<int, std::pair<int, int> > >; const_iterator = std::_Rb_tree<std::pair<int, std::pair<int, int> >, std::pair<int, std::pair<int, int> >, std::_Identity<std::pair<int, std::pair<int, int> > >, std::less<std::pair<int, std::pair<int, int> > >, std::allocator<std::pair<int, std::pair<int, int> > > >::const_iterator; key_type = std::pair<int, std::pair<int, int> >]'
  800 |       find(const key_type& __x) const
      |       ^~~~
/usr/include/c++/14/bits/stl_set.h:800:28: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const std::set<std::pair<int, std::pair<int, int> > >::key_type&' {aka 'const std::pair<int, std::pair<int, int> >&'}
  800 |       find(const key_type& __x) const
      |            ~~~~~~~~~~~~~~~~^~~
a.cc:65:34: error: no match for 'operator[]' (operand types are 'int' and 'std::pair<int, int>')
   65 |                                 M[w(i, j)]--;
      |                                  ^
a.cc:66:45: error: no match for 'operator[]' (operand types are 'int' and 'std::pair<int, int>')
   66 |                                 S.insert({ M[w(i,j)], w(i,j) });
      |                                             ^
a.cc:66:41: error: no matching function for call to 'std::set<std::pair<int, std::pair<int, int> > >::insert(<brace-enclosed initializer list>)'
   66 |                                 S.insert({ M[w(i,j)], w(i,j) });
      |                                 ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:568:9: note: candidate: 'template<class _InputIterator> void std::set<_Key, _Compare, _Alloc>::insert(_InputIterator, _InputIterator) [with _Key = std::pair<int, std::pair<int, int> >; _Compare = std::less<std::pair<int, std::pair<int, int> > >; _Alloc = std::allocator<std::pair<int, std::pair<int, int> > >]'
  568 |         insert(_InputIterator __first, _InputIterator __last)
      |         ^~~~~~
/usr/include/c++/14/bits/stl_set.h:568:9: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/14/bits/stl_set.h:511:7: note: candidate: 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = std::pair<int, std::pair<int, int> >; _Compare = std::less<std::pair<int, std::pair<int, int> > >; _Alloc = std::allocator<std::pair<int, std::pair<int, int> > >; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree<std::pair<int, std::pair<int, int> >, std::pair<int, std::pair<int, int> >, std::_Identity<std::pair<int, std::pair<int, int> > >, std::less<std::pair<int, std::pair<int, int> > >, std::allocator<std::pair<int, std::pair<int, int> > > >::const_iterator; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other = std::allocator<std::pair<int, std::pair<int, int> > >; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key> = __gnu_cxx::__alloc_traits<std::allocator<std::pair<int, std::pair<int, int> > >, std::pair<int, std::pair<int, int> > >::rebind<std::pair<int, std::pair<int, int> > >; typename _Alloc::value_type = std::pair<int, std::pair<int, int> >; value_type = std::pair<int, std::pair<int, int> >]'
  511 |       insert(const value_type& __x)
      |       ^~~~~~
/usr/include/c++/14/bits/stl_set.h:511:32: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const std::set<std::pair<int, std::pair<int, int> > >::value_type&' {aka 'const std::pair<int, std::pair<int, int> >&'}
  511 |       insert(const value_type& __x)
      |              ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_set.h:520:7: note: candidate: 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(value_type&&) [with _Key = std::pair<int, std::pair<int, int> >; _Compare = std::less<std::pair<int, std::pair<int, int> > >; _Alloc = std::allocator<std::pair<int, std::pair<int, int> > >; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree<std::pair<int, std::pair<int, int> >, std::pair<int, std::pair<int, int> >, std::_Identity<std::pair<int, std::pair<int, int> > >, std::less<std::pair<int, std::pair<int, int> > >, std::allocator<std::pair<int, std::pair<int, int> > > >::const_iterator; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other = std::allocator<std::pair<int, std::pair<int, int> > >; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key> = __gnu_cxx::__alloc_traits<std::allocator<std::pair<int, std::pair<int, int> > >, std::pair<int, std::pair<int, int> > >::rebind<std::pair<int, std::pair<int, int> > >; typename _Alloc::value_type = std::pair<int, std::pair<int, int> >; value_type = std::pair<int, std::pair<int, int> >]'
  520 |       insert(value_type&& __x)
      |       ^~~~~~
/usr/include/c++/14/bits/stl_set.h:520:27: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'std::set<std::pair<int, std::pair<int, int> > >::value_type&&' {aka 'std::pair<int, std::pair<int, int> >&&'}
  520 |       insert(value_type&& __x)
      |              ~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_set.h:548:7: note: candidate: 'std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::insert(const_iterator, const value_type&) [with _Key = std::pair<int, std::pair<int, int> >; _Compare = std::less<std::pair<int, std::pair<int, int> > >; _Alloc = std::allocator<std::pair<int, std::pair<int, int> > >; iterator = std::_Rb_tree<std::pair<int, std::pair<int, int> >, std::pair<int, std::pair<int, int> >, std::_Identity