답안 #987810

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
987810 2024-05-23T15:49:47 Z MilosMilutinovic Love Polygon (BOI18_polygon) C++14
100 / 100
281 ms 34248 KB
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> to(n);
  map<string, int> mp;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    string t;
    cin >> t;
    if (!mp.count(s)) {
      mp[s] = (int) mp.size();
    }
    if (!mp.count(t)) {
      mp[t] = (int) mp.size();
    }
    to[mp[s]] = mp[t];
  }
  if (n % 2 == 1) {
    cout << -1 << '\n';
    return 0;
  }
  vector<vector<int>> ch(n);
  for (int i = 0; i < n; i++) {
    if (to[i] == i) {
      continue;
    }
    ch[to[i]].push_back(i);
  }
  vector<int> deg(n);
  for (int i = 0; i < n; i++) {
    deg[to[i]] += 1;
  }
  vector<int> que;
  for (int i = 0; i < n; i++) {
    if (deg[i] == 0) {
      que.push_back(i);
    }
  }
  for (int b = 0; b < (int) que.size(); b++) {
    int i = que[b];
    deg[to[i]] -= 1;
    if (deg[to[i]] == 0) {
      que.push_back(to[i]);
    }
  }
  vector<vector<int>> dp(n, vector<int>(2));
  vector<int> comp;
  function<void(int, int)> Solve = [&](int v, int ban) {
    comp.push_back(v);
    int mx = 0;
    for (int u : ch[v]) {
      Solve(u, ban);
      dp[v][0] += max(dp[u][0], dp[u][1]);
      dp[v][1] += max(dp[u][0], dp[u][1]);
      if (u != ban) {
        mx = max(mx, -max(dp[u][0], dp[u][1]) + dp[u][0] + 1);
      }
    }
    dp[v][1] += mx;
    if (v == ban) {
      dp[v][1] = 0;
    }
  };
  int res = 0;
  for (int i = 0; i < n; i++) {
    if (deg[i] == 0) {
      continue;
    }
    if (to[i] == i) {
      Solve(i, -1);
      res += max(dp[i][1], dp[i][0]); 
    } else if (i == to[to[i]]) {
      deg[i] = 0;
      deg[to[i]] = 0;
      {
        vector<int> new_ch;
        for (int x : ch[i]) {
          if (x == to[i]) {
            continue;
          }
          new_ch.push_back(x);
        }
        ch[i] = new_ch;
      }
      {
        vector<int> new_ch;
        for (int x : ch[to[i]]) {
          if (x == i) {
            continue;
          }
          new_ch.push_back(x);
        }
        ch[to[i]] = new_ch;
      }
      Solve(i, -1);
      Solve(to[i], -1);
      res += max(max(dp[i][0], dp[i][1]) + max(dp[to[i]][0], dp[to[i]][1]), dp[i][0] + dp[to[i]][0] + 2);
    } else {
      vector<int> new_ch;
      for (int x : ch[to[i]]) {
        if (x == i) {
          continue;
        }
        new_ch.push_back(x);
      }
      ch[to[i]] = new_ch;
      int prv = to[i];
      to[i] = i;
      comp.clear();
      Solve(i, -1);
      int c = max(dp[i][0], dp[i][1]);
      for (int x : comp) {
        dp[x][0] = 0;
        dp[x][1] = 0;
        deg[x] = 0;
      }
      Solve(i, prv);
      c = max(c, dp[i][0] + 1);
      res += c;
    }
  }
  cout << (n - 2 * res) + res << '\n';
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 216 ms 32712 KB Output is correct
5 Correct 163 ms 21960 KB Output is correct
6 Correct 209 ms 34248 KB Output is correct
7 Correct 133 ms 10576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 190 ms 21776 KB Output is correct
2 Correct 166 ms 24268 KB Output is correct
3 Correct 137 ms 18788 KB Output is correct
4 Correct 129 ms 9860 KB Output is correct
5 Correct 189 ms 32080 KB Output is correct
6 Correct 157 ms 20812 KB Output is correct
7 Correct 198 ms 20768 KB Output is correct
8 Correct 147 ms 20092 KB Output is correct
9 Correct 151 ms 20428 KB Output is correct
10 Correct 114 ms 19540 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 216 ms 32712 KB Output is correct
20 Correct 163 ms 21960 KB Output is correct
21 Correct 209 ms 34248 KB Output is correct
22 Correct 133 ms 10576 KB Output is correct
23 Correct 190 ms 21776 KB Output is correct
24 Correct 166 ms 24268 KB Output is correct
25 Correct 137 ms 18788 KB Output is correct
26 Correct 129 ms 9860 KB Output is correct
27 Correct 189 ms 32080 KB Output is correct
28 Correct 157 ms 20812 KB Output is correct
29 Correct 198 ms 20768 KB Output is correct
30 Correct 147 ms 20092 KB Output is correct
31 Correct 151 ms 20428 KB Output is correct
32 Correct 114 ms 19540 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 175 ms 22988 KB Output is correct
38 Correct 198 ms 23556 KB Output is correct
39 Correct 160 ms 21004 KB Output is correct
40 Correct 174 ms 22128 KB Output is correct
41 Correct 175 ms 21056 KB Output is correct
42 Correct 163 ms 22604 KB Output is correct
43 Correct 174 ms 22224 KB Output is correct
44 Correct 200 ms 22688 KB Output is correct
45 Correct 175 ms 22608 KB Output is correct
46 Correct 215 ms 22496 KB Output is correct
47 Correct 181 ms 21828 KB Output is correct
48 Correct 192 ms 22400 KB Output is correct
49 Correct 207 ms 25036 KB Output is correct
50 Correct 154 ms 19452 KB Output is correct
51 Correct 149 ms 10576 KB Output is correct
52 Correct 211 ms 33020 KB Output is correct
53 Correct 198 ms 21708 KB Output is correct
54 Correct 177 ms 21648 KB Output is correct
55 Correct 172 ms 20916 KB Output is correct
56 Correct 175 ms 21108 KB Output is correct
57 Correct 135 ms 20372 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 1 ms 348 KB Output is correct
60 Correct 1 ms 348 KB Output is correct
61 Correct 1 ms 604 KB Output is correct
62 Correct 1 ms 504 KB Output is correct
63 Correct 1 ms 344 KB Output is correct
64 Correct 1 ms 344 KB Output is correct
65 Correct 281 ms 32716 KB Output is correct
66 Correct 215 ms 22352 KB Output is correct
67 Correct 232 ms 34136 KB Output is correct
68 Correct 189 ms 10800 KB Output is correct
69 Correct 1 ms 348 KB Output is correct
70 Correct 1 ms 344 KB Output is correct
71 Correct 1 ms 344 KB Output is correct
72 Correct 1 ms 348 KB Output is correct
73 Correct 1 ms 348 KB Output is correct
74 Correct 1 ms 348 KB Output is correct
75 Correct 1 ms 348 KB Output is correct
76 Correct 1 ms 348 KB Output is correct
77 Correct 1 ms 348 KB Output is correct
78 Correct 1 ms 348 KB Output is correct
79 Correct 1 ms 344 KB Output is correct
80 Correct 1 ms 348 KB Output is correct
81 Correct 1 ms 348 KB Output is correct
82 Correct 1 ms 348 KB Output is correct
83 Correct 1 ms 348 KB Output is correct