//Bismi Allah
#include "bits/stdc++.h"
using namespace std;
signed main () {
// ios::sync_with_stdio(0);
// cin.tie(0); cout.tie(0);
int n, ans = 1e9;
cin >> n;
vector <int> who(n, -1), used(n, 0);
set <int> unused;
map <string,int> mp;
function<void(int,int)> f=[&](int v, int cnt) {
//cout << v << " " << cnt << "\n";
if(v == 1e9) {
ans = min(ans, cnt);
return;
}
used[v]=1;
unused.erase(v);
for(int i = 0; i < used.size(); i ++) {
if(!used[i]) {
used[i]=1;
unused.erase(i);
int newcnt = cnt;
if(who[i] != v) newcnt ++;
if(who[v] != i) newcnt ++;
f(*unused.begin(), newcnt);
used[i]=0;
unused.insert(i);
}
}
unused.insert(v);
used[v]=0;
};
int cnt = 0;
for(int i = 0; i < n; i ++) {
string u, v;
cin >> u >> v;
if(mp.count(u) == 0) mp[u] = mp.size();
if(mp.count(v) == 0) mp[v] = mp.size();
if(mp[u] == mp[v]) cnt ++;
who[mp[u]] = mp[v];
}
if(n % 2 == 1) {
cout << "-1";
return 0;
}
for(int i = 0; i < n; i ++) unused.insert(i);
unused.insert(1e9);
f(0,0);
if(ans == 1e9) cout << "-1";
else cout << ans;
return 0;
}
Compilation message
polygon.cpp: In lambda function:
polygon.cpp:22:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for(int i = 0; i < used.size(); i ++) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2041 ms |
348 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Execution timed out |
2049 ms |
18608 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2037 ms |
16504 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2041 ms |
348 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |