#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();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
682 ms |
4440 KB |
Output is correct |
2 |
Correct |
692 ms |
4444 KB |
Output is correct |
3 |
Correct |
683 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
687 ms |
4444 KB |
Output is correct |
8 |
Correct |
688 ms |
4444 KB |
Output is correct |
9 |
Correct |
692 ms |
4444 KB |
Output is correct |
10 |
Correct |
695 ms |
4440 KB |
Output is correct |
11 |
Correct |
692 ms |
4444 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Runtime error |
134 ms |
17112 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
141 ms |
17200 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
682 ms |
4440 KB |
Output is correct |
2 |
Correct |
692 ms |
4444 KB |
Output is correct |
3 |
Correct |
683 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
687 ms |
4444 KB |
Output is correct |
8 |
Correct |
688 ms |
4444 KB |
Output is correct |
9 |
Correct |
692 ms |
4444 KB |
Output is correct |
10 |
Correct |
695 ms |
4440 KB |
Output is correct |
11 |
Correct |
692 ms |
4444 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Runtime error |
134 ms |
17112 KB |
Execution killed with signal 11 |
20 |
Halted |
0 ms |
0 KB |
- |