Submission #1098738

#TimeUsernameProblemLanguageResultExecution timeMemory
1098738vjudge1Love Polygon (BOI18_polygon)C++17
46 / 100
2096 ms15916 KiB
#include <bits/stdc++.h> using namespace std; #define ar array #define int long long #define ld long double #define crash assert(69 == 420) const int N = 2e5 + 20; const int MOD = 1e9 + 7; const int INF = 1e18; const int X = 100; int p[N]; int n; namespace sub1{ vector<int> g[N]; bitset<N> vis; int cnt = 0; void dfs(int x){ vis[x] = 1; cnt++; for(auto u: g[x]){ if(!vis[u])dfs(u); } } bool check(int msk){ vector<int> v; for(int i = 0;i < n;i++){ if(p[i] == i){ if((1 << i) & msk){ }else return 0; } } for(int i = 0;i < n;i++)g[i].clear(); vis &= 0; for(int i = 0;i < n;i++){ if((1 << i) & msk){ }else{ g[i].push_back(p[i]); g[p[i]].push_back(i); } } for(int i = 0;i<n;i++){ if(!vis[i]){ cnt = 0; dfs(i); if(cnt > 2)return 0; } } return 1; } }; namespace sub2{ void solve(){ int ans = 0; bitset<N> vis; for(int i = 0;i < n;i++){ if(vis[i])continue; int u = p[i]; vis[i] = 1; int cnt = 1; while(u != i){ vis[u] = 1; cnt++; u = p[u]; } if(cnt % 2)ans += cnt / 2 + 1; else if(cnt > 2)ans += cnt / 2; } cout<<ans; } }; signed main(){ios_base::sync_with_stdio(false);cin.tie(0); //int n; cin>>n; if(n % 2){ cout<<-1<<endl; return 0; } map<string, int> mp; int G = 0; for(int i = 0;i < n;i++){ string a, b; cin>>a>>b; if(!mp.count(a))mp[a] = G++; if(!mp.count(b))mp[b] = G++; p[mp[a]] = mp[b]; } if(n <= 20){ int ans = INF; for(int i = 0;i < (1 << n);i++){ if(sub1::check(i))ans = min(ans, (int)__builtin_popcount(i)); } cout<<ans; return 0; } sub2::solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...