답안 #397397

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
397397 2021-05-02T03:15:06 Z KoD Love Polygon (BOI18_polygon) C++17
100 / 100
233 ms 14508 KB
#include <bits/stdc++.h>

template <class T>
using Vec = std::vector<T>;

int main() {
    int N;
    std::cin >> N;
    if (N % 2 == 1) {
        std::cout << -1 << '\n';
        return 0;
    }
    Vec<int> to(N);
    {
        Vec<std::string> U(N), V(N);
        for (int i = 0; i < N; ++i) {
            std::cin >> U[i] >> V[i];
        }    
        auto cmp = U;
        std::sort(cmp.begin(), cmp.end());
        for (int i = 0; i < N; ++i) {
            const int u = std::lower_bound(cmp.cbegin(), cmp.cend(), U[i]) - cmp.cbegin();
            const int v = std::lower_bound(cmp.cbegin(), cmp.cend(), V[i]) - cmp.cbegin();
            to[u] = v;
        }
    }
    Vec<int> deg(N);
    for (int i = 0; i < N; ++i) {
        deg[to[i]] += 1;
    }
    Vec<Vec<int>> tree(N);
    Vec<bool> used(N);
    {
        std::queue<int> que;
        for (int i = 0; i < N; ++i) {
            if (deg[i] == 0) {
                que.push(i);
            }
        }
        while (!que.empty()) {
            const auto u = que.front();
            que.pop();
            tree[to[u]].push_back(u);
            deg[to[u]] -= 1;
            if (deg[to[u]] == 0) {
                que.push(to[u]);
            }
            used[u] = true;
        }
    }
    const auto dfs = [&](auto&& dfs, const int u) -> std::array<int, 2> {
        std::array<int, 2> ret{};
        int max_dif = 0;
        for (const auto v: tree[u]) {
            const auto [x, y] = dfs(dfs, v);
            ret[0] += std::max(x, y);
            max_dif = std::max(max_dif, (x + 1) - std::max(x, y));
        }
        ret[1] = ret[0] + max_dif;
        return ret;
    };
    int use = 0;
    for (int src = 0; src < N; ++src) {
        if (used[src]) {
            continue;
        }
        Vec<std::array<int, 2>> val;
        {
            int u = src;
            while (!used[u]) {
                val.push_back(dfs(dfs, u));
                used[u] = true;
                u = to[u];
            }
        }
        const int len = (int) val.size();
        if (len == 1) {
            use += std::max(val[0][0], val[0][1]);
            continue;
        }
        if (len == 2) {
            use += val[0][0] + val[1][0] + 2;
            continue;
        }
        std::array<std::array<int, 2>, 2> dp{};
        dp[0][0] = val[0][0];
        dp[0][1] = val[0][1];
        dp[1][1] = val[0][0] + 1;
        for (int i = 1; i < len; ++i) {
            std::array<std::array<int, 2>, 2> next{};
            for (int j = 0; j < 2; ++j) {
                for (int k = 0; k < 2; ++k) {
                    for (int l = 0; l < 2; ++l) {
                        for (int m = 0; m < 2; ++m) {
                            if (j + l + m > 1) {
                                continue;
                            }
                            next[m][k] = std::max(next[m][k], dp[j][k] + val[i][l] + m);
                        }
                    }
                }
            }
            dp = std::move(next);
        }       
        use += std::max({ dp[0][0], dp[0][1], dp[1][0] });
    }
    std::cout << N - use << '\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 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 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 292 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
15 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 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 183 ms 10084 KB Output is correct
5 Correct 196 ms 10076 KB Output is correct
6 Correct 191 ms 10052 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 195 ms 10016 KB Output is correct
2 Correct 202 ms 12228 KB Output is correct
3 Correct 181 ms 12232 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 197 ms 14428 KB Output is correct
6 Correct 196 ms 12200 KB Output is correct
7 Correct 193 ms 12176 KB Output is correct
8 Correct 219 ms 12232 KB Output is correct
9 Correct 233 ms 12356 KB Output is correct
10 Correct 164 ms 12184 KB Output is correct
11 Correct 1 ms 204 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 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 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 292 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
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 204 KB Output is correct
19 Correct 183 ms 10084 KB Output is correct
20 Correct 196 ms 10076 KB Output is correct
21 Correct 191 ms 10052 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 195 ms 10016 KB Output is correct
24 Correct 202 ms 12228 KB Output is correct
25 Correct 181 ms 12232 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 197 ms 14428 KB Output is correct
28 Correct 196 ms 12200 KB Output is correct
29 Correct 193 ms 12176 KB Output is correct
30 Correct 219 ms 12232 KB Output is correct
31 Correct 233 ms 12356 KB Output is correct
32 Correct 164 ms 12184 KB Output is correct
33 Correct 1 ms 204 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 197 ms 12184 KB Output is correct
38 Correct 197 ms 12172 KB Output is correct
39 Correct 199 ms 12164 KB Output is correct
40 Correct 216 ms 12192 KB Output is correct
41 Correct 198 ms 12180 KB Output is correct
42 Correct 231 ms 12176 KB Output is correct
43 Correct 197 ms 12232 KB Output is correct
44 Correct 191 ms 12232 KB Output is correct
45 Correct 232 ms 12316 KB Output is correct
46 Correct 201 ms 12228 KB Output is correct
47 Correct 188 ms 12184 KB Output is correct
48 Correct 202 ms 12316 KB Output is correct
49 Correct 201 ms 12192 KB Output is correct
50 Correct 178 ms 12184 KB Output is correct
51 Correct 1 ms 208 KB Output is correct
52 Correct 205 ms 14508 KB Output is correct
53 Correct 198 ms 12176 KB Output is correct
54 Correct 190 ms 12184 KB Output is correct
55 Correct 212 ms 12284 KB Output is correct
56 Correct 186 ms 12228 KB Output is correct
57 Correct 176 ms 12228 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 204 KB Output is correct
63 Correct 1 ms 204 KB Output is correct
64 Correct 1 ms 204 KB Output is correct
65 Correct 187 ms 12228 KB Output is correct
66 Correct 182 ms 12180 KB Output is correct
67 Correct 183 ms 12236 KB Output is correct
68 Correct 1 ms 204 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 204 KB Output is correct
79 Correct 1 ms 300 KB Output is correct
80 Correct 1 ms 232 KB Output is correct
81 Correct 1 ms 204 KB Output is correct
82 Correct 1 ms 204 KB Output is correct
83 Correct 1 ms 204 KB Output is correct