#include <iostream>
#include <set>
#include <map>
using namespace std;
#define MAXN 100005
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define f first
#define s second
int n, ec[MAXN];
string st[MAXN], nx[MAXN];
map<string, int> idx;
set<int> adj[MAXN];
bool d[MAXN];
int main(){
cin >> n;
FOR(i, 1, n){
cin >> st[i];
idx.insert({st[i], i});
cin >> nx[i];
ec[i] = 0;
}
if(n % 2) {
cout << -1 << endl;
return 0;
}
int r = 0;
FOR(i, 1, n){
d[i] = (idx[nx[idx[nx[i]]]] == i && idx[nx[i]] != i);
}
FOR(i, 1, n){
if(i == idx[nx[i]] || d[i] || d[idx[nx[i]]]) continue;
ec[i]++, adj[i].insert(idx[nx[i]]);
ec[idx[nx[i]]]++, adj[idx[nx[i]]].insert(i);
}
set<pair<int, int>> se;
FOR(i, 1, n) if(!d[i]) se.insert({adj[i].size(), i});
while(se.size()){
auto si = *se.begin();
d[si.f] = true;
if(si.f == 0){
r++;
//cout << st[si.s] << " FALLALEUJAH! " << endl;
}else{
int j = 0, jmn = INT_MAX;
for(int i : adj[si.s]){
if(!d[i]){
if(jmn > ec[i]) j = i, jmn = ec[i];
}
}
r++;
//cout << st[si.s] << " ~ " << st[j] << " : " << si.s << " " << j << endl;
se.erase({jmn, j});
for(int j2 : adj[j]){
se.erase({ec[j2], j2});
se.insert({ec[j2], j2});
}
}
for(int i2 : adj[si.s]) {
se.erase({ec[i2], i2});
ec[i2]--;
se.insert({ec[i2], i2});
}
se.erase(se.begin());
}
cout << r << endl;
}
Compilation message
polygon.cpp: In function 'int main()':
polygon.cpp:52:30: error: 'INT_MAX' was not declared in this scope
52 | int j = 0, jmn = INT_MAX;
| ^~~~~~~
polygon.cpp:4:1: note: 'INT_MAX' is defined in header '<climits>'; did you forget to '#include <climits>'?
3 | #include <map>
+++ |+#include <climits>
4 | using namespace std;