Submission #829772

#TimeUsernameProblemLanguageResultExecution timeMemory
829772Darren0724Love Polygon (BOI18_polygon)C++17
0 / 100
156 ms9272 KiB
#include<bits/stdc++.h> using namespace std; const int N=100005; map<string,int> m; vector<int> vis(N); vector<int> v(N),in(N); int cnt=0; int id(string s){ if(m.count(s)){ return m[s]; } int p=m.size(); m[s]=p; return p; } void dfs(int k){ vis[k]=1; cnt++; if(!vis[v[k]]){ dfs(v[k]); } } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n;cin>>n; if(n&1){ cout<<-1<<endl; return 0; } for(int i=0;i<n;i++){ string a,b;cin>>a>>b; v[id(a)]=id(b); in[id(b)]++; } int ans=0; for(int i=0;i<n;i++){ if(!vis[i]&&v[v[i]]==i){ vis[i]=1; vis[v[i]]=1; } } for(int i=0;i<n;i++){ if(in[i]==0){ cnt=0; dfs(i); ans+=(cnt+1)/2; } } cout<<ans<<endl; 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...