Submission #676697

# Submission time Handle Problem Language Result Execution time Memory
676697 2022-12-31T17:57:24 Z Cyanmond Love Polygon (BOI18_polygon) C++17
100 / 100
334 ms 27064 KB
#include <bits/stdc++.h>

void chmax(int &a, int b) {
    a = std::max(a, b);
}

int main() {
    int N;
    std::cin >> N;
    if (N % 2 == 1) {
        std::cout << -1 << std::endl;
        return 0;
    }
    std::vector<std::vector<int>> namori(N), r_namori(N);
    std::map<std::string, int> index_map;

    auto get_index = [&](std::string x) {
        if (index_map.find(x) == index_map.end()) {
            return index_map[x] = (int)index_map.size();
        } else {
            return index_map[x];
        }
    };

    for (int i = 0; i < N; ++i) {
        std::string a, b;
        std::cin >> a >> b;
        const int x = get_index(a), y = get_index(b);
        namori[x].push_back(y);
        r_namori[y].push_back(x);
    }

    std::vector<bool> seen(N);

    auto calc_max_matching = [&](auto &&self, const int root, const int p) -> std::pair<int, bool> {
        seen[root] = true;
        bool used = false;
        int ret = 0;
        for (const int t : r_namori[root]) {
            if (t == p) {
                continue;
            }
            const auto [sum, b] = self(self, t, root);
            ret += sum;
            if (not b and not used) {
                used = true;
                ++ret;
            }
        }
        return std::make_pair(ret, used);
    };

    int answer = 0;
    for (int f = 0; f < N; ++f) {
        if (seen[f]) {
            continue;
        }

        // find cycle
        std::vector<int> cycle;
        auto find_cycle = [&](auto &&self, const int v) -> bool {
            cycle.push_back(v);
            if (seen[v]) {
                return true;
            }
            seen[v] = true;
            for (const int t : namori[v]) {
                if (self(self, t)) {
                    return true;
                }
            }
            cycle.pop_back();
            return false;
        };
        find_cycle(find_cycle, f);
        cycle.erase(cycle.begin(), std::find(cycle.begin(), cycle.end(), cycle.back()) + 1);

        // greedy matching
        const int m = (int)cycle.size();
        std::vector<std::pair<int, int>> vals(m);
        for (int i = 0; i < m; ++i) {
            const int v = cycle[i];
            vals[i].first = calc_max_matching(calc_max_matching, v, cycle[(i - 1 + m) % m]).first;
            int sum = 0;
            for (const int t : r_namori[v]) {
                if (t == cycle[(i - 1 + m) % m]) {
                    continue;
                }
                sum += calc_max_matching(calc_max_matching, t, v).first;
            }
            vals[i].second = sum;
        }

        // dp
        auto calc_dp = [&](std::vector<std::pair<int, int>> data) {
            std::vector<std::pair<int, int>> dp(m + 1, {-N, -N});
            dp[0].first = 0;
            for (int i = 0; i < m; ++i) {
                chmax(dp[i + 1].first, dp[i].first + data[i].first);
                chmax(dp[i + 1].first, dp[i].second + data[i].second + 1);
                chmax(dp[i + 1].second, dp[i].first + data[i].second);
                if (cycle.size() == 2) {
                    chmax(dp[i + 1].first, dp[i].second + data[i].second + 2);
                }
                chmax(dp[i + 1].first, dp[i + 1].second);
            }
            return dp[m].first;
        };

        int res = calc_dp(vals);
        vals.push_back(vals[0]);
        vals.erase(vals.begin());
        chmax(res, calc_dp(vals));
        answer += res;
    }

    std::cout << N - answer << std::endl;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 285 ms 26316 KB Output is correct
5 Correct 287 ms 21152 KB Output is correct
6 Correct 295 ms 27040 KB Output is correct
7 Correct 0 ms 312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 277 ms 20428 KB Output is correct
2 Correct 304 ms 21560 KB Output is correct
3 Correct 224 ms 21196 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 334 ms 24400 KB Output is correct
6 Correct 255 ms 19732 KB Output is correct
7 Correct 272 ms 19680 KB Output is correct
8 Correct 255 ms 20024 KB Output is correct
9 Correct 224 ms 19244 KB Output is correct
10 Correct 184 ms 18392 KB Output is correct
11 Correct 1 ms 300 KB Output is correct
12 Correct 1 ms 304 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 300 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 285 ms 26316 KB Output is correct
20 Correct 287 ms 21152 KB Output is correct
21 Correct 295 ms 27040 KB Output is correct
22 Correct 0 ms 312 KB Output is correct
23 Correct 277 ms 20428 KB Output is correct
24 Correct 304 ms 21560 KB Output is correct
25 Correct 224 ms 21196 KB Output is correct
26 Correct 1 ms 312 KB Output is correct
27 Correct 334 ms 24400 KB Output is correct
28 Correct 255 ms 19732 KB Output is correct
29 Correct 272 ms 19680 KB Output is correct
30 Correct 255 ms 20024 KB Output is correct
31 Correct 224 ms 19244 KB Output is correct
32 Correct 184 ms 18392 KB Output is correct
33 Correct 1 ms 300 KB Output is correct
34 Correct 1 ms 304 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 1 ms 296 KB Output is correct
37 Correct 300 ms 20896 KB Output is correct
38 Correct 267 ms 20848 KB Output is correct
39 Correct 267 ms 19812 KB Output is correct
40 Correct 285 ms 19736 KB Output is correct
41 Correct 273 ms 19676 KB Output is correct
42 Correct 260 ms 20184 KB Output is correct
43 Correct 278 ms 20096 KB Output is correct
44 Correct 275 ms 20040 KB Output is correct
45 Correct 267 ms 20112 KB Output is correct
46 Correct 272 ms 20068 KB Output is correct
47 Correct 219 ms 19320 KB Output is correct
48 Correct 287 ms 20428 KB Output is correct
49 Correct 265 ms 21576 KB Output is correct
50 Correct 240 ms 21328 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 276 ms 24368 KB Output is correct
53 Correct 264 ms 19744 KB Output is correct
54 Correct 268 ms 19656 KB Output is correct
55 Correct 242 ms 20020 KB Output is correct
56 Correct 230 ms 19200 KB Output is correct
57 Correct 193 ms 18416 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 1 ms 300 KB Output is correct
61 Correct 1 ms 212 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 1 ms 212 KB Output is correct
64 Correct 0 ms 212 KB Output is correct
65 Correct 269 ms 26216 KB Output is correct
66 Correct 258 ms 21344 KB Output is correct
67 Correct 271 ms 27064 KB Output is correct
68 Correct 1 ms 340 KB Output is correct
69 Correct 1 ms 300 KB Output is correct
70 Correct 1 ms 300 KB Output is correct
71 Correct 1 ms 300 KB Output is correct
72 Correct 1 ms 212 KB Output is correct
73 Correct 1 ms 296 KB Output is correct
74 Correct 1 ms 212 KB Output is correct
75 Correct 1 ms 212 KB Output is correct
76 Correct 1 ms 212 KB Output is correct
77 Correct 1 ms 340 KB Output is correct
78 Correct 1 ms 212 KB Output is correct
79 Correct 0 ms 212 KB Output is correct
80 Correct 1 ms 212 KB Output is correct
81 Correct 1 ms 212 KB Output is correct
82 Correct 0 ms 212 KB Output is correct
83 Correct 0 ms 300 KB Output is correct