Submission #535572

#TimeUsernameProblemLanguageResultExecution timeMemory
535572perchutsLove Polygon (BOI18_polygon)C++17
54 / 100
287 ms22708 KiB
#include <bits/stdc++.h> #define maxn (int)(1e5+51) #define all(x) x.begin(), x.end() #define sz(x) (int) x.size() #define endl '\n' #define ll long long #define pb push_back #define ull unsigned long long #define ii pair<int,int> #define iii tuple<int,int,int> #define inf 2000000001 #define mod 1000000007 //998244353 #define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); using namespace std; template<typename X, typename Y> bool ckmin(X& x, const Y& y) { return (y < x) ? (x=y,1):0; } template<typename X, typename Y> bool ckmax(X& x, const Y& y) { return (x < y) ? (x=y,1):0; } map<string,int>mp; int par[maxn], cmp[maxn], qnt[maxn], ans; vector<int>g[maxn]; int vis[maxn], mark[maxn]; void dfs(int u,int p){ vis[u] = 2; for(auto v:g[u]){ if(vis[v]==2)continue; dfs(v,u); } if(!mark[u])ans++, mark[p] = 1; } void dfs(int u){ mark[u] = 1; ckmax(vis[u],1); for(auto v:g[u]){ if(!mark[v])dfs(v); } } int main(){_ int n;cin>>n; if(n&1){ cout<<-1<<endl; return 0; } int q = 0; for(int i=1;i<=n;++i){ string a,b;cin>>a>>b; if(mp.find(a)==mp.end())mp[a]=++q; if(mp.find(b)==mp.end())mp[b]=++q; int u = mp[a], v = mp[b]; par[u] = v; g[v].pb(u); } int components = 0; for(int i=1;i<=n;++i){ if(!vis[i]){ int cur = i, cnt = 1; while(!vis[cur])vis[cur] = 1, cur = par[cur]; int start = cur; ++components, cmp[cur] = components, vis[cur] = 2, cur = par[cur]; while(cur!=start) cmp[cur] = components,++cnt, vis[cur] = 2, cur = par[cur]; qnt[components] = cnt&1; if(cnt!=2)ans += (cnt+1)/2; dfs(start); } } for(int i=1;i<=n;++i)mark[i] = 0; for(int i=1;i<=n;++i){ if(vis[i]==1){ int root = i; while(vis[root]==1)root = par[root]; dfs(root,root); if(mark[root])ans -= qnt[cmp[root]], qnt[cmp[root]] = 0; } } cout<<ans<<endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...