#include <bits/stdc++.h>
#define eb emplace_back
#define mp make_pair
#define F first
#define S second
using namespace std;
using pii = pair<int, int>;
map<string, int> id;
int num = 0;
int getid(string& s){
if(id.find(s) == id.end()) id[s] = num++;
return id[s];
}
int n;
vector<vector<int>> g;
bool check(int msk){
for(int i = 0; i < n; i++){
int cnt = 0;
for(int j : g[i]){
if(!(1 << j & msk)) cnt++;
}
//cerr << bitset<8>(msk) << " " << i << " " << cnt << "\n";
if(1 << i & msk){
if(cnt > 1) return false;
}
else{
if(cnt > 0) return false;
}
}
//cerr << "ok " << bitset<8>(msk) << "\n";
return true;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
g.resize(n);
vector<int> f(n);
for(int i = 1; i <= n; i++){
string s, t;
cin >> s >> t;
int u = getid(s), v = getid(t);
f[u] = v;
}
for(int i = 0; i < n; i++){
if(f[f[i]] == i && f[i] != i) continue;
g[f[i]].eb(i);
}
//for(auto& i : id) cerr << i.F << " " << i.S << "\n";
if(n % 2){
cout << "-1\n";
return 0;
}
int ans = n;
for(int i = 0; i < (1 << n); i++){
if(check(i)) ans = min(ans, __builtin_popcount(i));
}
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
308 KB |
Output is correct |
2 |
Correct |
27 ms |
308 KB |
Output is correct |
3 |
Correct |
11 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
224 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
16 ms |
316 KB |
Output is correct |
8 |
Correct |
16 ms |
320 KB |
Output is correct |
9 |
Correct |
10 ms |
316 KB |
Output is correct |
10 |
Correct |
15 ms |
320 KB |
Output is correct |
11 |
Correct |
14 ms |
312 KB |
Output is correct |
12 |
Correct |
0 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 |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Incorrect |
186 ms |
16196 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
172 ms |
15328 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
308 KB |
Output is correct |
2 |
Correct |
27 ms |
308 KB |
Output is correct |
3 |
Correct |
11 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
224 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
16 ms |
316 KB |
Output is correct |
8 |
Correct |
16 ms |
320 KB |
Output is correct |
9 |
Correct |
10 ms |
316 KB |
Output is correct |
10 |
Correct |
15 ms |
320 KB |
Output is correct |
11 |
Correct |
14 ms |
312 KB |
Output is correct |
12 |
Correct |
0 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 |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Incorrect |
186 ms |
16196 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |