#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
map<string, int> id;
string a[N], b[N];
vector<int> adj[N];
int prv[N], ans = 0, vis[N];
void dfs(int v) {
vis[v] = 1;
for (int u : adj[v]) {
if (!vis[u]) {
prv[u] = v;
dfs(u);
} else if (vis[u] == 1) {
// cout << v << " -> " << u << '\n';
// cout << "ciclo: " << v << ' ';
int sz = 1, cur = v;
while (cur != u) {
cur = prv[cur];
sz++;
// cout << cur << ' ';
}
// cout << '\n';
ans += (sz == 2 ? 0 : (sz + 1) / 2);
}
}
vis[v] = 2;
}
int main() {
int n; cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
id[a[i]], id[b[i]];
}
if (n & 1) {
cout << -1 << '\n';
return 0;
}
int cnt = 0;
for (auto it = id.begin(); it != id.end(); it++) {
it->second = ++cnt;
}
for (int i = 0; i < n; i++) {
adj[id[a[i]]].push_back(id[b[i]]);
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
dfs(i);
}
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
8940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
8940 KB |
Output is correct |
2 |
Correct |
6 ms |
8940 KB |
Output is correct |
3 |
Correct |
6 ms |
8940 KB |
Output is correct |
4 |
Correct |
378 ms |
28268 KB |
Output is correct |
5 |
Correct |
373 ms |
22892 KB |
Output is correct |
6 |
Correct |
370 ms |
29164 KB |
Output is correct |
7 |
Correct |
211 ms |
18924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
396 ms |
20716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
8940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |