#include <bits/stdc++.h>
using namespace std;
#define ar array
#define int long long
#define ld long double
#define crash assert(69 == 420)
const int N = 2e5 + 20;
const int MOD = 1e9 + 7;
const int INF = 1e18;
const int X = 100;
int p[N];
int n;
namespace sub1{
vector<int> g[N];
bitset<N> vis;
int cnt = 0;
void dfs(int x){
vis[x] = 1;
cnt++;
for(auto u: g[x]){
if(!vis[u])dfs(u);
}
}
bool check(int msk){
vector<int> v;
for(int i = 0;i < n;i++){
if(p[i] == i){
if((1 << i) & msk){
}else return 0;
}
}
for(int i = 0;i < n;i++)g[i].clear();
vis &= 0;
for(int i = 0;i < n;i++){
if((1 << i) & msk){
}else{
g[i].push_back(p[i]);
g[p[i]].push_back(i);
}
}
for(int i = 0;i<n;i++){
if(!vis[i]){
cnt = 0;
dfs(i);
if(cnt > 2)return 0;
}
}
return 1;
}
};
signed main(){ios_base::sync_with_stdio(false);cin.tie(0);
//int n;
cin>>n;
if(n % 2){
cout<<-1<<endl;
return 0;
}
map<string, int> mp;
int G = 0;
for(int i = 0;i < n;i++){
string a, b;
cin>>a>>b;
if(!mp.count(a))mp[a] = G++;
if(!mp.count(b))mp[b] = G++;
p[mp[a]] = mp[b];
}
if(n <= 20){
int ans = INF;
for(int i = 0;i < (1 << n);i++){
if(sub1::check(i))ans = min(ans, (int)__builtin_popcount(i));
}
cout<<ans;
return 0;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
242 ms |
4956 KB |
Output is correct |
2 |
Correct |
275 ms |
5168 KB |
Output is correct |
3 |
Correct |
244 ms |
4952 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
5160 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
249 ms |
5168 KB |
Output is correct |
8 |
Correct |
487 ms |
5164 KB |
Output is correct |
9 |
Correct |
124 ms |
4956 KB |
Output is correct |
10 |
Correct |
482 ms |
5208 KB |
Output is correct |
11 |
Correct |
240 ms |
5164 KB |
Output is correct |
12 |
Correct |
2 ms |
5160 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Incorrect |
130 ms |
15852 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
132 ms |
15800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
242 ms |
4956 KB |
Output is correct |
2 |
Correct |
275 ms |
5168 KB |
Output is correct |
3 |
Correct |
244 ms |
4952 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
5160 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
249 ms |
5168 KB |
Output is correct |
8 |
Correct |
487 ms |
5164 KB |
Output is correct |
9 |
Correct |
124 ms |
4956 KB |
Output is correct |
10 |
Correct |
482 ms |
5208 KB |
Output is correct |
11 |
Correct |
240 ms |
5164 KB |
Output is correct |
12 |
Correct |
2 ms |
5160 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4952 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Incorrect |
130 ms |
15852 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |