답안 #807563

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
807563 2023-08-04T19:29:40 Z dong_liu Love Polygon (BOI18_polygon) C++17
100 / 100
123 ms 24192 KB
#include <algorithm>
#include <array>
#include <iostream>
#include <vector>
using namespace std;

#define ar array

const int N = 1e5;

vector<int> g[N], cc;
bool on[N];
int o[N], v[N];

void dfs(int i) {
  int j = o[i];
  if (v[j] == 0) {
    v[j] = 1;
    dfs(j);
  } else if (v[j] == 1)
    cc.push_back(j);
  v[i] = 2;
}

ar<int, 2> dp(int i) {
  int t01 = 0, tm = 0;
  for (int j : g[i])
    if (!on[j]) {
      ar<int, 2> a = dp(j);
      t01 += max(a[0], a[1]), tm = max(tm, 1 + a[0] - max(a[0], a[1]));
    }
  return {t01, t01 + tm};
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n;
  static ar<string, 2> e[N];
  static string c[N];
  for (int i = 0; i < n; i++) cin >> e[i][0] >> e[i][1], c[i] = e[i][0];
  if (n % 2) {
    cout << -1 << '\n';
    return 0;
  }
  sort(c, c + n);
  for (int h = 0; h < n; h++) {
    int u = lower_bound(c, c + n, e[h][0]) - c;
    int v = lower_bound(c, c + n, e[h][1]) - c;
    o[u] = v;
    g[v].push_back(u);
  }
  for (int i = 0; i < n; i++)
    if (!v[i]) {
      v[i] = 1;
      dfs(i);
    }
  int p0 = 0, p1 = 0;
  for (int i : cc) {
    int u = i;
    vector<ar<int, 2>> f;
    do {
      on[u] = 1;
      u = o[u];
    } while (u != i);
    u = i;
    do {
      f.push_back(dp(u));
      u = o[u];
    } while (u != i);
    if (f.size() == 1)
      p1 += max(f[0][0], f[0][1]);
    else if (f.size() == 2)
      p0 += 1, p1 += f[0][0] + f[1][0];
    else {
      int x = 0;
      for (int it = 0; it < 2; it++) {
        ar<int, 2> t = f[0];
        for (int i = 1; i < (int)f.size(); i++)
          t = {max(t[0], t[1]) + f[i][0],
               max(t[0] + f[i][0] + 1, max(t[0], t[1]) + f[i][1])};
        x = max(x, max(t[0], t[1]));
        rotate(f.begin(), f.begin() + 1, f.end());
      }
      p1 += x;
    }
  }
  int p2 = n / 2 - p0 - p1;
  cout << p1 + p2 * 2 << '\n';
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 11988 KB Output is correct
2 Correct 5 ms 11988 KB Output is correct
3 Correct 5 ms 12036 KB Output is correct
4 Correct 6 ms 12084 KB Output is correct
5 Correct 5 ms 11988 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 5 ms 11988 KB Output is correct
8 Correct 5 ms 11988 KB Output is correct
9 Correct 5 ms 11988 KB Output is correct
10 Correct 5 ms 11988 KB Output is correct
11 Correct 5 ms 11972 KB Output is correct
12 Correct 5 ms 11988 KB Output is correct
13 Correct 5 ms 11988 KB Output is correct
14 Correct 5 ms 11988 KB Output is correct
15 Correct 7 ms 11988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 12036 KB Output is correct
2 Correct 6 ms 11988 KB Output is correct
3 Correct 6 ms 11964 KB Output is correct
4 Correct 91 ms 23424 KB Output is correct
5 Correct 102 ms 18312 KB Output is correct
6 Correct 99 ms 24192 KB Output is correct
7 Correct 19 ms 14092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 17420 KB Output is correct
2 Correct 118 ms 18840 KB Output is correct
3 Correct 88 ms 18664 KB Output is correct
4 Correct 17 ms 14164 KB Output is correct
5 Correct 123 ms 22420 KB Output is correct
6 Correct 92 ms 16560 KB Output is correct
7 Correct 89 ms 16716 KB Output is correct
8 Correct 86 ms 17284 KB Output is correct
9 Correct 95 ms 16128 KB Output is correct
10 Correct 68 ms 15436 KB Output is correct
11 Correct 5 ms 11988 KB Output is correct
12 Correct 5 ms 12028 KB Output is correct
13 Correct 5 ms 12028 KB Output is correct
14 Correct 6 ms 11988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 11988 KB Output is correct
2 Correct 5 ms 11988 KB Output is correct
3 Correct 5 ms 12036 KB Output is correct
4 Correct 6 ms 12084 KB Output is correct
5 Correct 5 ms 11988 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 5 ms 11988 KB Output is correct
8 Correct 5 ms 11988 KB Output is correct
9 Correct 5 ms 11988 KB Output is correct
10 Correct 5 ms 11988 KB Output is correct
11 Correct 5 ms 11972 KB Output is correct
12 Correct 5 ms 11988 KB Output is correct
13 Correct 5 ms 11988 KB Output is correct
14 Correct 5 ms 11988 KB Output is correct
15 Correct 7 ms 11988 KB Output is correct
16 Correct 7 ms 12036 KB Output is correct
17 Correct 6 ms 11988 KB Output is correct
18 Correct 6 ms 11964 KB Output is correct
19 Correct 91 ms 23424 KB Output is correct
20 Correct 102 ms 18312 KB Output is correct
21 Correct 99 ms 24192 KB Output is correct
22 Correct 19 ms 14092 KB Output is correct
23 Correct 97 ms 17420 KB Output is correct
24 Correct 118 ms 18840 KB Output is correct
25 Correct 88 ms 18664 KB Output is correct
26 Correct 17 ms 14164 KB Output is correct
27 Correct 123 ms 22420 KB Output is correct
28 Correct 92 ms 16560 KB Output is correct
29 Correct 89 ms 16716 KB Output is correct
30 Correct 86 ms 17284 KB Output is correct
31 Correct 95 ms 16128 KB Output is correct
32 Correct 68 ms 15436 KB Output is correct
33 Correct 5 ms 11988 KB Output is correct
34 Correct 5 ms 12028 KB Output is correct
35 Correct 5 ms 12028 KB Output is correct
36 Correct 6 ms 11988 KB Output is correct
37 Correct 112 ms 17860 KB Output is correct
38 Correct 87 ms 17924 KB Output is correct
39 Correct 99 ms 16772 KB Output is correct
40 Correct 91 ms 16636 KB Output is correct
41 Correct 88 ms 16672 KB Output is correct
42 Correct 87 ms 17040 KB Output is correct
43 Correct 87 ms 17044 KB Output is correct
44 Correct 89 ms 17104 KB Output is correct
45 Correct 87 ms 17008 KB Output is correct
46 Correct 113 ms 16988 KB Output is correct
47 Correct 84 ms 16240 KB Output is correct
48 Correct 92 ms 17424 KB Output is correct
49 Correct 91 ms 18668 KB Output is correct
50 Correct 84 ms 18684 KB Output is correct
51 Correct 22 ms 14104 KB Output is correct
52 Correct 99 ms 22400 KB Output is correct
53 Correct 87 ms 16532 KB Output is correct
54 Correct 89 ms 16592 KB Output is correct
55 Correct 90 ms 17276 KB Output is correct
56 Correct 100 ms 16128 KB Output is correct
57 Correct 67 ms 15504 KB Output is correct
58 Correct 5 ms 12032 KB Output is correct
59 Correct 5 ms 11988 KB Output is correct
60 Correct 6 ms 11988 KB Output is correct
61 Correct 5 ms 11988 KB Output is correct
62 Correct 5 ms 11988 KB Output is correct
63 Correct 5 ms 11988 KB Output is correct
64 Correct 6 ms 11988 KB Output is correct
65 Correct 106 ms 23384 KB Output is correct
66 Correct 102 ms 18432 KB Output is correct
67 Correct 94 ms 24056 KB Output is correct
68 Correct 17 ms 14160 KB Output is correct
69 Correct 6 ms 11988 KB Output is correct
70 Correct 6 ms 12084 KB Output is correct
71 Correct 5 ms 11988 KB Output is correct
72 Correct 5 ms 11988 KB Output is correct
73 Correct 6 ms 11988 KB Output is correct
74 Correct 6 ms 11988 KB Output is correct
75 Correct 6 ms 12032 KB Output is correct
76 Correct 6 ms 12032 KB Output is correct
77 Correct 6 ms 11988 KB Output is correct
78 Correct 7 ms 11988 KB Output is correct
79 Correct 7 ms 11988 KB Output is correct
80 Correct 6 ms 11988 KB Output is correct
81 Correct 6 ms 11988 KB Output is correct
82 Correct 7 ms 11988 KB Output is correct
83 Correct 5 ms 11988 KB Output is correct