Submission #924677

#TimeUsernameProblemLanguageResultExecution timeMemory
924677aykhnLove Polygon (BOI18_polygon)C++17
0 / 100
915 ms1048576 KiB
#include <bits/stdc++.h> // author: aykhn using namespace std; #define int long long #define inf 0x3F3F3F3F const int MXN = 1e5 + 5; int n; vector<int> adj[MXN], g[MXN]; map<string, int> mp; string name[MXN]; int to[MXN], dp[MXN][2]; int col[MXN], in[MXN], mark[MXN], p[MXN]; vector<int> id[MXN]; int idx = 0; int cyc(int a) { col[a] = 1; for (int v : g[a]) { if (col[v] == 2) { continue; } if (col[v] == 1) { idx++; int s = a; int e = v; while (1) { in[s] = 1; id[idx].push_back(s); if (s == e) break; s = p[s]; } return 1; } else { p[v] = a; if (cyc(v)) { col[a] = 2; return 1; } } } col[a] = 2; return 0; } void dfs(int a) { if (adj[a].empty()) { dp[a][0] = dp[a][1] = 1; return; } dp[a][0] = 0; dp[a][1] = inf; for (int v : adj[a]) { dfs(v); dp[a][0] += min(dp[v][0], dp[v][1]); } for (int v : adj[a]) { dp[a][1] = min(dp[a][1], dp[a][0] - min(dp[v][0], dp[v][1]) + dp[v][0] - 1); } dp[a][0]++; assert(dp[a][0] >= dp[a][1]); } signed main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); int n, m = 0, cnt = 0; cin >> n; if (n & 1) { cout << -1 << '\n'; return 0; } for (int i = 1; i <= n; i++) { string a, b; cin >> a >> b; if (mp.find(a) == mp.end()) mp[a] = ++m, name[m] = a; if (mp.find(b) == mp.end()) mp[b] = ++m, name[m] = b; to[mp[a]] = mp[b]; } vector<int> r; int res = 0; for (int i = 1; i <= m; i++) { if (to[to[i]] == i) { res--; mark[i] = 1, mark[to[i]] = 1; continue; } if (to[to[to[i]]] == to[i]) to[i] = i; g[i].push_back(to[i]); } for (int i = 1; i <= m; i++) { if (mark[i]) continue; if (!col[i]) { p[i] = i; cyc(i); } } for (int i = 1; i <= m; i++) { if (mark[i]) continue; if (!(in[i] && in[to[i]])) { adj[to[i]].push_back(i); } } for (int i = 1; i <= m; i++) { if (in[i]) dfs(i); } for (int c = 1; c <= idx; c++) { if (id[c].size() == 1) { res += dp[id[c][0]][1]; continue; } int ans = 0, sz = (int)id[c].size(), mn = 0; for (int x : id[c]) ans += dp[x][1]; vector<array<int, 2>> dpp(id[c].size(), {inf, inf}), dps(id[c].size(), {inf, inf}); dpp[0][1] = dp[id[c][0]][0] - dp[id[c][1]][1]; dpp[0][0] = 0; for (int i = 1; i < id[c].size(); i++) { dpp[i][0] = min(0LL, dpp[i - 1][1] + dp[id[c][i]][0] - dp[id[c][i]][1] - 2); dpp[i][1] = dpp[i - 1][0] + dp[id[c][i]][0] - dp[id[c][i]][1]; mn = min(mn, dp[i][0]); } dps[sz - 1][1] = dp[id[c][sz - 1]][0] - dp[id[c][sz - 1]][1]; for (int i = sz - 1; i >= 0; i--) { dps[i][0] = min(0LL, dps[i + 1][1] + dp[id[c][i]][0] - dp[id[c][i]][1] - 2); dps[i][1] = dps[i + 1][0] + dp[id[c][i]][0] - dp[id[c][i]][1]; } } }

Compilation message (stderr)

polygon.cpp: In function 'int main()':
polygon.cpp:142:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  142 |         for (int i = 1; i < id[c].size(); i++)
      |                         ~~^~~~~~~~~~~~~~
polygon.cpp:81:19: warning: unused variable 'cnt' [-Wunused-variable]
   81 |     int n, m = 0, cnt = 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...