#include <bits/stdc++.h>
#define pii pair<int, int>
using namespace std;
const int MAX = 100007;
pair<string, string> E[MAX];
int F[MAX], V[MAX], in[MAX], S[MAX];
vector<int> G[MAX];
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int N;
cin >> N;
if (N & 1) {
cout << -1;
return 0;
}
map<string, int> SN;
for (int i = 1; i <= N; ++i) {
string a, b;
cin >> a >> b;
SN[a] = i;
E[i] = {a, b};
}
for (int i = 1; i <= N; ++i) F[SN[E[i].first]] = SN[E[i].second];
int RN = 0, match = 0;
for (int i = 1; i <= N; ++i) {
if (i != F[i] && F[F[i]] == i) V[i] = 1;
else RN++;
}
for (int i = 1; i <= N; ++i) {
if (V[i] || V[F[i]]) continue;
G[i].push_back(F[i]);
in[F[i]]++;
}
queue<int> Q;
for (int i = 1; i <= N; ++i) if (!G[i].empty() && !in[i]) Q.push(i);
while (!Q.empty()) {
int cur = Q.front(); Q.pop();
if (V[F[cur]]) continue;
if (!S[cur] && !S[F[cur]]) {
match++;
S[cur] = S[F[cur]] = 1;
}
if (!--in[F[cur]]) Q.push(F[cur]);
}
for (int i = 1; i <= N; ++i) {
if (!in[i]) continue;
int cur = i;
vector<int> C = {S[cur]};
while (1) {
cur = F[cur];
in[cur]--;
if (cur == i) break;
C.push_back(S[cur]);
}
// cout << "Cycle\n";
// for (int n : C) cout << n << ' ';
// cout << '\n';
bool one = (*max_element(C.begin(), C.end()) == 1);
if (!one) match += C.size() / 2;
else {
int piv = max_element(C.begin(), C.end()) - C.begin();
for (int i = 0; i < C.size(); ++i) {
int a = (piv + i) % C.size(), b = (piv + i + 1) % C.size();
if (!S[C[a]] && !S[C[b]]) {
match++;
S[C[a]] = S[C[b]] = 1;
}
}
}
}
int ans = RN - match;
cout << ans;
return 0;
}
Compilation message
polygon.cpp: In function 'int main()':
polygon.cpp:72:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int i = 0; i < C.size(); ++i) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9052 KB |
Output is correct |
2 |
Correct |
4 ms |
9052 KB |
Output is correct |
3 |
Correct |
4 ms |
9052 KB |
Output is correct |
4 |
Correct |
3 ms |
9052 KB |
Output is correct |
5 |
Correct |
4 ms |
9052 KB |
Output is correct |
6 |
Correct |
4 ms |
8916 KB |
Output is correct |
7 |
Correct |
4 ms |
9052 KB |
Output is correct |
8 |
Incorrect |
4 ms |
9052 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9052 KB |
Output is correct |
2 |
Correct |
4 ms |
9052 KB |
Output is correct |
3 |
Correct |
3 ms |
9052 KB |
Output is correct |
4 |
Correct |
125 ms |
23508 KB |
Output is correct |
5 |
Correct |
130 ms |
23204 KB |
Output is correct |
6 |
Correct |
125 ms |
23604 KB |
Output is correct |
7 |
Correct |
4 ms |
9048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
23376 KB |
Output is correct |
2 |
Correct |
125 ms |
23380 KB |
Output is correct |
3 |
Correct |
105 ms |
22868 KB |
Output is correct |
4 |
Correct |
4 ms |
9052 KB |
Output is correct |
5 |
Correct |
123 ms |
23124 KB |
Output is correct |
6 |
Correct |
135 ms |
23376 KB |
Output is correct |
7 |
Correct |
132 ms |
23504 KB |
Output is correct |
8 |
Correct |
122 ms |
23376 KB |
Output is correct |
9 |
Correct |
116 ms |
23604 KB |
Output is correct |
10 |
Correct |
100 ms |
22868 KB |
Output is correct |
11 |
Correct |
4 ms |
9052 KB |
Output is correct |
12 |
Incorrect |
4 ms |
9052 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9052 KB |
Output is correct |
2 |
Correct |
4 ms |
9052 KB |
Output is correct |
3 |
Correct |
4 ms |
9052 KB |
Output is correct |
4 |
Correct |
3 ms |
9052 KB |
Output is correct |
5 |
Correct |
4 ms |
9052 KB |
Output is correct |
6 |
Correct |
4 ms |
8916 KB |
Output is correct |
7 |
Correct |
4 ms |
9052 KB |
Output is correct |
8 |
Incorrect |
4 ms |
9052 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |