Submission #1005648

#TimeUsernameProblemLanguageResultExecution timeMemory
1005648LonlyRLove Polygon (BOI18_polygon)C++17
100 / 100
227 ms16848 KiB
#include <bits/stdc++.h> using namespace std; int main(){ cin.tie(0); ios::sync_with_stdio(0); int n, c = 0, sol = 0; cin >> n; if(n&1){ cout << "-1\n"; exit(0); } vector<int> love(n), deg(n), s, active(n, 1); map<string,int> dns; set<int> single; for(int i = 0; i < n; i++){ single.insert(i); string a, b; cin >> a >> b; if(!dns.count(a)) dns[a] = c++; if(!dns.count(b)) dns[b] = c++; love[dns[a]] = dns[b]; deg[dns[b]]++; } for(int i = 0; i < n; i++) if(deg[i] == 0) s.push_back(i); int i = 0; for(int j = 0; j < n; j++){ if(active[j] && love[love[j]] == j && love[j] != j){ active[j] = 0, active[love[j]] = 0; single.erase(j), single.erase(love[j]); i += 2; } } for(; i < n; i++){ if(s.empty()) s.push_back(*single.begin()); int u = s.back(); s.pop_back(); active[u] = 0; single.erase(u); sol++; if(active[love[u]]){ i++; active[love[u]] = 0; single.erase(love[u]); if(active[love[love[u]]] && !--deg[love[love[u]]]) s.push_back(love[love[u]]); if(love[love[u]] == u) sol--, cout << "WHAT\n"; } } cout << sol << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...