#include <bits/stdc++.h>
using namespace std;
#define MAX 200007
#define int long long
main(){
int n; cin >> n;
if (n % 2 == 1) {cout << -1; return 0;}
map<string, string> init;
map<string, int> indexed;
int adj[n];
for (int x = 0; x < n; x++){
string a, b; cin >> a >> b;
init[a] = b;
}
string name[n]; int ptr = 0;
for (auto y : init){
name[ptr] = y.first;
indexed[y.first] = ptr;
ptr++;
}
int indeg[n]; memset(indeg, 0, sizeof(indeg));
for (auto y : init){
adj[indexed[y.first]] = indexed[y.second];
if (y.first != y.second) indeg[indexed[y.second]]++;
}
bitset<MAX> taken;
queue<int> process;
for (int x = 0; x < n; x++){
if (indeg[x] == 0) process.push(x);
if (x != adj[x] && adj[adj[x]] == x) taken[x] = 1, taken[adj[x]] = 1;
}
int ans = 0;
while (!process.empty()){
int x = process.front();
process.pop();
if (taken[x]) continue;
if (taken[adj[x]]){
adj[x] = x;
//indeg decrease, but doesn't matter since already taken
continue;
}
ans++;
indeg[adj[adj[x]]]--;
if (indeg[adj[adj[x]]] == 0) process.push(adj[adj[x]]);
adj[adj[x]] = x;
taken[x] = 1;
taken[adj[x]] = 1;
}
for (int x = 0; x < n; x++){
if (!taken[x]) ans++;
}
cout << ans;
}
Compilation message
polygon.cpp:5:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
5 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
516 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
356 KB |
Output is correct |
7 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
500 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
313 ms |
26280 KB |
Output is correct |
2 |
Correct |
302 ms |
26164 KB |
Output is correct |
3 |
Correct |
230 ms |
26844 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
251 ms |
26064 KB |
Output is correct |
6 |
Correct |
303 ms |
26324 KB |
Output is correct |
7 |
Correct |
291 ms |
26320 KB |
Output is correct |
8 |
Correct |
279 ms |
26476 KB |
Output is correct |
9 |
Correct |
244 ms |
26656 KB |
Output is correct |
10 |
Correct |
221 ms |
26708 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
516 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
356 KB |
Output is correct |
7 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |