#include <bits/stdc++.h>
using namespace std;
#define MAXN 100005
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define f first
#define s second
int n, ec[MAXN];
string st[MAXN], nx[MAXN];
map<string, int> idx;
set<int> adj[MAXN];
set<pair<int, int>> e;
bool d[MAXN];
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
FOR(i, 1, n) ec[i] = 0;
FOR(i, 1, n){
cin >> st[i];
idx.insert({st[i], i});
cin >> nx[i];
}
if(n % 2) {
cout << -1 << endl;
return 0;
}
int r = 0;
FOR(i, 1, n){
d[i] = (idx[nx[idx[nx[i]]]] == i && idx[nx[i]] != i);
}
FOR(i, 1, n){
if(i == idx[nx[i]] || d[i] || d[idx[nx[i]]]) continue;
e.insert({i, idx[nx[i]]});
ec[i]++, ec[idx[nx[i]]]++;
}
FOR(i, 1, n){
if(i == idx[nx[i]] || d[i] || d[idx[nx[i]]]) continue;
adj[i].insert(idx[nx[i]]);
//cout << "DCVVB: " << idx[nx[i]] << endl;
adj[idx[nx[i]]].insert(i);
}
set<pair<int, int>> se;
FOR(i, 1, n) if(!d[i]) se.insert({ec[i], i});
if(se.size() > n) {
return 0;
}
while(se.size()){
auto si = *se.begin();
if(si.f == 0){
r++;
//cout << st[si.s] << " FALLALEUJAH! " << endl;
}else{
int j = 0, jmn = INT_MAX;
for(int i : adj[si.s]){
if(jmn > ec[i]) j = i, jmn = ec[i];
}
r++;
//cout << st[si.s] << " ~ " << st[j] << " : " << si.s << " " << j << endl;
se.erase({jmn, j});
for(int j2 : adj[j]){
se.erase({ec[j2], j2});
adj[j2].erase(j), ec[j2]--;
se.insert({ec[j2], j2});
}
}
for(int i2 : adj[si.s]) {
se.erase({ec[i2], i2});
adj[i2].erase(si.s), ec[i2]--;
se.insert({ec[i2], i2});
}
se.erase(se.begin());
}
cout << r << "\n";
}
Compilation message
polygon.cpp: In function 'int main()':
polygon.cpp:53:18: warning: comparison of integer expressions of different signedness: 'std::set<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
53 | if(se.size() > n) {
| ~~~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11352 KB |
Output is correct |
2 |
Correct |
2 ms |
11356 KB |
Output is correct |
3 |
Correct |
2 ms |
11356 KB |
Output is correct |
4 |
Correct |
2 ms |
11356 KB |
Output is correct |
5 |
Correct |
2 ms |
11356 KB |
Output is correct |
6 |
Correct |
2 ms |
11356 KB |
Output is correct |
7 |
Correct |
2 ms |
11356 KB |
Output is correct |
8 |
Correct |
2 ms |
11428 KB |
Output is correct |
9 |
Correct |
2 ms |
11356 KB |
Output is correct |
10 |
Correct |
2 ms |
11356 KB |
Output is correct |
11 |
Correct |
2 ms |
11356 KB |
Output is correct |
12 |
Correct |
2 ms |
11356 KB |
Output is correct |
13 |
Correct |
2 ms |
11356 KB |
Output is correct |
14 |
Correct |
2 ms |
11356 KB |
Output is correct |
15 |
Correct |
2 ms |
11356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11356 KB |
Output is correct |
2 |
Correct |
2 ms |
11356 KB |
Output is correct |
3 |
Correct |
2 ms |
11356 KB |
Output is correct |
4 |
Correct |
376 ms |
38324 KB |
Output is correct |
5 |
Execution timed out |
2071 ms |
37204 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
396 ms |
38264 KB |
Output is correct |
2 |
Correct |
352 ms |
38480 KB |
Output is correct |
3 |
Correct |
168 ms |
24404 KB |
Output is correct |
4 |
Correct |
48 ms |
19572 KB |
Output is correct |
5 |
Correct |
381 ms |
38480 KB |
Output is correct |
6 |
Correct |
397 ms |
38480 KB |
Output is correct |
7 |
Correct |
359 ms |
38148 KB |
Output is correct |
8 |
Correct |
280 ms |
31448 KB |
Output is correct |
9 |
Correct |
331 ms |
38500 KB |
Output is correct |
10 |
Correct |
196 ms |
38416 KB |
Output is correct |
11 |
Correct |
2 ms |
11356 KB |
Output is correct |
12 |
Correct |
2 ms |
11356 KB |
Output is correct |
13 |
Correct |
2 ms |
11356 KB |
Output is correct |
14 |
Correct |
2 ms |
11392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11352 KB |
Output is correct |
2 |
Correct |
2 ms |
11356 KB |
Output is correct |
3 |
Correct |
2 ms |
11356 KB |
Output is correct |
4 |
Correct |
2 ms |
11356 KB |
Output is correct |
5 |
Correct |
2 ms |
11356 KB |
Output is correct |
6 |
Correct |
2 ms |
11356 KB |
Output is correct |
7 |
Correct |
2 ms |
11356 KB |
Output is correct |
8 |
Correct |
2 ms |
11428 KB |
Output is correct |
9 |
Correct |
2 ms |
11356 KB |
Output is correct |
10 |
Correct |
2 ms |
11356 KB |
Output is correct |
11 |
Correct |
2 ms |
11356 KB |
Output is correct |
12 |
Correct |
2 ms |
11356 KB |
Output is correct |
13 |
Correct |
2 ms |
11356 KB |
Output is correct |
14 |
Correct |
2 ms |
11356 KB |
Output is correct |
15 |
Correct |
2 ms |
11356 KB |
Output is correct |
16 |
Correct |
2 ms |
11356 KB |
Output is correct |
17 |
Correct |
2 ms |
11356 KB |
Output is correct |
18 |
Correct |
2 ms |
11356 KB |
Output is correct |
19 |
Correct |
376 ms |
38324 KB |
Output is correct |
20 |
Execution timed out |
2071 ms |
37204 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |