#define here cerr<<"===========================================\n"
#include "bits/stdc++.h"
#define ld double
#define ll long long
#define llinf 100000000000000000LL // 10^17
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) (ll)(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define daj_mi_malo_vremena ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
using namespace std;
#define maxn 100005
ll n;
vector<ll> g[maxn];
map<string,ll> mp;
ll it = 1;
ll f[maxn];
ll in[maxn];
bool vis[maxn];
vector<pll> edg;
int main(){
daj_mi_malo_vremena
cin >> n;
for(ll i = 1;i<=n;i++){
string x1,y1;
cin >> x1 >> y1;
if(mp[x1]==0) mp[x1] = it++;
if(mp[y1]==0) mp[y1] = it++;
ll x = mp[x1];
ll y = mp[y1];
in[y]++;
f[x] = y;
edg.pb({x,y});
}
if(n&1){
cout<<-1<<endl;
return 0;
}
queue<ll> q;
ll ans = 0;
for(ll i = 1;i<=n;i++){
if(in[i]==0) q.push(i);
if(i==f[f[i]]&&f[i]!=i) vis[i] = 1;
}
ll sami = 0;
while(sz(q)){
ll x = q.front();
q.pop();
ll y = f[x];
if(!vis[y]&&!vis[x]&&x!=y){
ans++;
vis[x] = vis[y] = 1;
in[f[y]]--;
if(in[f[y]]==0) q.push(f[y]);
}else{
in[y]--;
if(in[y]==0) q.push(y);
}
}
for(ll i = 1;i<=n;i++){
if(vis[i]) continue;
ll x = i;
while(vis[x]==0){
ll y = f[x];
if(vis[y]||(y==x)){
break;
}
vis[x] = vis[y] = 1;
ans++;
x = f[y];
}
}
for(ll i = 1;i<=n;i++) if(!vis[i]) sami++;
ans+=sami;
cout<<ans<<endl;
return 0;
}
/*
8
leonard emmy
ada emmy
isaac leonard
emmy pierre
pierre bernhard
bernhard emmy
sofia karl
karl sofia
4
a c
b c
c d
d d
3
rocky scarlet
scarlet patrick
patrick rocky
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
1 ms |
2676 KB |
Output is correct |
11 |
Correct |
1 ms |
2688 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2680 KB |
Output is correct |
14 |
Correct |
1 ms |
2684 KB |
Output is correct |
15 |
Correct |
2 ms |
2684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
169 ms |
13604 KB |
Output is correct |
5 |
Correct |
174 ms |
15844 KB |
Output is correct |
6 |
Correct |
174 ms |
15772 KB |
Output is correct |
7 |
Correct |
175 ms |
15704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
171 ms |
13812 KB |
Output is correct |
2 |
Correct |
179 ms |
13776 KB |
Output is correct |
3 |
Correct |
141 ms |
13520 KB |
Output is correct |
4 |
Correct |
164 ms |
13592 KB |
Output is correct |
5 |
Correct |
167 ms |
13644 KB |
Output is correct |
6 |
Correct |
175 ms |
14060 KB |
Output is correct |
7 |
Correct |
167 ms |
14084 KB |
Output is correct |
8 |
Correct |
159 ms |
13968 KB |
Output is correct |
9 |
Correct |
153 ms |
14220 KB |
Output is correct |
10 |
Correct |
149 ms |
13624 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
1 ms |
2676 KB |
Output is correct |
11 |
Correct |
1 ms |
2688 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2680 KB |
Output is correct |
14 |
Correct |
1 ms |
2684 KB |
Output is correct |
15 |
Correct |
2 ms |
2684 KB |
Output is correct |
16 |
Correct |
1 ms |
2644 KB |
Output is correct |
17 |
Correct |
1 ms |
2644 KB |
Output is correct |
18 |
Correct |
1 ms |
2644 KB |
Output is correct |
19 |
Correct |
169 ms |
13604 KB |
Output is correct |
20 |
Correct |
174 ms |
15844 KB |
Output is correct |
21 |
Correct |
174 ms |
15772 KB |
Output is correct |
22 |
Correct |
175 ms |
15704 KB |
Output is correct |
23 |
Correct |
171 ms |
13812 KB |
Output is correct |
24 |
Correct |
179 ms |
13776 KB |
Output is correct |
25 |
Correct |
141 ms |
13520 KB |
Output is correct |
26 |
Correct |
164 ms |
13592 KB |
Output is correct |
27 |
Correct |
167 ms |
13644 KB |
Output is correct |
28 |
Correct |
175 ms |
14060 KB |
Output is correct |
29 |
Correct |
167 ms |
14084 KB |
Output is correct |
30 |
Correct |
159 ms |
13968 KB |
Output is correct |
31 |
Correct |
153 ms |
14220 KB |
Output is correct |
32 |
Correct |
149 ms |
13624 KB |
Output is correct |
33 |
Correct |
1 ms |
2644 KB |
Output is correct |
34 |
Correct |
2 ms |
2644 KB |
Output is correct |
35 |
Correct |
2 ms |
2644 KB |
Output is correct |
36 |
Correct |
2 ms |
2644 KB |
Output is correct |
37 |
Correct |
178 ms |
15940 KB |
Output is correct |
38 |
Correct |
173 ms |
16236 KB |
Output is correct |
39 |
Correct |
178 ms |
16212 KB |
Output is correct |
40 |
Correct |
171 ms |
16272 KB |
Output is correct |
41 |
Correct |
168 ms |
16188 KB |
Output is correct |
42 |
Correct |
168 ms |
16072 KB |
Output is correct |
43 |
Correct |
167 ms |
16148 KB |
Output is correct |
44 |
Correct |
184 ms |
16104 KB |
Output is correct |
45 |
Correct |
178 ms |
16144 KB |
Output is correct |
46 |
Correct |
191 ms |
16192 KB |
Output is correct |
47 |
Correct |
167 ms |
16472 KB |
Output is correct |
48 |
Correct |
167 ms |
16052 KB |
Output is correct |
49 |
Correct |
169 ms |
15904 KB |
Output is correct |
50 |
Correct |
143 ms |
15676 KB |
Output is correct |
51 |
Correct |
171 ms |
15776 KB |
Output is correct |
52 |
Correct |
169 ms |
15868 KB |
Output is correct |
53 |
Correct |
163 ms |
16184 KB |
Output is correct |
54 |
Correct |
172 ms |
16236 KB |
Output is correct |
55 |
Correct |
159 ms |
16184 KB |
Output is correct |
56 |
Correct |
158 ms |
16368 KB |
Output is correct |
57 |
Correct |
129 ms |
15708 KB |
Output is correct |
58 |
Correct |
2 ms |
2644 KB |
Output is correct |
59 |
Correct |
1 ms |
2644 KB |
Output is correct |
60 |
Correct |
2 ms |
2644 KB |
Output is correct |
61 |
Correct |
2 ms |
2644 KB |
Output is correct |
62 |
Correct |
2 ms |
2680 KB |
Output is correct |
63 |
Correct |
2 ms |
2644 KB |
Output is correct |
64 |
Correct |
2 ms |
2648 KB |
Output is correct |
65 |
Correct |
172 ms |
15892 KB |
Output is correct |
66 |
Correct |
167 ms |
15784 KB |
Output is correct |
67 |
Correct |
189 ms |
15856 KB |
Output is correct |
68 |
Correct |
180 ms |
15724 KB |
Output is correct |
69 |
Correct |
2 ms |
2644 KB |
Output is correct |
70 |
Correct |
2 ms |
2680 KB |
Output is correct |
71 |
Correct |
2 ms |
2684 KB |
Output is correct |
72 |
Correct |
2 ms |
2644 KB |
Output is correct |
73 |
Correct |
2 ms |
2644 KB |
Output is correct |
74 |
Correct |
2 ms |
2684 KB |
Output is correct |
75 |
Correct |
2 ms |
2644 KB |
Output is correct |
76 |
Correct |
2 ms |
2644 KB |
Output is correct |
77 |
Correct |
2 ms |
2644 KB |
Output is correct |
78 |
Correct |
2 ms |
2644 KB |
Output is correct |
79 |
Correct |
2 ms |
2644 KB |
Output is correct |
80 |
Correct |
1 ms |
2676 KB |
Output is correct |
81 |
Correct |
2 ms |
2680 KB |
Output is correct |
82 |
Correct |
2 ms |
2644 KB |
Output is correct |
83 |
Correct |
2 ms |
2644 KB |
Output is correct |