#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define f first
#define s second
#define pi pair<int,int>
const int MAX = 2e5;
int n;
map<string,int>m;
vector<int>g[MAX];
int love[MAX];
int indegree[MAX];
int visited[MAX];
map<int,string>name;
int main(){
int n;
cin>> n;
string from, to;
int k = 1;
if(n%2){
cout << -1 << endl;
return 0;
}
int a,b;
for(int i = 0; i < n; i++){
cin >> from >> to;
if(m[from]==0) {
// cout << from << " " << k << endl;
name[k]=from;
m[from]=k++;
}
if(m[to]==0){
// cout << to << " " << k << endl;
name[k]=to;
m[to]=k++;
}
a = m[from], b= m[to];
love[a]=b;
if(love[b]==a && a!=b){
visited[a]= visited[b]=1;
}
else{
g[a].pb(b);
indegree[b]++;
}
}
priority_queue<pi,vector<pi>, greater<pi>>q;
for(int i = 1; i <= n; i++){
if(indegree[i]==0 && visited[i]==0){
q.push({0,i});
}
}
ll ans = 0;
while(!q.empty()){
int from = q.top().s;
q.pop();
//cout << name[from] << " " << ans<< endl;
for(int to : g[from]){
indegree[to]--;
if(!visited[to]&&!visited[from]){
if(visited[from])continue;
q.push({indegree[to],to});
ans++;
visited[from]= visited[to] =1;
}
else if(indegree[to]==0 && !visited[to]){
q.push({0,to});
}
}
}
// ans /=2;
ll comp= 0;
for(int i = 1; i <= n; i++){
if(!visited[i] &&!visited[love[i]]&& love[i]!=i && love[i]!=0) {
comp = 0;
while(!visited[i]){
visited[i]=1;
i =love[i];
comp++;
}
ans += (comp+1)/2;
}
}
for(int i = 1; i <= n; i++){
if(!visited[i]) {
ans++;
}
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Correct |
2 ms |
7084 KB |
Output is correct |
3 |
Correct |
2 ms |
7260 KB |
Output is correct |
4 |
Correct |
2 ms |
7260 KB |
Output is correct |
5 |
Correct |
2 ms |
5212 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
2 ms |
7260 KB |
Output is correct |
8 |
Correct |
1 ms |
7260 KB |
Output is correct |
9 |
Correct |
2 ms |
7512 KB |
Output is correct |
10 |
Correct |
2 ms |
7260 KB |
Output is correct |
11 |
Correct |
2 ms |
7260 KB |
Output is correct |
12 |
Correct |
2 ms |
7260 KB |
Output is correct |
13 |
Correct |
2 ms |
7260 KB |
Output is correct |
14 |
Correct |
2 ms |
7304 KB |
Output is correct |
15 |
Correct |
2 ms |
7512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7256 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
3 |
Correct |
1 ms |
7260 KB |
Output is correct |
4 |
Correct |
239 ms |
26240 KB |
Output is correct |
5 |
Correct |
238 ms |
26196 KB |
Output is correct |
6 |
Correct |
230 ms |
26192 KB |
Output is correct |
7 |
Correct |
1 ms |
5208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
251 ms |
26828 KB |
Output is correct |
2 |
Correct |
241 ms |
26452 KB |
Output is correct |
3 |
Correct |
193 ms |
25832 KB |
Output is correct |
4 |
Correct |
1 ms |
5212 KB |
Output is correct |
5 |
Correct |
259 ms |
26080 KB |
Output is correct |
6 |
Correct |
242 ms |
26624 KB |
Output is correct |
7 |
Correct |
245 ms |
26780 KB |
Output is correct |
8 |
Correct |
264 ms |
26668 KB |
Output is correct |
9 |
Correct |
259 ms |
27036 KB |
Output is correct |
10 |
Correct |
228 ms |
27080 KB |
Output is correct |
11 |
Correct |
2 ms |
7260 KB |
Output is correct |
12 |
Correct |
1 ms |
7260 KB |
Output is correct |
13 |
Correct |
2 ms |
7312 KB |
Output is correct |
14 |
Correct |
2 ms |
7260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Correct |
2 ms |
7084 KB |
Output is correct |
3 |
Correct |
2 ms |
7260 KB |
Output is correct |
4 |
Correct |
2 ms |
7260 KB |
Output is correct |
5 |
Correct |
2 ms |
5212 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
2 ms |
7260 KB |
Output is correct |
8 |
Correct |
1 ms |
7260 KB |
Output is correct |
9 |
Correct |
2 ms |
7512 KB |
Output is correct |
10 |
Correct |
2 ms |
7260 KB |
Output is correct |
11 |
Correct |
2 ms |
7260 KB |
Output is correct |
12 |
Correct |
2 ms |
7260 KB |
Output is correct |
13 |
Correct |
2 ms |
7260 KB |
Output is correct |
14 |
Correct |
2 ms |
7304 KB |
Output is correct |
15 |
Correct |
2 ms |
7512 KB |
Output is correct |
16 |
Correct |
2 ms |
7256 KB |
Output is correct |
17 |
Correct |
2 ms |
7260 KB |
Output is correct |
18 |
Correct |
1 ms |
7260 KB |
Output is correct |
19 |
Correct |
239 ms |
26240 KB |
Output is correct |
20 |
Correct |
238 ms |
26196 KB |
Output is correct |
21 |
Correct |
230 ms |
26192 KB |
Output is correct |
22 |
Correct |
1 ms |
5208 KB |
Output is correct |
23 |
Correct |
251 ms |
26828 KB |
Output is correct |
24 |
Correct |
241 ms |
26452 KB |
Output is correct |
25 |
Correct |
193 ms |
25832 KB |
Output is correct |
26 |
Correct |
1 ms |
5212 KB |
Output is correct |
27 |
Correct |
259 ms |
26080 KB |
Output is correct |
28 |
Correct |
242 ms |
26624 KB |
Output is correct |
29 |
Correct |
245 ms |
26780 KB |
Output is correct |
30 |
Correct |
264 ms |
26668 KB |
Output is correct |
31 |
Correct |
259 ms |
27036 KB |
Output is correct |
32 |
Correct |
228 ms |
27080 KB |
Output is correct |
33 |
Correct |
2 ms |
7260 KB |
Output is correct |
34 |
Correct |
1 ms |
7260 KB |
Output is correct |
35 |
Correct |
2 ms |
7312 KB |
Output is correct |
36 |
Correct |
2 ms |
7260 KB |
Output is correct |
37 |
Correct |
276 ms |
28560 KB |
Output is correct |
38 |
Correct |
247 ms |
28720 KB |
Output is correct |
39 |
Correct |
252 ms |
28884 KB |
Output is correct |
40 |
Correct |
280 ms |
28876 KB |
Output is correct |
41 |
Correct |
268 ms |
28988 KB |
Output is correct |
42 |
Correct |
249 ms |
28876 KB |
Output is correct |
43 |
Correct |
262 ms |
28836 KB |
Output is correct |
44 |
Correct |
274 ms |
29032 KB |
Output is correct |
45 |
Correct |
263 ms |
28836 KB |
Output is correct |
46 |
Correct |
289 ms |
28844 KB |
Output is correct |
47 |
Correct |
284 ms |
29180 KB |
Output is correct |
48 |
Correct |
299 ms |
28960 KB |
Output is correct |
49 |
Correct |
251 ms |
28644 KB |
Output is correct |
50 |
Correct |
195 ms |
28240 KB |
Output is correct |
51 |
Correct |
2 ms |
5208 KB |
Output is correct |
52 |
Correct |
266 ms |
28228 KB |
Output is correct |
53 |
Correct |
269 ms |
29056 KB |
Output is correct |
54 |
Correct |
241 ms |
28752 KB |
Output is correct |
55 |
Correct |
248 ms |
28732 KB |
Output is correct |
56 |
Correct |
232 ms |
29380 KB |
Output is correct |
57 |
Correct |
197 ms |
29388 KB |
Output is correct |
58 |
Correct |
2 ms |
7256 KB |
Output is correct |
59 |
Correct |
2 ms |
7260 KB |
Output is correct |
60 |
Correct |
2 ms |
7312 KB |
Output is correct |
61 |
Correct |
2 ms |
7256 KB |
Output is correct |
62 |
Correct |
2 ms |
7316 KB |
Output is correct |
63 |
Correct |
2 ms |
7312 KB |
Output is correct |
64 |
Correct |
2 ms |
7260 KB |
Output is correct |
65 |
Correct |
294 ms |
28260 KB |
Output is correct |
66 |
Correct |
279 ms |
28440 KB |
Output is correct |
67 |
Correct |
242 ms |
28288 KB |
Output is correct |
68 |
Correct |
1 ms |
5212 KB |
Output is correct |
69 |
Correct |
3 ms |
7260 KB |
Output is correct |
70 |
Correct |
2 ms |
7260 KB |
Output is correct |
71 |
Correct |
2 ms |
7320 KB |
Output is correct |
72 |
Correct |
2 ms |
7260 KB |
Output is correct |
73 |
Correct |
1 ms |
5212 KB |
Output is correct |
74 |
Correct |
2 ms |
5212 KB |
Output is correct |
75 |
Correct |
2 ms |
7256 KB |
Output is correct |
76 |
Correct |
2 ms |
7512 KB |
Output is correct |
77 |
Correct |
2 ms |
7256 KB |
Output is correct |
78 |
Correct |
1 ms |
7260 KB |
Output is correct |
79 |
Correct |
3 ms |
7260 KB |
Output is correct |
80 |
Correct |
2 ms |
7096 KB |
Output is correct |
81 |
Correct |
2 ms |
7260 KB |
Output is correct |
82 |
Correct |
2 ms |
7260 KB |
Output is correct |
83 |
Correct |
2 ms |
7256 KB |
Output is correct |