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;
#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];
set<pair<int, int>> se;
bool d[MAXN];
int main(){
cin >> n;
FOR(i, 1, n) ec[i] = 0;
FOR(i, 1, n){
cin >> st[i];
idx.insert({st[i], i});
cin >> nx[i];
}
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;
adj[i].insert(idx[nx[i]]);
adj[idx[nx[i]]].insert(i);
}
FOR(i, 1, n) if(!d[i]) se.insert({adj[i].size(), i});
int ct = n;
while(n && se.size()){
n--;
auto si = *se.begin();
if(si.f == 0){
r++;
}else{
int j = 0, jmn = INT_MAX;
for(int i : adj[si.s]){
if(jmn > adj[i].size()) j = i, jmn = adj[i].size();
}
r++;
se.erase({jmn, j});
for(int j2 : adj[j]){
se.erase({adj[j2].size(), j2});
adj[j2].erase(j);
se.insert({adj[j2].size(), j2});
}
}
for(int i2 : adj[si.s]) {
se.erase({adj[i2].size(), i2});
adj[i2].erase(si.s);
se.insert({adj[i2].size(), i2});
}
se.erase(se.begin());
}
cout << r << endl;
}
Compilation message (stderr)
polygon.cpp: In function 'int main()':
polygon.cpp:52:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::set<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | if(jmn > adj[i].size()) j = i, jmn = adj[i].size();
| ~~~~^~~~~~~~~~~~~~~
polygon.cpp:42:9: warning: unused variable 'ct' [-Wunused-variable]
42 | int ct = 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... |