Submission #505297

#TimeUsernameProblemLanguageResultExecution timeMemory
505297wiwihoLove Polygon (BOI18_polygon)C++14
100 / 100
225 ms24152 KiB
#include <bits/stdc++.h> #define eb emplace_back #define mp make_pair #define F first #define S second #define printv(a, b) { \ for(auto pv : a) b << pv << " "; \ b << "\n"; \ } using namespace std; using pii = pair<int, int>; const int MAX = 1e9; map<string, int> id; int num = 1; int getid(string& s){ if(id.find(s) == id.end()) id[s] = num++; return id[s]; } int n; vector<vector<int>> g; vector<int> dp0, dp1; vector<bool> vst, vst2; vector<int> f; void init(){ g.resize(n + 1); dp0.resize(n + 1); dp1.resize(n + 1); vst.resize(n + 1); vst2.resize(n + 1); f.resize(n + 1); } void dfs(int now){ vst[now] = true; int sum = 0, mn = n; int cnt = 0; for(int i : g[now]){ if(vst2[i]) continue; dfs(i); cnt++; sum += dp1[i]; mn = min(mn, dp0[i] - dp1[i]); } if(cnt == 0){ dp0[now] = 0; dp1[now] = 1; } else{ dp0[now] = sum; dp1[now] = sum + mn + 1; } } int solve(int s){ //cerr << "solve\n"; while(!vst[s]){ vst[s] = true; s = f[s]; } vector<int> cy; int v = s; do{ vst2[v] = vst[v] = true; cy.eb(v); v = f[v]; } while(v != s); //printv(cy, cerr); for(int i : cy) dfs(i); vector<vector<int>> dps(3, vector<int>(3, MAX)); dps[0][0] = dp0[cy[0]]; dps[1][1] = dp0[cy[0]] + 1; dps[2][2] = dp1[cy[0]]; for(int i = 1; i < cy.size(); i++){ int now = cy[i]; vector<vector<int>> dps2(3, vector<int>(3, MAX)); for(int fst = 0; fst < 3; fst++){ dps2[fst][0] = min(dps[fst][1], dps[fst][2]) + dp0[now]; dps2[fst][1] = min({dps[fst][0], dps[fst][1], dps[fst][2]}) + dp0[now] + 1; dps2[fst][2] = min(dps[fst][1], dps[fst][2]) + dp1[now]; } dps = dps2; } int ans = MAX; ans = min({ans, dps[0][1], dps[0][2]}); ans = min({ans, dps[1][0], dps[1][1], dps[1][2]}); ans = min({ans, dps[2][1], dps[2][2]}); if(cy.size() == 2) ans = min(ans, dp0[cy[0]] + dp0[cy[1]]); //cerr << "solve ok " << ans << "\n"; return ans; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; init(); for(int i = 1; i <= n; i++){ string s, t; cin >> s >> t; int u = getid(s), v = getid(t); f[u] = v; } for(int i = 1; i <= n; i++){ g[f[i]].eb(i); } //for(auto& i : id) cerr << i.F << " " << i.S << "\n"; if(n % 2){ cout << "-1\n"; return 0; } int ans = 0; for(int i = 1; i <= n; i++){ if(vst[i]) continue; ans += solve(i); } //printv(dp0, cerr); //printv(dp1, cerr); cout << ans << "\n"; return 0; }

Compilation message (stderr)

polygon.cpp: In function 'int solve(int)':
polygon.cpp:85:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |     for(int i = 1; i < cy.size(); i++){
      |                    ~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...