Language: C++

#include <cassert>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;


template<class T> inline void chmax(T& a, T b) { if(b > a) a = b; }

const int A = 0, M = 1, INF = 0xfffffff;

int V;
vector<int> point;
vector<int> cpoint;
vector<vector<int> > G;
vector<vector<int> > rG;
vector<int> vs;
vector<bool> used;
vector<int> cmp;
vector<int> size;
vector<vector<int> > cG;

void init(int v) {
	V = v;
	point.resize(v, 0);
	G.resize(v);
	rG.resize(v);
	used.resize(v);
	cmp.resize(v);
}

void add_edge(int from, int to) {
	G[from].push_back(to);
	rG[to].push_back(from);
}

void dfs(int v) {
	used[v] = true;
	for(int i = 0; i < G[v].size(); ++i)
		if(!used[G[v][i]])
			dfs(G[v][i]);

	vs.push_back(v);
}

void rdfs(int v, int k) {
	used[v] = true;
	cmp[v] = k;
	++size[k];
	for(int i = 0; i < rG[v].size(); ++i)
		if(!used[rG[v][i]])
			rdfs(rG[v][i], k);
}

int scc() {
	fill(used.begin(), used.end(), false);
	vs.clear();
	for(int v = 0; v < V; ++v)
		if(!used[v])
			dfs(v);

	fill(used.begin(), used.end(), false);
	int k = 0;
	for(int i = vs.size() - 1; i >= 0; --i) {
		size.push_back(0);
		if(!used[vs[i]])
			rdfs(vs[i], k++);
	}

	return k;
}

bool infinite() {
	for(int i = 0; i < size.size(); ++i) {
		if(size[i] > 1) {
			for(int j = 0; j < V; ++j)
				if(cmp[j] == i && point[j])
					return true;
		}
	}

	return false;
}

vector<int> n(2), m(2);
vector<string> c(2);
vector<vector<int> > es[2];

int main() {
	cin.tie(false);
	ios::sync_with_stdio(false);

	for(int i = A; i <= M; ++i) {
		cin >> n[i] >> m[i];

		for(int j = 0; j < n[i]; ++j) {
			char in;
			cin >> in;
			c[i] += in;
		}

		es[i].resize(n[i]);
		for(int j = 0; j < m[i]; ++j) {
			int a, b;
			cin >> a >> b;
			--a; --b;

			es[i][a].push_back(b);
		}
	}

	init(n[A] * n[M]);

	for(int i = 0; i < n[A]; ++i) {
		for(int k = 0; k < n[M]; ++k) {
			int from = i * n[M] + k;
			for(int j = 0; j < es[A][i].size(); ++j)
				for(int l = 0; l < es[M][k].size(); ++l)
					add_edge(from, es[A][i][j] * n[M] + es[M][k][l]);

			if(c[A][i] == c[M][k])
				point[from] = 1;

			else {
				for(int j = 0; j < es[A][i].size(); ++j)
					add_edge(from, es[A][i][j] * n[M] + k);

				for(int l = 0; l < es[M][k].size(); ++l)
					add_edge(from, i * n[M] + es[M][k][l]);
			}
		}
	}

	int num = scc();

	if(infinite()) {
		cout << -1 << endl;
		return EXIT_SUCCESS;
	}

	cG.resize(num);
	for(int i = 0; i < V; ++i)
		for(int j = 0; j < G[i].size(); ++j)
			if(cmp[i] != cmp[G[i][j]])
				cG[cmp[i]].push_back(cmp[G[i][j]]);

	cpoint.resize(num, 0);
	for(int i = 0; i < V; ++i)
		cpoint[cmp[i]] += point[i];

	for(int i = 0; i < num; ++i) {
		sort(cG[i].begin(), cG[i].end());
		cG[i].erase(unique(cG[i].begin(), cG[i].end()), cG[i].end());
	}

	vector<int> dp(num, 0);
	for(int i = 0; i < num; ++i)
		for(int j = 0; j < cG[i].size(); ++j)
			chmax(dp[cG[i][j]], dp[i] + cpoint[i]);

	cout << dp[num - 1] + cpoint[num - 1] << endl;



	return EXIT_SUCCESS;
}

Compiler output:
a.cc: In function 'void rdfs(int, int)':
a.cc:50:11: error: reference to 'size' is ambiguous
   50 |         ++size[k];
      |           ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:3:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:21:13: note:                 'std::vector<int> size'
   21 | vector<int> size;
      |             ^~~~
a.cc: In function 'int scc()':
a.cc:66:17: error: reference to 'size' is ambiguous
   66 |                 size.push_back(0);
      |                 ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:21:13: note:                 'std::vector<int> size'
   21 | vector<int> size;
      |             ^~~~
a.cc: In function 'bool infinite()':
a.cc:75:28: error: reference to 'size' is ambiguous
   75 |         for(int i = 0; i < size.size(); ++i) {
      |                            ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:21:13: note:                 'std::vector<int> size'
   21 | vector<int> size;
      |             ^~~~
a.cc:76:20: error: reference to 'size' is ambiguous
   76 |                 if(size[i] > 1) {
      |                    ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:21:13: note:                 'std::vector<int> size'
   21 | vector<int> size;
      |             ^~~~
a.cc: In function 'int main()':
a.cc:91:16: error: no matching function for call to 'std::basic_istream<char>::tie(bool)'
   91 |         cin.tie(false);
      |         ~~~~~~~^~~~~~~
In file included from /usr/include/c++/14/ios:46:
/usr/include/c++/14/bits/basic_ios.h:299:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>* std::basic_ios<_CharT, _Traits>::tie() const [with _CharT = char; _Traits = std::char_traits<char>]'
  299 |       tie() const
      |       ^~~
/usr/include/c++/14/bits/basic_ios.h:299:7: note:   candidate expects 0 arguments, 1 provided
/usr/include/c++/14/bits/basic_ios.h:311:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>* std::basic_ios<_CharT, _Traits>::tie(std::basic_ostream<_CharT, _Traits>*) [with _CharT = char; _Traits = std::char_traits<char>]'
  311 |       tie(basic_ostream<_CharT, _Traits>* __tiestr)
      |       ^~~
/usr/include/c++/14/bits/basic_ios.h:311:43: note:   no known conversion for argument 1 from 'bool' to 'std::basic_ostream<char>*'
  311 |       tie(basic_ostream<_CharT, _Traits>* __tiestr)
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
a.cc:153:17: error: 'sort' was not declared in this scope; did you mean 'short'?
  153 |                 sort(cG[i].begin(), cG[i].end());
      |                 ^~~~
      |                 short
a.cc:154:29: error: 'unique' was not declared in this scope
  154 |                 cG[i].erase(unique(cG[i].begin(), cG[i].end()), cG[i].end());
      |                             ^~~~~~