#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define ar array
#define all(x) x.begin(), x.end()
#define siz(x) (int) x.size()
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
#define FOR(i, a, b) for(int i=(a); i<(b); i++)
#define ROF(i, a, b) for(int i=(b-1); i>=(a); i--)
#define F0R(i, n) FOR(i, 0, n)
#define R0F(i, n) ROF(i, 0, n)
#define trav(x, y) for(auto&x:y)
using ll=long long;
using ld=long double;
using pii=pair<int, int>;
using pll=pair<ll, ll>;
using pli=pair<ll, int>;
using vi=vector<int>;
using vl=vector<ll>;
using vpii=vector<pii>;
template<class T> inline bool ckmin(T&a, const T&b) {return b<a?a=b,1:0;}
template<class T> inline bool ckmax(T&a, const T&b) {return b>a?a=b,1:0;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const char nl='\n';
const int mxN=20;
const int MOD=1e9+7;
const ll infLL=1e18;
const ld eps=1e-6;
int a[mxN];
map<string, int> mp;
string s[mxN], t[mxN];
int dp[1<<mxN];
int32_t main(){
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
if(n&1) {
cout << "-1\n";
return 0;
}
F0R(i, n)
cin >> s[i] >> t[i], mp[s[i]]=i;
F0R(i, n){
a[i]=mp[t[i]];
// cout << a[i] << nl;
}
memset(dp, 0x3f, sizeof dp);
dp[0]=0;
F0R(i, 1<<n){
if(__builtin_popcount(i)&1) continue;
F0R(j, n) F0R(k, n){
if(((1<<j)&i)||((1<<k)&i)) continue;
ckmin(dp[i|(1<<j)|(1<<k)], dp[i]+(a[j]!=k)+(a[k]!=j));
}
}
cout<<dp[(1<<n)-1]<<nl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
417 ms |
4400 KB |
Output is correct |
2 |
Correct |
421 ms |
4412 KB |
Output is correct |
3 |
Correct |
425 ms |
4416 KB |
Output is correct |
4 |
Correct |
2 ms |
4300 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
432 ms |
4300 KB |
Output is correct |
8 |
Correct |
411 ms |
4412 KB |
Output is correct |
9 |
Correct |
420 ms |
4420 KB |
Output is correct |
10 |
Correct |
418 ms |
4416 KB |
Output is correct |
11 |
Correct |
424 ms |
4408 KB |
Output is correct |
12 |
Correct |
2 ms |
4432 KB |
Output is correct |
13 |
Correct |
2 ms |
4428 KB |
Output is correct |
14 |
Correct |
2 ms |
4428 KB |
Output is correct |
15 |
Correct |
2 ms |
4428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4300 KB |
Output is correct |
2 |
Correct |
2 ms |
4300 KB |
Output is correct |
3 |
Correct |
2 ms |
4300 KB |
Output is correct |
4 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
460 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
417 ms |
4400 KB |
Output is correct |
2 |
Correct |
421 ms |
4412 KB |
Output is correct |
3 |
Correct |
425 ms |
4416 KB |
Output is correct |
4 |
Correct |
2 ms |
4300 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
432 ms |
4300 KB |
Output is correct |
8 |
Correct |
411 ms |
4412 KB |
Output is correct |
9 |
Correct |
420 ms |
4420 KB |
Output is correct |
10 |
Correct |
418 ms |
4416 KB |
Output is correct |
11 |
Correct |
424 ms |
4408 KB |
Output is correct |
12 |
Correct |
2 ms |
4432 KB |
Output is correct |
13 |
Correct |
2 ms |
4428 KB |
Output is correct |
14 |
Correct |
2 ms |
4428 KB |
Output is correct |
15 |
Correct |
2 ms |
4428 KB |
Output is correct |
16 |
Correct |
2 ms |
4300 KB |
Output is correct |
17 |
Correct |
2 ms |
4300 KB |
Output is correct |
18 |
Correct |
2 ms |
4300 KB |
Output is correct |
19 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 11 |
20 |
Halted |
0 ms |
0 KB |
- |