이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
const int maxn5 = 2e6 + 10;
int parof[maxn5];
int dp[maxn5];
map <string, int> av;
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n; cin >> n;
int ind = 0;
for(int i = 0; i < n; i++){
string s, t; cin >> s >> t;
if(av.find(s) == av.end())
av[s] = ind++;
if(av.find(t) == av.end())
av[t] = ind++;
parof[av[s]] = av[t];
}
/*
for(auto it = av.begin(); it != av.end(); it++)
cout << (it -> fi) << ' ' << (it -> se) << endl;
//*/
if(n % 2 == 1)
return cout << -1 << endl, 0;
for(int mask = 1; mask < (1 << n); mask++) if(__builtin_popcount(mask) % 2 == 0){
int v = 0;
for(int i = 0; i < n; i++) if((mask >> i)&1)
v = i;
dp[mask] = n + 2;
for(int i = 0; i < v; i++) if((mask >> i)&1)
dp[mask] = min(dp[mask], dp[mask ^ (1 << v) ^ (1 << i)] + (parof[i] != v) + (parof[v] != i));
}
cout << dp[(1 << n) - 1] << endl;
}
# | 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... |