Submission #403977

# Submission time Handle Problem Language Result Execution time Memory
403977 2021-05-13T16:09:17 Z rama_pang Love Polygon (BOI18_polygon) C++17
100 / 100
319 ms 36376 KB
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int N;
  cin >> N;
  vector<int> to(N);

  const auto GetName = [&](string s) {
    static map<string, int> mp;
    if (mp.count(s)) return mp[s];
    return mp[s] = mp.size();
  };

  for (int i = 0; i < N; i++) {
    string s, t;
    cin >> s >> t;
    int u = GetName(s);
    int v = GetName(t);
    to[u] = v;
  }

  if (N & 1) {
    cout << -1 << '\n';
    return 0;
  }

  const auto SolveTree = [&](vector<int> nodes) -> int {
    static vector<int> dp0(N), dp1(N);
    static vector<vector<int>> adj(N);
    for (auto u : nodes) adj[u].clear();

    int root = -1;
    for (auto u : nodes) {
      if (to[u] == u) {
        assert(root == -1);
        root = u;
      } else {
        adj[to[u]].emplace_back(u);
      }
    }

    const auto Dfs = [&](const auto &self, int u) -> void {
      // If we don't use arrow on v:
      // - Must use arrow on u
      // - Must use arrow on other children of u
      // - Must use arrow on children of v
      // dp0[u] = don't use arrow on u
      // dp1[u] = use arrow on u
      dp0[u] = 0;
      dp1[u] = to[u] == u ? -1e9 : 1;

      int sum0 = 0;
      for (auto v : adj[u]) {
        self(self, v);
        sum0 += dp0[v];
      }
      dp0[u] += sum0;
      dp1[u] += sum0;
      for (auto v : adj[u]) {
        dp0[u] = max(dp0[u], sum0 - dp0[v] + dp1[v]);
      }
    };

    assert(root != -1);
    Dfs(Dfs, root);
    return max(dp0[root], dp1[root]);
  };

  const auto SolveForest = [&](vector<int> nodes) -> int {
    static vector<int> vis(N);
    static vector<vector<int>> adj(N);
    for (auto u : nodes) vis[u] = 0;
    for (auto u : nodes) adj[u].clear();

    for (auto u : nodes) {
      adj[u].emplace_back(to[u]);
      adj[to[u]].emplace_back(u);
    }

    int ans = 0;
    for (auto i : nodes) if (!vis[i]) {
      vis[i] = 1;
      vector<int> que = {i};
      for (int j = 0; j < int(que.size()); j++) {
        int u = que[j];
        for (auto v : adj[u]) if (!vis[v]) {
          vis[v] = 1;
          que.emplace_back(v);
        }
      }
      ans += SolveTree(que);
    }

    return ans;
  };

  const auto Solve = [&](vector<int> nodes) -> int {
    static vector<int> indeg(N);
    static vector<int> in_cycle(N);
    for (auto u : nodes) indeg[u] = 0;
    for (auto u : nodes) in_cycle[u] = 1;

    for (auto u : nodes) {
      indeg[to[u]]++;
    }

    vector<int> que;
    for (auto u : nodes) {
      if (indeg[u] == 0) {
        que.emplace_back(u);
      }
    }

    for (int i = 0; i < int(que.size()); i++) {
      int u = que[i];
      int v = to[u];
      indeg[v]--;
      in_cycle[u] = 0;
      if (indeg[v] == 0) {
        que.emplace_back(v);
      }
    }

    vector<int> cyc;
    for (auto u : nodes) if (in_cycle[u]) {
      cyc.emplace_back(u);
    }

    if (cyc.size() == 1) {
      return SolveTree(nodes);
    } else {
      int u = cyc[0];
      int v = to[u];
      assert(u != v);

      // Don't use (u, v)
      to[u] = u;
      int ans_1 = SolveTree(nodes);
      to[u] = v;

      // Use (u, v)
      vector<int> nn = nodes;
      nn.erase(find(begin(nn), end(nn), u));
      nn.erase(find(begin(nn), end(nn), v));
      for (auto x : nn) {
        if (to[x] == u || to[x] == v) {
          to[x] = x;
        }
      }
      int ans_2 = (to[u] == v) + (to[v] == u) + SolveForest(nn);

      return max(ans_1, ans_2);
    }
  };

  int ans = 0;
  vector<int> vis(N);
  vector<vector<int>> adj(N);
  for (int i = 0; i < N; i++) {
    adj[i].emplace_back(to[i]);
    adj[to[i]].emplace_back(i);
  }

  for (int i = 0; i < N; i++) if (!vis[i]) {
    vis[i] = 1;
    vector<int> que = {i};
    for (int j = 0; j < int(que.size()); j++) {
      int u = que[j];
      for (auto v : adj[u]) if (!vis[v]) {
        vis[v] = 1;
        que.emplace_back(v);
      }
    }
    ans += Solve(que);
  }

  cout << N - ans << '\n';
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 308 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 316 KB Output is correct
9 Correct 2 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 2 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 248 KB Output is correct
14 Correct 1 ms 268 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 284 ms 34784 KB Output is correct
5 Correct 290 ms 27760 KB Output is correct
6 Correct 290 ms 36348 KB Output is correct
7 Correct 189 ms 10668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 248 ms 20800 KB Output is correct
2 Correct 248 ms 23484 KB Output is correct
3 Correct 181 ms 18324 KB Output is correct
4 Correct 290 ms 8816 KB Output is correct
5 Correct 248 ms 26448 KB Output is correct
6 Correct 241 ms 21696 KB Output is correct
7 Correct 231 ms 20164 KB Output is correct
8 Correct 249 ms 19636 KB Output is correct
9 Correct 216 ms 20288 KB Output is correct
10 Correct 155 ms 21104 KB Output is correct
11 Correct 1 ms 244 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 308 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 316 KB Output is correct
9 Correct 2 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 2 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 248 KB Output is correct
14 Correct 1 ms 268 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 284 ms 34784 KB Output is correct
20 Correct 290 ms 27760 KB Output is correct
21 Correct 290 ms 36348 KB Output is correct
22 Correct 189 ms 10668 KB Output is correct
23 Correct 248 ms 20800 KB Output is correct
24 Correct 248 ms 23484 KB Output is correct
25 Correct 181 ms 18324 KB Output is correct
26 Correct 290 ms 8816 KB Output is correct
27 Correct 248 ms 26448 KB Output is correct
28 Correct 241 ms 21696 KB Output is correct
29 Correct 231 ms 20164 KB Output is correct
30 Correct 249 ms 19636 KB Output is correct
31 Correct 216 ms 20288 KB Output is correct
32 Correct 155 ms 21104 KB Output is correct
33 Correct 1 ms 244 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 1 ms 204 KB Output is correct
37 Correct 290 ms 29512 KB Output is correct
38 Correct 272 ms 29772 KB Output is correct
39 Correct 256 ms 27704 KB Output is correct
40 Correct 277 ms 31008 KB Output is correct
41 Correct 266 ms 28312 KB Output is correct
42 Correct 277 ms 28836 KB Output is correct
43 Correct 302 ms 29344 KB Output is correct
44 Correct 278 ms 29620 KB Output is correct
45 Correct 319 ms 29280 KB Output is correct
46 Correct 318 ms 31084 KB Output is correct
47 Correct 238 ms 29872 KB Output is correct
48 Correct 230 ms 22852 KB Output is correct
49 Correct 283 ms 25560 KB Output is correct
50 Correct 184 ms 20472 KB Output is correct
51 Correct 232 ms 10640 KB Output is correct
52 Correct 276 ms 28568 KB Output is correct
53 Correct 316 ms 23716 KB Output is correct
54 Correct 253 ms 22212 KB Output is correct
55 Correct 209 ms 21620 KB Output is correct
56 Correct 205 ms 22260 KB Output is correct
57 Correct 150 ms 23212 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 1 ms 204 KB Output is correct
62 Correct 1 ms 316 KB Output is correct
63 Correct 2 ms 204 KB Output is correct
64 Correct 2 ms 204 KB Output is correct
65 Correct 262 ms 34640 KB Output is correct
66 Correct 269 ms 27628 KB Output is correct
67 Correct 260 ms 36376 KB Output is correct
68 Correct 212 ms 10568 KB Output is correct
69 Correct 1 ms 204 KB Output is correct
70 Correct 1 ms 204 KB Output is correct
71 Correct 1 ms 204 KB Output is correct
72 Correct 1 ms 204 KB Output is correct
73 Correct 1 ms 204 KB Output is correct
74 Correct 1 ms 204 KB Output is correct
75 Correct 1 ms 204 KB Output is correct
76 Correct 1 ms 204 KB Output is correct
77 Correct 1 ms 204 KB Output is correct
78 Correct 1 ms 316 KB Output is correct
79 Correct 1 ms 204 KB Output is correct
80 Correct 1 ms 316 KB Output is correct
81 Correct 1 ms 204 KB Output is correct
82 Correct 1 ms 312 KB Output is correct
83 Correct 1 ms 204 KB Output is correct