#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;
}
};
namespace sub2{
void solve(){
int ans = 0;
bitset<N> vis;
for(int i = 0;i < n;i++){
if(vis[i])continue;
int u = p[i];
int cnt = 1;
while(u != i){
cnt++;
u = p[u];
}
if(cnt % 2)ans += cnt / 2 + 1;
else if(cnt > 2)ans += cnt / 2;
}
cout<<ans;
}
};
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;
}
sub2::solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
239 ms |
4956 KB |
Output is correct |
2 |
Correct |
259 ms |
5160 KB |
Output is correct |
3 |
Correct |
242 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4952 KB |
Output is correct |
5 |
Correct |
3 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
245 ms |
4956 KB |
Output is correct |
8 |
Correct |
477 ms |
4956 KB |
Output is correct |
9 |
Correct |
121 ms |
4956 KB |
Output is correct |
10 |
Correct |
476 ms |
4956 KB |
Output is correct |
11 |
Correct |
238 ms |
5160 KB |
Output is correct |
12 |
Correct |
2 ms |
4952 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Execution timed out |
2099 ms |
15812 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2041 ms |
15696 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
239 ms |
4956 KB |
Output is correct |
2 |
Correct |
259 ms |
5160 KB |
Output is correct |
3 |
Correct |
242 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4952 KB |
Output is correct |
5 |
Correct |
3 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
245 ms |
4956 KB |
Output is correct |
8 |
Correct |
477 ms |
4956 KB |
Output is correct |
9 |
Correct |
121 ms |
4956 KB |
Output is correct |
10 |
Correct |
476 ms |
4956 KB |
Output is correct |
11 |
Correct |
238 ms |
5160 KB |
Output is correct |
12 |
Correct |
2 ms |
4952 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 |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Execution timed out |
2099 ms |
15812 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |