#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pii array<int, 2>
const int N = 1e5 + 4;
int m, out[N];
bool vis[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
vector<string> all;
vector<array<string, 2>> edges;
cin >> m;
for (int i = 0; i < m; ++i) {
string v, u;
cin >> v >> u;
edges.pb({v, u});
all.pb(v);
all.pb(u);
}
unordered_map<string, int> match;
int last = 1;
for (string s : all) {
if (match[s] == 0) {
match[s] = last++;
}
}
for (auto [v, u] : edges) {
out[match[v]] = match[u];
}
int ans = 0, odd = 0;
for (int i = 1; i < last; ++i) {
if (vis[i] == false) {
int cur = i, cnt = 0;
while (vis[cur] == false) {
vis[cur] = true;
cur = out[cur];
++cnt;
}
if (cnt != 2) {
ans += cnt / 2;
odd += cnt % 2;
}
}
}
if (odd % 2 == 1) {
cout << -1;
}
else {
cout << ans + odd / 2;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
64 ms |
23220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |