This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 11;
struct edge{
	int u, v;
};
int p[MAXN];
set<int> G[MAXN];
bool done[MAXN];
template<class T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
int32_t main(){
	set<string> S; map<string, int> mp;
	int n; cin >> n;
	if(n % 2 == 1){
		cout << -1 << endl;
		return 0;
	}
	vector<pair<string, string>> ES;
	vector<edge> E;
	for(int i = 0; i < n; i++){
		string s, t; cin >> s >> t; S.insert(s); S.insert(t);
		ES.push_back({s, t});
	}
	int id = 0; for(auto i : S) mp[i] = id++;
	for(int i = 0; i < n; i++){
		E.push_back({mp[ES[i].first], mp[ES[i].second]});
	}
	for(int i = 0; i < n; i++) p[E[i].u] = E[i].v;
	int res = 0;
	for(int i = 0; i < n; i++){
		if(p[i] != i && p[p[i]] == i) done[i] = true, res++;
		else if(p[i] != i){
			G[p[i]].insert(i); G[i].insert(p[i]);
		}
	}
	for(int i = 0; i < n; i++){
		vector<int> to_e;
		if(done[i]) G[i].clear();
		for(auto u : G[i]){
			if(done[u]) to_e.push_back(u);
		}
		for(auto u : to_e){
			G[i].erase(u);
		}
	}
	min_priority_queue<pair<int, int>> pq;
	for(int i = 0; i < n; i++){
		if(G[i].size() != 0 && !done[i]) pq.push({G[i].size(), i});
	}
	while(!pq.empty()){
		auto [sz, v] = pq.top(); pq.pop();
		if(sz == 0) continue;
		if(G[v].size() != sz) continue;
		auto u = *G[v].begin();
		for(auto x : G[v]){
			int f, t;
			f = G[x].size();
			G[x].erase(v);
			t = G[x].size();
			if(t < f) pq.push({G[x].size(), x});
		}
		for(auto x : G[u]){
			int f, t;
			f = G[x].size();
			G[x].erase(u);
			t = G[x].size();
			if(t < f) pq.push({G[x].size(), x});
		}
		// cout << "T " << u << ' '  << v << endl;
		G[u].clear(); G[v].clear();
		res++;
	}
	int ans = n - res;
	cout << ans << endl;
}
Compilation message (stderr)
polygon.cpp: In function 'int32_t main()':
polygon.cpp:55:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   55 |   auto [sz, v] = pq.top(); pq.pop();
      |        ^
polygon.cpp:57:18: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'std::tuple_element<0, std::pair<int, int> >::type' {aka 'int'} [-Wsign-compare]
   57 |   if(G[v].size() != sz) continue;
      |      ~~~~~~~~~~~~^~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |