#include <bits/stdc++.h>
using namespace std;
#define rep(n) for (int i = 0; i < n; ++i)
#define rep2(n) for (int j = 0; j < n; ++j)
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
const int maxn = 2e5;
int loves[maxn];
bool been[maxn] = {};
int dfs (int i) {
been[i] = 1;
return 1+(been[loves[i]]?0:(loves[i]));
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
map<string, int> decode;
int n, num = 1, loved[maxn], ans = 0;
string s, t;
cin >> n;
if (n%2==1) {
cout << "-1\n";
return 0;
}
rep (n) {
cin >> s >> t;
if (!decode[s]) decode[s] = num++;
if (!decode[t]) decode[t] = num++;
loves[decode[s]] = decode[t];
loved[decode[t]]++;
}
queue<int> q;
for (int i = 1; i <= n; ++i) {
if (i==loves[loves[i]] && loves[i]!=i) {
been[i] = 1;
been[loves[i]] = 1;
}
else if (loved[i]==0) q.push(i);
}
while (!q.empty()) {
int id = q.front();
q.pop();
been[id] = 1;
if (!been[loves[id]]) {
been[loves[id]] = 1;
if (--loved[loves[loves[id]]]==0) q.push(loves[loves[id]]);
}
ans++;
}
for (int i = 1; i <= n; ++i) {
if (!been[i]) ans += (dfs(i)+1)/2;
}
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
288 ms |
11436 KB |
Output is correct |
2 |
Correct |
287 ms |
11384 KB |
Output is correct |
3 |
Correct |
228 ms |
11256 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
273 ms |
11384 KB |
Output is correct |
6 |
Correct |
259 ms |
11384 KB |
Output is correct |
7 |
Correct |
265 ms |
11516 KB |
Output is correct |
8 |
Correct |
250 ms |
11468 KB |
Output is correct |
9 |
Correct |
252 ms |
11572 KB |
Output is correct |
10 |
Correct |
199 ms |
11384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |