#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 11;
struct edge{
int u, v;
};
int p[MAXN];
set<int> G[MAXN];
bool done[MAXN];
template<class T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
int32_t main(){
set<string> S; map<string, int> mp;
int n; cin >> n;
if(n % 2 == 1){
cout << -1 << endl;
return 0;
}
vector<pair<string, string>> ES;
vector<edge> E;
for(int i = 0; i < n; i++){
string s, t; cin >> s >> t; S.insert(s); S.insert(t);
ES.push_back({s, t});
}
int id = 0; for(auto i : S) mp[i] = id++;
for(int i = 0; i < n; i++){
E.push_back({mp[ES[i].first], mp[ES[i].second]});
}
for(int i = 0; i < n; i++) p[E[i].u] = E[i].v;
int res = 0;
for(int i = 0; i < n; i++){
if(p[i] != i && p[p[i]] == i) done[i] = true, res++;
else if(p[i] != i){
G[p[i]].insert(i); G[i].insert(p[i]);
}
}
for(int i = 0; i < n; i++){
vector<int> to_e;
if(done[i]) G[i].clear();
for(auto u : G[i]){
if(done[u]) to_e.push_back(u);
}
for(auto u : to_e){
G[i].erase(u);
}
}
min_priority_queue<pair<int, int>> pq;
for(int i = 0; i < n; i++){
if(G[i].size() != 0 && !done[i]) pq.push({G[i].size(), i});
}
while(!pq.empty()){
auto [sz, v] = pq.top(); pq.pop();
if(sz == 0) continue;
if(G[v].size() != sz) continue;
auto u = *G[v].begin();
for(auto x : G[v]){
int f, t;
f = G[x].size();
G[x].erase(v);
t = G[x].size();
if(t < f) pq.push({G[x].size(), x});
}
for(auto x : G[u]){
int f, t;
f = G[x].size();
G[x].erase(u);
t = G[x].size();
if(t < f) pq.push({G[x].size(), x});
}
// cout << "T " << u << ' ' << v << endl;
G[u].clear(); G[v].clear();
res++;
}
int ans = n - res;
cout << ans << endl;
}
Compilation message
polygon.cpp: In function 'int32_t main()':
polygon.cpp:55:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
55 | auto [sz, v] = pq.top(); pq.pop();
| ^
polygon.cpp:57:18: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'std::tuple_element<0, std::pair<int, int> >::type' {aka 'int'} [-Wsign-compare]
57 | if(G[v].size() != sz) continue;
| ~~~~~~~~~~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
5004 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5012 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
5012 KB |
Output is correct |
11 |
Correct |
3 ms |
5012 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4988 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4884 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
293 ms |
41368 KB |
Output is correct |
5 |
Correct |
278 ms |
40868 KB |
Output is correct |
6 |
Correct |
294 ms |
41504 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
288 ms |
39264 KB |
Output is correct |
2 |
Correct |
297 ms |
41512 KB |
Output is correct |
3 |
Correct |
193 ms |
31176 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
290 ms |
41456 KB |
Output is correct |
6 |
Correct |
295 ms |
41592 KB |
Output is correct |
7 |
Correct |
277 ms |
41380 KB |
Output is correct |
8 |
Correct |
238 ms |
36572 KB |
Output is correct |
9 |
Correct |
293 ms |
41496 KB |
Output is correct |
10 |
Correct |
222 ms |
43196 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
5004 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5012 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
5012 KB |
Output is correct |
11 |
Correct |
3 ms |
5012 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4988 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4884 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
293 ms |
41368 KB |
Output is correct |
20 |
Correct |
278 ms |
40868 KB |
Output is correct |
21 |
Correct |
294 ms |
41504 KB |
Output is correct |
22 |
Correct |
2 ms |
4948 KB |
Output is correct |
23 |
Correct |
288 ms |
39264 KB |
Output is correct |
24 |
Correct |
297 ms |
41512 KB |
Output is correct |
25 |
Correct |
193 ms |
31176 KB |
Output is correct |
26 |
Correct |
3 ms |
4948 KB |
Output is correct |
27 |
Correct |
290 ms |
41456 KB |
Output is correct |
28 |
Correct |
295 ms |
41592 KB |
Output is correct |
29 |
Correct |
277 ms |
41380 KB |
Output is correct |
30 |
Correct |
238 ms |
36572 KB |
Output is correct |
31 |
Correct |
293 ms |
41496 KB |
Output is correct |
32 |
Correct |
222 ms |
43196 KB |
Output is correct |
33 |
Correct |
2 ms |
4948 KB |
Output is correct |
34 |
Correct |
2 ms |
4948 KB |
Output is correct |
35 |
Correct |
2 ms |
4948 KB |
Output is correct |
36 |
Correct |
2 ms |
4948 KB |
Output is correct |
37 |
Correct |
306 ms |
41552 KB |
Output is correct |
38 |
Correct |
281 ms |
41560 KB |
Output is correct |
39 |
Correct |
295 ms |
41592 KB |
Output is correct |
40 |
Correct |
281 ms |
41576 KB |
Output is correct |
41 |
Correct |
280 ms |
41608 KB |
Output is correct |
42 |
Correct |
293 ms |
41544 KB |
Output is correct |
43 |
Correct |
281 ms |
41592 KB |
Output is correct |
44 |
Correct |
296 ms |
41580 KB |
Output is correct |
45 |
Correct |
291 ms |
41600 KB |
Output is correct |
46 |
Correct |
281 ms |
41612 KB |
Output is correct |
47 |
Correct |
280 ms |
41636 KB |
Output is correct |
48 |
Correct |
291 ms |
41372 KB |
Output is correct |
49 |
Correct |
295 ms |
41556 KB |
Output is correct |
50 |
Correct |
194 ms |
31180 KB |
Output is correct |
51 |
Correct |
2 ms |
4948 KB |
Output is correct |
52 |
Correct |
264 ms |
41456 KB |
Output is correct |
53 |
Correct |
296 ms |
41728 KB |
Output is correct |
54 |
Correct |
282 ms |
41536 KB |
Output is correct |
55 |
Correct |
249 ms |
36652 KB |
Output is correct |
56 |
Correct |
281 ms |
41488 KB |
Output is correct |
57 |
Correct |
219 ms |
43264 KB |
Output is correct |
58 |
Correct |
2 ms |
4948 KB |
Output is correct |
59 |
Correct |
2 ms |
5004 KB |
Output is correct |
60 |
Correct |
2 ms |
4948 KB |
Output is correct |
61 |
Correct |
2 ms |
5008 KB |
Output is correct |
62 |
Correct |
2 ms |
4948 KB |
Output is correct |
63 |
Correct |
3 ms |
4988 KB |
Output is correct |
64 |
Correct |
3 ms |
5012 KB |
Output is correct |
65 |
Correct |
270 ms |
41400 KB |
Output is correct |
66 |
Correct |
271 ms |
40776 KB |
Output is correct |
67 |
Correct |
266 ms |
41404 KB |
Output is correct |
68 |
Correct |
2 ms |
5020 KB |
Output is correct |
69 |
Correct |
2 ms |
4948 KB |
Output is correct |
70 |
Correct |
2 ms |
4948 KB |
Output is correct |
71 |
Correct |
2 ms |
4948 KB |
Output is correct |
72 |
Correct |
2 ms |
4948 KB |
Output is correct |
73 |
Correct |
2 ms |
4948 KB |
Output is correct |
74 |
Correct |
3 ms |
5012 KB |
Output is correct |
75 |
Correct |
2 ms |
5008 KB |
Output is correct |
76 |
Correct |
2 ms |
4948 KB |
Output is correct |
77 |
Correct |
3 ms |
4952 KB |
Output is correct |
78 |
Correct |
2 ms |
5012 KB |
Output is correct |
79 |
Correct |
3 ms |
4948 KB |
Output is correct |
80 |
Correct |
2 ms |
4948 KB |
Output is correct |
81 |
Correct |
2 ms |
4948 KB |
Output is correct |
82 |
Correct |
2 ms |
4948 KB |
Output is correct |
83 |
Correct |
2 ms |
5008 KB |
Output is correct |