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;
 
int main(){
	cin.tie(0); ios::sync_with_stdio(0);
	int n, c = 0, sol = 0;
	cin >> n;
	if(n&1){
		cout << "-1\n";
		exit(0);
	}
	vector<int> love(n), deg(n), s, active(n, 1);
	map<string,int> dns;
	set<int> single;
	for(int i = 0; i < n; i++){
		single.insert(i);
		string a, b;
		cin >> a >> b;
		if(!dns.count(a)) dns[a] = c++;
		if(!dns.count(b)) dns[b] = c++;
		love[dns[a]] = dns[b];
		deg[dns[b]]++;
	}
	for(int i = 0; i < n; i++) if(deg[i] == 0) s.push_back(i);
	int i = 0;
	for(int j = 0; j < n; j++){
		if(active[j] && love[love[j]] == j && love[j] != j){
			active[j] = 0, active[love[j]] = 0;
			single.erase(j), single.erase(love[j]);
			i += 2;
		}
	}
	for(; i < n; i++){
		if(s.empty()) s.push_back(*single.begin());
		int u = s.back();
		s.pop_back();
		active[u] = 0;
		single.erase(u);
		sol++;
		if(active[love[u]]){
			i++;
			active[love[u]] = 0;
			single.erase(love[u]);
			if(love[love[u]] == u) sol--, cout << "WHAT\n";
		}
	}
	cout << sol << "\n";
}
| # | 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... |