#include <bits/stdc++.h>
#define N 200005
using namespace std;
vector<int> adj[N];
int ans = 0;
int edge[N];
bool cycle[N];
int vis[N];
bool del[N];
void dfs(int v){
vis[v] = 1;
if(vis[edge[v]] == 1){
cycle[v] = 1;
int tmp = edge[v];
while(tmp != v){
cycle[tmp] = 1;
tmp = edge[tmp];
}
}
else if(!vis[edge[v]]){
dfs(edge[v]);
}
vis[v] = 2;
}
void dfs2(int v){
for(auto u:adj[v]){
if(cycle[u])continue;
dfs2(u);
ans += !del[u];
if(!del[u]){
del[v] = del[u] = 1;
}
}
}
void solve(){
int n;
cin >> n;
int cnt = 1;
map<string,int>val;
for(int i=1;i<=n;i++){
string s,t;
cin >> s >> t;
if(!val[s])val[s] = cnt++;
if(!val[t])val[t] = cnt++;
edge[val[s]] = val[t];
adj[val[t]].push_back(val[s]);
}
if(n%2){
cout << -1 << endl;
return;
}
if(n <= 20){
int ans = n;
for(int mask = 0;mask < (1<<(n+1));mask+=2){
vector<int> go(n+1);
bool ok = 1;
for(int i=1;i<=n;i++){
if(mask & (1<<i)){
if(edge[i] == i)ok = 0;
if(go[i] && edge[i] != go[i])ok = 0;
go[i] = edge[i];
if(go[edge[i]] && go[edge[i]] != i)ok = 0;
go[edge[i]] = i;
}
}
if(ok)
ans = min(ans,n - __builtin_popcount(mask));
}
cout << ans;
return;
}
for(int i=1;i<=n;i++){
if(!vis[i]){
dfs(i);
}
}
/*
for(auto u:val){
cout << u.first << " " << u.second << endl;
}*/
for(int i=1;i<=n;i++){
if(cycle[i]){
dfs2(i);
}
}
/*
for(auto u:val){
cout << u.first << " " << del[u.second] << endl;
}*/
///cout << ans << endl;
for(int i=1;i<=n;i++){
if(del[i])continue;
//cout << i << endl;
vector<int> v;
int tmp = i;
while(v.empty() || tmp != i){
v.push_back(tmp);
if(!del[edge[tmp]])
tmp = edge[tmp];
else break;
}
if(tmp == i){
if(v.size() != 2){
ans += v.size()/2 + v.size()%2;
}
for(auto u:v)del[u] = 1;
continue;
}
int bef = -1;
for(auto u:adj[i]){
if(!del[u])bef = u;
}
while(bef != -1){
v.push_back(bef);
for(auto u:adj[bef]){
if(!del[u]){
bef = u;
break;
}
}
if(bef == v.back())bef = -1;
}
for(auto u:v)del[u] = 1;
ans += v.size()/2 + v.size()%2;
}
cout << ans;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t=1;
//cin>>t;
while(t--){
solve();
}
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
5008 KB |
Output is correct |
2 |
Correct |
101 ms |
5024 KB |
Output is correct |
3 |
Correct |
94 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
89 ms |
5012 KB |
Output is correct |
8 |
Correct |
88 ms |
5020 KB |
Output is correct |
9 |
Correct |
89 ms |
5060 KB |
Output is correct |
10 |
Correct |
89 ms |
4940 KB |
Output is correct |
11 |
Correct |
94 ms |
5016 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5000 KB |
Output is correct |
2 |
Correct |
3 ms |
4928 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
275 ms |
20740 KB |
Output is correct |
5 |
Correct |
257 ms |
17492 KB |
Output is correct |
6 |
Correct |
263 ms |
21156 KB |
Output is correct |
7 |
Correct |
239 ms |
16864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
280 ms |
16728 KB |
Output is correct |
2 |
Correct |
251 ms |
18480 KB |
Output is correct |
3 |
Correct |
204 ms |
17504 KB |
Output is correct |
4 |
Correct |
233 ms |
15480 KB |
Output is correct |
5 |
Correct |
267 ms |
23188 KB |
Output is correct |
6 |
Correct |
240 ms |
15880 KB |
Output is correct |
7 |
Correct |
301 ms |
16028 KB |
Output is correct |
8 |
Correct |
239 ms |
16324 KB |
Output is correct |
9 |
Correct |
234 ms |
15452 KB |
Output is correct |
10 |
Correct |
174 ms |
14660 KB |
Output is correct |
11 |
Correct |
8 ms |
5020 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
7 ms |
5020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
5008 KB |
Output is correct |
2 |
Correct |
101 ms |
5024 KB |
Output is correct |
3 |
Correct |
94 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
89 ms |
5012 KB |
Output is correct |
8 |
Correct |
88 ms |
5020 KB |
Output is correct |
9 |
Correct |
89 ms |
5060 KB |
Output is correct |
10 |
Correct |
89 ms |
4940 KB |
Output is correct |
11 |
Correct |
94 ms |
5016 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
4 ms |
5000 KB |
Output is correct |
17 |
Correct |
3 ms |
4928 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
275 ms |
20740 KB |
Output is correct |
20 |
Correct |
257 ms |
17492 KB |
Output is correct |
21 |
Correct |
263 ms |
21156 KB |
Output is correct |
22 |
Correct |
239 ms |
16864 KB |
Output is correct |
23 |
Correct |
280 ms |
16728 KB |
Output is correct |
24 |
Correct |
251 ms |
18480 KB |
Output is correct |
25 |
Correct |
204 ms |
17504 KB |
Output is correct |
26 |
Correct |
233 ms |
15480 KB |
Output is correct |
27 |
Correct |
267 ms |
23188 KB |
Output is correct |
28 |
Correct |
240 ms |
15880 KB |
Output is correct |
29 |
Correct |
301 ms |
16028 KB |
Output is correct |
30 |
Correct |
239 ms |
16324 KB |
Output is correct |
31 |
Correct |
234 ms |
15452 KB |
Output is correct |
32 |
Correct |
174 ms |
14660 KB |
Output is correct |
33 |
Correct |
8 ms |
5020 KB |
Output is correct |
34 |
Correct |
3 ms |
4940 KB |
Output is correct |
35 |
Correct |
3 ms |
4940 KB |
Output is correct |
36 |
Correct |
7 ms |
5020 KB |
Output is correct |
37 |
Incorrect |
261 ms |
16592 KB |
Output isn't correct |
38 |
Halted |
0 ms |
0 KB |
- |