Submission #535850

#TimeUsernameProblemLanguageResultExecution timeMemory
535850perchutsLove Polygon (BOI18_polygon)C++17
29 / 100
289 ms24524 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], ans; vector<int>g[maxn]; int vis[maxn], mark[maxn], mark2[maxn][2], dp[maxn][2]; int dfs(int u, bool mode){ if(mark2[u][mode])return dp[u][mode]; ckmax(vis[u],1); mark2[u][mode] = 1; ckmax(vis[u],1); for(auto v:g[u]){ if(vis[v]!=2){ if(mode)dp[u][1] += dfs(v,0); else dp[u][0] += max(dfs(v,0),dfs(v,1)); } } return dp[u][mode] = dp[u][mode] + mode; } 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); } for(int i=1;i<=n;++i){ if(!vis[i]){ int cur = i; while(!vis[cur])vis[cur] = 1, cur = par[cur]; int start = cur, cnt = 1; cur = par[cur]; while(cur!=start){ vis[cur] = 2, cur = par[cur], ++cnt; } vis[start] = 2; if(cnt==2){ mark[cur] = mark[par[cur]] = 1; for(auto v:g[cur])if(vis[v]!=2)ans += dfs(v,0); for(auto v:g[par[cur]])if(vis[v]!=2)ans += dfs(v,0); }else{ cur = par[start]; if(sz(g[start])==1&&cnt==1){ ans++; continue; } if(sz(g[start])!=1){ if(dfs(start,1)>dfs(start,0))mark[start] = 1; ans += max(dfs(start,1),dfs(start,0)); } while(cur!=start){ if(sz(g[cur])!=1){ if(dfs(cur,1)>dfs(cur,0))mark[cur] = 1; ans += max(dfs(cur,1),dfs(cur,0)); } cur = par[cur]; } cur = par[start]; while(cur!=start&&sz(g[cur])==1)cur = par[cur]; start = cur, cur = par[cur], cnt = 0; while(cur!=start){ if(sz(g[cur])!=1)ans += (cnt+1)/2, cnt = 0; else cnt++; cur = par[cur]; } ans += (cnt+1)/2; } } } 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...