#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;
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++;
}
go[first] = second;
}
vector<int> perm;
for(int i=0;i<n;i++){
perm.push_back(i);
}
int ans = 1000000009;
do{
bool can = 1;
for(int i=0;i<n;i++){
if(perm[i] == i || perm[perm[i]] != i){
can = 0;
break;
}
}
if(can){
int cnt = 0;
for(int i=0;i<n;i++){
//cout <<perm[i]<<" ";
cnt += (perm[i] != go[i]);
}
//cout<<'\n';
ans = min(ans,cnt);
}
}while(next_permutation(perm.begin(),perm.end()));
cout << (ans == 1000000009 ? -1 : ans) << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2057 ms |
204 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Execution timed out |
2081 ms |
9068 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2065 ms |
9084 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2057 ms |
204 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |