Language: C++

#include<cstdio>
#include<algorithm>
#include<stack>
#define st first
#define nd second

using namespace std;
struct O{
		int st;
		int nd;
		int val;
};
const int N = 2e3 + 10;

int dp[N][N],   a[3 * N], fr[N] , ba[N];

int main()
{
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= 3 * n; i++) {
		scanf("%d", &a[i]);
	}
	dp[a[1]][a[2]] = dp[a[2]][a[1]] = ba[a[2]] = ba[a[1]] = fr[a[2]] = fr[a[1]] =1;
	int col = 0, ma = 1;
	for(int i = 3; i < 3 *  n; i += 3){
		if(a[i] == a[i + 1] && a[i] == a[i + 2]) {
			col ++;
			continue;
		}
		sort(a + i, a + i + 3);
		stack<O> stx;
		if(a[i + 1] == a[i + 2]){
			swap(a[i], a[i + 2]);	
		}	    
		if(a[i] == a[i + 1]){
			for(int j = 1; j <= n; j++){
				if(dp[a[i]][j] != 0) stx.push({ a[i + 2], j, max(dp[a[i]][j] + 1, ma)});
				if(dp[j][a[i]] != 0) stx.push({j, a[i + 2], max(dp[j][a[i]] + 1, ma)});
			}
			stx.push({a[i], a[i], max(dp[a[i + 2]][a[i + 2]] + 1, ma});			
		}
		else{
			int x = max(ma, dp[a[i]][a[i]] + 1), y = max(ma, dp[a[i + 1]][a[ i +1]] + 1), z = max(ma, dp[a[i + 2]][a[i+2]] + 1);
			stx.push({a[i + 1], a[i + 2], x}), stx.push({a[i], a[i +2], y}), stx.push({a[i], a[i + 1], z});
			stx.push({a[i + 2], a[i + 1], x}), stx.push({a[i + 2], a[i], y}), stx.push({a[i + 1], a[i], z});
			for(int j = 1; j <= n; j++){
				for(int k = 0; k < 3; k++){
					if(fr[j] !=  0) stx.push({j, a[i + k], fr[j]});
					if(ba[j] != 0)  stx.push({a[i + k], j, ba[j]});
				}
			}
		}
		while(!stx.empty()){
			int x = stx.top().st, y = stx.top().nd, z= stx.top().val;
			stx.pop();
			dp[x][y] = max(dp[x][y], z);
			fr[x] = max(fr[x], z);
			ba[y] = max(ba[y],  z);
			ma = max(ma, z);
		}	
	}
	printf("%d", col +  max(ma, dp[a[3 *n]][a[3 * n]] + 1) - 1);
}		

Compiler output:
a.cc: In function 'int main()':
a.cc:41:81: error: expected ')' before '}' token
   41 |                         stx.push({a[i], a[i], max(dp[a[i + 2]][a[i + 2]] + 1, ma});
      |                                                  ~                              ^
      |                                                                                 )
a.cc:41:33: error: no matching function for call to 'std::stack<O>::push(<brace-enclosed initializer list>)'
   41 |                         stx.push({a[i], a[i], max(dp[a[i + 2]][a[i + 2]] + 1, ma});
      |                         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/stack:63,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_stack.h:258:7: note: candidate: 'void std::stack<_Tp, _Sequence>::push(const value_type&) [with _Tp = O; _Sequence = std::deque<O, std::allocator<O> >; value_type = O]'
  258 |       push(const value_type& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_stack.h:258:30: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const std::stack<O>::value_type&' {aka 'const O&'}
  258 |       push(const value_type& __x)
      |            ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_stack.h:263:7: note: candidate: 'void std::stack<_Tp, _Sequence>::push(value_type&&) [with _Tp = O; _Sequence = std::deque<O, std::allocator<O> >; value_type = O]'
  263 |       push(value_type&& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_stack.h:263:25: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'std::stack<O>::value_type&&' {aka 'O&&'}
  263 |       push(value_type&& __x)
      |            ~~~~~~~~~~~~~^~~