#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) {
int sz = 1, cur = v;
while (cur != u) {
cur = prv[cur];
sz++;
}
ans += (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;
}
for (auto it = id.begin(); it != id.end(); it++) {
it->second++;
}
for (int i = 0; i < n; i++) {
adj[id[a[i]]].push_back(id[b[i]]);
adj[id[b[i]]].push_back(id[a[i]]);
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
dfs(i);
}
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
9072 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 |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
382 ms |
20192 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
9072 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |