#include <bits/stdc++.h>
using namespace std;
int n;
int nxt[100000];
string p[100000];
map<string, int> mp;
inline void Subtask1()
{
vector<int> f;
f.resize(1 << n, 1e9);
f[0] = 0;
for (int i = 0; i < (1 << n); ++i)
{
if (__builtin_popcount(i) & 1)
{
continue;
}
for (int j = 0; j < n; ++j)
{
for (int k = j + 1; k < n; ++k)
{
if (((i >> j) & 1) && ((i >> k) & 1))
{
f[i] = min(f[i], (nxt[j] != k) + (nxt[k] != j) + f[i - (1 << j) - (1 << k)]);
}
}
}
}
cout << f[(1 << n) - 1];
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n; ++i)
{
cin >> p[i];
mp[p[i]] = i;
cin >> p[i];
}
for (int i = 0; i < n; ++i)
{
nxt[i] = mp[p[i]];
}
if (n & 1)
{
cout << -1;
return 0;
}
Subtask1();
return 0;
}
| # | 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... |