#include <bits/stdc++.h>
using namespace std;
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int n, c = 0, sol = 0;
cin >> n;
if(n&1){
cout << "-1\n";
exit(0);
}
vector<int> love(n), deg(n), s, active(n, 1);
map<string,int> dns;
set<int> single;
for(int i = 0; i < n; i++){
single.insert(i);
string a, b;
cin >> a >> b;
if(!dns.count(a)) dns[a] = c++;
if(!dns.count(b)) dns[b] = c++;
love[dns[a]] = dns[b];
deg[dns[b]]++;
}
for(int i = 0; i < n; i++) if(deg[i] == 0) s.push_back(i);
int i = 0;
for(int j = 0; j < n; j++){
if(active[j] && love[love[j]] == j && love[j] != j){
active[j] = 0, active[love[j]] = 0;
single.erase(j), single.erase(love[j]);
i += 2;
}
}
for(; i < n; i++){
if(s.empty()) s.push_back(*single.begin());
int u = s.back();
s.pop_back();
active[u] = 0;
single.erase(u);
sol++;
if(active[love[u]]){
i++;
active[love[u]] = 0;
single.erase(love[u]);
if(active[love[love[u]]]) s.push_back(love[love[u]]);
if(love[love[u]] == u) sol--, cout << "WHAT\n";
}
}
cout << sol << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
190 ms |
14184 KB |
Output is correct |
5 |
Correct |
192 ms |
14016 KB |
Output is correct |
6 |
Correct |
186 ms |
13936 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
178 ms |
14148 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |