Submission #825333

#TimeUsernameProblemLanguageResultExecution timeMemory
825333QwertyPiLove Polygon (BOI18_polygon)C++14
0 / 100
359 ms41472 KiB
#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; for(auto u : G[i]){ if(done[u]) to_e.push_back(u); } for(auto u : to_e){ G[i].erase(u); } } cout << res << endl; min_priority_queue<pair<int, int>> pq; for(int i = 0; i < n; i++){ if(G[i].size() != 0) 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}); } G[u].clear(); G[v].clear(); res++; } int ans = n - res; cout << ans << endl; }

Compilation message (stderr)

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;
      |      ~~~~~~~~~~~~^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...