#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]) {
comp = 0;
while(!visited[i]){
visited[i]=1;
comp++;
i =love[i];
}
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 |
7256 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 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 |
7316 KB |
Output is correct |
8 |
Correct |
2 ms |
7260 KB |
Output is correct |
9 |
Correct |
2 ms |
7260 KB |
Output is correct |
10 |
Correct |
2 ms |
7316 KB |
Output is correct |
11 |
Correct |
2 ms |
7260 KB |
Output is correct |
12 |
Correct |
2 ms |
7096 KB |
Output is correct |
13 |
Correct |
2 ms |
7260 KB |
Output is correct |
14 |
Correct |
2 ms |
7260 KB |
Output is correct |
15 |
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 |
7260 KB |
Output is correct |
3 |
Correct |
2 ms |
7260 KB |
Output is correct |
4 |
Correct |
239 ms |
28108 KB |
Output is correct |
5 |
Correct |
250 ms |
28168 KB |
Output is correct |
6 |
Correct |
241 ms |
28256 KB |
Output is correct |
7 |
Correct |
1 ms |
5212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
333 ms |
28760 KB |
Output is correct |
2 |
Incorrect |
289 ms |
28396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
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 |
7316 KB |
Output is correct |
8 |
Correct |
2 ms |
7260 KB |
Output is correct |
9 |
Correct |
2 ms |
7260 KB |
Output is correct |
10 |
Correct |
2 ms |
7316 KB |
Output is correct |
11 |
Correct |
2 ms |
7260 KB |
Output is correct |
12 |
Correct |
2 ms |
7096 KB |
Output is correct |
13 |
Correct |
2 ms |
7260 KB |
Output is correct |
14 |
Correct |
2 ms |
7260 KB |
Output is correct |
15 |
Correct |
2 ms |
7260 KB |
Output is correct |
16 |
Correct |
2 ms |
7260 KB |
Output is correct |
17 |
Correct |
2 ms |
7260 KB |
Output is correct |
18 |
Correct |
2 ms |
7260 KB |
Output is correct |
19 |
Correct |
239 ms |
28108 KB |
Output is correct |
20 |
Correct |
250 ms |
28168 KB |
Output is correct |
21 |
Correct |
241 ms |
28256 KB |
Output is correct |
22 |
Correct |
1 ms |
5212 KB |
Output is correct |
23 |
Correct |
333 ms |
28760 KB |
Output is correct |
24 |
Incorrect |
289 ms |
28396 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |