Language: C++

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

vector<char> tag[2];
vector< vector<int> > e[2];

struct Node {
	unsigned char a, b;
};

int v[100][100];

int main() {
	fill(&v[0][0], &v[0][0]+100*100, -1);

	for (int t = 0; t < 2; ++t) {
		int N, M; cin >> N >> M;
		tag[t].resize(N);
		for (int i = 0; i < N; ++i) {
			cin >> tag[t][i];
		}
		e[t].resize(N);
		for (int i = 0; i < M; ++i) {
			int a, b; cin >> a >> b; --a, --b;
			e[t][a].push_back(b);
		}
	}

	queue<Node> Q[101];
	for (int a = 0; a < tag[0].size(); ++a) for (int b = 0; b < tag[1].size(); ++b) {
		Q[0].push({a, b});
	}

	for (int score = 0; score <= 100; ++score) {
		while ( !Q[score].empty() ) {
			Node node = Q[score].top(); Q[score].pop();
			int a = (int)node.a, b = (int)node.b;

			if (tag[0][a] == tag[1][b]) ++score;

			if (score <= v[a][b]) continue;
			v[a][b] = score;

			if (score > 100) continue;

			if (tag[0][a] != tag[1][b]) {
				for (int i = 0; i < e[0][a].size(); ++i) {
					Q[score].push({e[0][a][i], b});
				}
				for (int i = 0; i < e[1][b].size(); ++i) {
					Q[score].push({a, e[1][b][i]});
				}
			}
			for (int i = 0; i < e[0][a].size(); ++i) {
				for (int j = 0; j < e[1][b].size(); ++j) {
					Q[score].push({e[0][a][i], e[1][b][j]});
				}
			}
		}
	}
	int ans = 0;
	for (int a = 0; a < tag[0].size(); ++a) for (int b = 0; b < tag[1].size(); ++b) {
		ans = max(ans, v[a][b]);
	}
	if (ans > 100) cout << -1 << endl;
	else cout << ans << endl;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:34:28: warning: narrowing conversion of 'a' from 'int' to 'unsigned char' [-Wnarrowing]
   34 |                 Q[0].push({a, b});
      |                            ^
a.cc:34:31: warning: narrowing conversion of 'b' from 'int' to 'unsigned char' [-Wnarrowing]
   34 |                 Q[0].push({a, b});
      |                               ^
a.cc:39:46: error: 'class std::queue<Node>' has no member named 'top'; did you mean 'pop'?
   39 |                         Node node = Q[score].top(); Q[score].pop();
      |                                              ^~~
      |                                              pop
a.cc:51:54: warning: narrowing conversion of '(& e[0].std::vector<std::vector<int> >::operator[](((std::vector<std::vector<int> >::size_type)a)))->std::vector<int>::operator[](((std::vector<int>::size_type)i))' from '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} to 'unsigned char' [-Wnarrowing]
   51 |                                         Q[score].push({e[0][a][i], b});
      |                                         ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
a.cc:51:68: warning: narrowing conversion of 'b' from 'int' to 'unsigned char' [-Wnarrowing]
   51 |                                         Q[score].push({e[0][a][i], b});
      |                                                                    ^
a.cc:54:56: warning: narrowing conversion of 'a' from 'int' to 'unsigned char' [-Wnarrowing]
   54 |                                         Q[score].push({a, e[1][b][i]});
      |                                                        ^
a.cc:54:54: warning: narrowing conversion of '(& e[1].std::vector<std::vector<int> >::operator[](((std::vector<std::vector<int> >::size_type)b)))->std::vector<int>::operator[](((std::vector<int>::size_type)i))' from '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} to 'unsigned char' [-Wnarrowing]
   54 |                                         Q[score].push({a, e[1][b][i]});
      |                                         ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
a.cc:59:54: warning: narrowing conversion of '(& e[0].std::vector<std::vector<int> >::operator[](((std::vector<std::vector<int> >::size_type)a)))->std::vector<int>::operator[](((std::vector<int>::size_type)i))' from '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} to 'unsigned char' [-Wnarrowing]
   59 |                                         Q[score].push({e[0][a][i], e[1][b][j]});
      |                                         ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:59:54: warning: narrowing conversion of '(& e[1].std::vector<std::vector<int> >::operator[](((std::vector<std::vector<int> >::size_type)b)))->std::vector<int>::operator[](((std::vector<int>::size_type)j))' from '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} to 'unsigned char' [-Wnarrowing]