Submission #666446

#TimeUsernameProblemLanguageResultExecution timeMemory
666446AugustynLove Polygon (BOI18_polygon)C++14
54 / 100
88 ms11212 KiB
#include<bits/stdc++.h> using namespace std; int n,a,b,il; char wcz; int pol[100001],lvl[100001],odp; bool spar[100001]; queue<int>kol; string s1,s2; unordered_map<string,int>skal; int main() { cin>>n; ios_base::sync_with_stdio(0); if(n%2==1) { printf("-1"); return 0; } for(int i=n;i;--i) { cin>>s1>>s2; if(skal[s1]) a=skal[s1]; else { ++il; skal[s1]=il; a=il; } if(skal[s2]) b=skal[s2]; else { ++il; skal[s2]=il; b=il; } pol[a]=b; ++lvl[b]; } for(int i=1;i<=il;++i) { if(lvl[i]==0) kol.push(i); } while(!kol.empty()) { a=kol.front(); kol.pop(); if(pol[a]==a||spar[a]) continue; if(spar[pol[a]]==0) { spar[pol[a]]=1; spar[a]=1; ++odp; lvl[pol[a]]=0; --lvl[pol[pol[a]]]; if(lvl[pol[pol[a]]]==0) kol.push(pol[pol[a]]); } else { ++odp; spar[a]=1; } --lvl[pol[a]]; if(lvl[pol[a]]==0) kol.push(pol[a]); } for(int i=1;i<=il;++i) { if(spar[i]==0) { int ter=pol[i]; int wielk=1; spar[i]=1; while(ter!=i) { spar[ter]=1; ter=pol[ter]; ++wielk; } if(wielk!=2) odp+=wielk/2+(wielk%2); } } cout<<odp; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...