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>
#define ll long long
#define ar array
#define db double
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rint(l, r) uniform_int_distribution<int>(l, r)(rng)
template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
void test_case() {
int n; cin >> n;
map<string, int> mp;
vector<int> a(n);
for (int i = 0; i < n; i++) {
string s, t;
cin >> s >> t;
if (!mp.count(s)) mp[s] = mp.size();
if (!mp.count(t)) mp[t] = mp.size();
a[mp[s]] = mp[t];
}
if (n & 1) {
cout << -1 << '\n'; return;
}
vector<int> dp(1 << n, 1e9);
dp[0] = 0;
for (int m = 0; m < 1 << n; m++) {
for (int i = 0; i < n; i++) if (!(m >> i & 1)) {
for (int j = 0; j < n; j++) if (i != j && !(m >> j & 1)) {
ckmin(dp[m | 1 << i | 1 << j], dp[m] + (a[i] != j) + (a[j] != i));
}
}
}
cout << dp.back() << '\n';
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
// cin >> t;
while (t--) test_case();
}
# | 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... |