#include "bits/stdc++.h"
using namespace std;
// Hi
int main(){
ios::sync_with_stdio(0);cin.tie(0);
int n;
cin>>n;
vector<int> go(n);
map<string,int> mp;
int nxt = 0;
vector<pair<int,int>> edges;
for(int i=0;i<n;i++){
string s,t;
cin>>s>>t;
int first = 0,second = 0;
if(mp.find(s) != mp.end()){
first = mp[s];
}else{
first = nxt;
mp[s] = nxt++;
}
if(mp.find(t) != mp.end()){
second = mp[t];
}else{
second = nxt;
mp[t] = nxt++;
}
edges.push_back({first,second});
}
vector<int> perm;
if(n&1){
cout << -1 << endl;
return 0;
}
auto good = [&](){
vector<bool> vis(n+1,0);
for(int i=0;i<n;i++){
if(go[i] == i)return false;
if(go[i] != -1){
if(go[go[i]] != -1 && go[go[i]] != i)return false;
if(vis[go[i]])return false;
vis[go[i]] = 1;
}
}
return true;
};
int ans = 1000000009;
for(int i=0;i<(1 << n);i++){
int cnt = n;
go.assign(n,-1);
for(int j=0;j<n;j++){
if(i & (1<<j)){
go[edges[j].first] = edges[j].second;
cnt--;
}
}
/*
for(auto x:go)cout << x<<" ";
cout<<'\n';*/
if(good()){
/*
for(auto x:go)cout << x<<" ";
cout<<'\n';*/
ans = min(ans,cnt);
}
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
147 ms |
288 KB |
Output is correct |
2 |
Correct |
141 ms |
324 KB |
Output is correct |
3 |
Correct |
156 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
149 ms |
292 KB |
Output is correct |
8 |
Correct |
146 ms |
288 KB |
Output is correct |
9 |
Correct |
140 ms |
292 KB |
Output is correct |
10 |
Correct |
159 ms |
204 KB |
Output is correct |
11 |
Correct |
151 ms |
292 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
245 ms |
9200 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
183 ms |
9312 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
147 ms |
288 KB |
Output is correct |
2 |
Correct |
141 ms |
324 KB |
Output is correct |
3 |
Correct |
156 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
149 ms |
292 KB |
Output is correct |
8 |
Correct |
146 ms |
288 KB |
Output is correct |
9 |
Correct |
140 ms |
292 KB |
Output is correct |
10 |
Correct |
159 ms |
204 KB |
Output is correct |
11 |
Correct |
151 ms |
292 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Incorrect |
245 ms |
9200 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |