답안 #723949

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
723949 2023-04-14T13:56:29 Z _martynas Love Polygon (BOI18_polygon) C++11
100 / 100
137 ms 26740 KB
#include <bits/stdc++.h>

using namespace std;

#define F first
#define S second
#define PB push_back
#define all(a) (a).begin(), (a).end()

using pii = pair<int, int>;
using vi = vector<int>;

const int MXN = 1e5+5;

int n;
string A[MXN], B[MXN];
vector<string> names;
int to[MXN];
vi from[MXN];
bool visited[MXN], in_stack[MXN];
int cycle[MXN], id = 1; // id == 0 => not in a cycle
int dp[2][MXN]; // dp[0] don't take, dp[1] take

void recur(int u) {
    if(from[u].empty()) {
        dp[0][u] = 0, dp[1][u] = 1;
        return;
    }
    int sum = 0;
    for(int v : from[u]) {
        if(cycle[v]) continue;
        recur(v);
        sum += dp[0][v];
    }
    dp[1][u] = sum+1;
    dp[0][u] = sum;
    for(int v : from[u]) {
        if(cycle[v]) continue;
        dp[0][u] = max(dp[0][u], sum-dp[0][v]+dp[1][v]);
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for(int i = 0; i < n; i++) {
        cin >> A[i] >> B[i];
        names.PB(A[i]);
    }
    sort(all(names));
    for(int i = 0; i < n; i++) {
        to[lower_bound(all(names), A[i]) - names.begin()] =
            lower_bound(all(names), B[i]) - names.begin();
    }
    if(n % 2) {
        cout << "-1\n";
        return 0;
    }
    for(int i = 0; i < n; i++) {
        from[to[i]].PB(i);
    }
    int ans = 0;
    for(int i = 0; i < n; i++) {
        if(visited[i]) continue;
        int u = i;
        vi st = {u};
        visited[u] = true;
        in_stack[u] = true;
        while(!visited[to[u]]) {
            st.PB(to[u]), u = to[u];
            visited[u] = true, in_stack[u] = true;
        }
        if(in_stack[to[u]]) {
            // new cycle
            vi nodes;
            int v = u;
            u = to[u];
            nodes.PB(v);
            cycle[v] = cycle[u] = id;
            while(u != v) nodes.PB(u), cycle[u] = id, u = to[u];
            id++;
            for(int a : nodes) recur(a);
            if(nodes.size() == 1) {
                ans += dp[0][nodes[0]];
            }
            else if(nodes.size() == 2) {
                ans += dp[1][nodes[0]]+dp[1][nodes[1]];
            }
            else {
                vector<vi> more_dp(2, vi(nodes.size(), -MXN));
                // more_dp[0][i] - i-th is changed
                // more_dp[1][i] - i-th is unchanged
                // 1st case:
                // first node is unchanged
                int mx = 0;
                more_dp[1][0] = dp[1][nodes[0]];
                for(int i = 1; i < nodes.size(); i++) {
                    more_dp[0][i] = max(more_dp[0][i], more_dp[1][i-1]+dp[1][nodes[i]]-1);
                    more_dp[0][i] = max(more_dp[0][i], more_dp[0][i-1]+dp[0][nodes[i]]);
                    more_dp[1][i] = max(more_dp[1][i], more_dp[0][i-1]+dp[1][nodes[i]]);
                }
                mx = more_dp[0][nodes.size()-1];
                more_dp = vector<vi>(2, vi(nodes.size(), -MXN));
                // 2nd case:
                // first node is changed
                more_dp[0][0] = dp[0][nodes[0]];
                for(int i = 1; i < nodes.size(); i++) {
                    more_dp[0][i] = max(more_dp[0][i], more_dp[1][i-1]+dp[1][nodes[i]]-1);
                    more_dp[0][i] = max(more_dp[0][i], more_dp[0][i-1]+dp[0][nodes[i]]);
                    more_dp[1][i] = max(more_dp[1][i], more_dp[0][i-1]+dp[1][nodes[i]]);
                }
                mx = max(mx, max(more_dp[0][nodes.size()-1],
                                 more_dp[1][nodes.size()-1]-dp[0][nodes[0]]+dp[1][nodes[0]]-1));
                ans += mx;
            }
        }
        else {
            // old part of tree
        }
        for(int v : st) in_stack[v] = false;
    }
    cout << n-ans << "\n";
    return 0;
}

Compilation message

polygon.cpp: In function 'int main()':
polygon.cpp:98:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |                 for(int i = 1; i < nodes.size(); i++) {
      |                                ~~^~~~~~~~~~~~~~
polygon.cpp:108:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |                 for(int i = 1; i < nodes.size(); i++) {
      |                                ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8916 KB Output is correct
2 Correct 4 ms 8916 KB Output is correct
3 Correct 5 ms 8916 KB Output is correct
4 Correct 5 ms 8916 KB Output is correct
5 Correct 5 ms 8904 KB Output is correct
6 Correct 5 ms 8916 KB Output is correct
7 Correct 5 ms 8916 KB Output is correct
8 Correct 5 ms 8916 KB Output is correct
9 Correct 5 ms 8916 KB Output is correct
10 Correct 5 ms 8916 KB Output is correct
11 Correct 6 ms 8944 KB Output is correct
12 Correct 5 ms 8916 KB Output is correct
13 Correct 5 ms 8916 KB Output is correct
14 Correct 5 ms 8944 KB Output is correct
15 Correct 6 ms 8944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8916 KB Output is correct
2 Correct 5 ms 8916 KB Output is correct
3 Correct 5 ms 8916 KB Output is correct
4 Correct 118 ms 21564 KB Output is correct
5 Correct 134 ms 19016 KB Output is correct
6 Correct 137 ms 22112 KB Output is correct
7 Correct 112 ms 14768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 115 ms 18268 KB Output is correct
2 Correct 113 ms 20120 KB Output is correct
3 Correct 123 ms 19032 KB Output is correct
4 Correct 96 ms 14800 KB Output is correct
5 Correct 122 ms 26680 KB Output is correct
6 Correct 117 ms 17176 KB Output is correct
7 Correct 109 ms 17560 KB Output is correct
8 Correct 119 ms 17816 KB Output is correct
9 Correct 109 ms 16668 KB Output is correct
10 Correct 89 ms 16408 KB Output is correct
11 Correct 5 ms 8944 KB Output is correct
12 Correct 5 ms 8916 KB Output is correct
13 Correct 5 ms 8944 KB Output is correct
14 Correct 5 ms 8916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8916 KB Output is correct
2 Correct 4 ms 8916 KB Output is correct
3 Correct 5 ms 8916 KB Output is correct
4 Correct 5 ms 8916 KB Output is correct
5 Correct 5 ms 8904 KB Output is correct
6 Correct 5 ms 8916 KB Output is correct
7 Correct 5 ms 8916 KB Output is correct
8 Correct 5 ms 8916 KB Output is correct
9 Correct 5 ms 8916 KB Output is correct
10 Correct 5 ms 8916 KB Output is correct
11 Correct 6 ms 8944 KB Output is correct
12 Correct 5 ms 8916 KB Output is correct
13 Correct 5 ms 8916 KB Output is correct
14 Correct 5 ms 8944 KB Output is correct
15 Correct 6 ms 8944 KB Output is correct
16 Correct 6 ms 8916 KB Output is correct
17 Correct 5 ms 8916 KB Output is correct
18 Correct 5 ms 8916 KB Output is correct
19 Correct 118 ms 21564 KB Output is correct
20 Correct 134 ms 19016 KB Output is correct
21 Correct 137 ms 22112 KB Output is correct
22 Correct 112 ms 14768 KB Output is correct
23 Correct 115 ms 18268 KB Output is correct
24 Correct 113 ms 20120 KB Output is correct
25 Correct 123 ms 19032 KB Output is correct
26 Correct 96 ms 14800 KB Output is correct
27 Correct 122 ms 26680 KB Output is correct
28 Correct 117 ms 17176 KB Output is correct
29 Correct 109 ms 17560 KB Output is correct
30 Correct 119 ms 17816 KB Output is correct
31 Correct 109 ms 16668 KB Output is correct
32 Correct 89 ms 16408 KB Output is correct
33 Correct 5 ms 8944 KB Output is correct
34 Correct 5 ms 8916 KB Output is correct
35 Correct 5 ms 8944 KB Output is correct
36 Correct 5 ms 8916 KB Output is correct
37 Correct 108 ms 18720 KB Output is correct
38 Correct 121 ms 18276 KB Output is correct
39 Correct 126 ms 17696 KB Output is correct
40 Correct 112 ms 17404 KB Output is correct
41 Correct 127 ms 17440 KB Output is correct
42 Correct 127 ms 17884 KB Output is correct
43 Correct 136 ms 17976 KB Output is correct
44 Correct 116 ms 18104 KB Output is correct
45 Correct 112 ms 18000 KB Output is correct
46 Correct 115 ms 17948 KB Output is correct
47 Correct 118 ms 17156 KB Output is correct
48 Correct 126 ms 18332 KB Output is correct
49 Correct 115 ms 20156 KB Output is correct
50 Correct 102 ms 19100 KB Output is correct
51 Correct 100 ms 14916 KB Output is correct
52 Correct 123 ms 26740 KB Output is correct
53 Correct 128 ms 17172 KB Output is correct
54 Correct 121 ms 17516 KB Output is correct
55 Correct 111 ms 17904 KB Output is correct
56 Correct 108 ms 16664 KB Output is correct
57 Correct 91 ms 16384 KB Output is correct
58 Correct 5 ms 8916 KB Output is correct
59 Correct 5 ms 8916 KB Output is correct
60 Correct 5 ms 9044 KB Output is correct
61 Correct 5 ms 8916 KB Output is correct
62 Correct 5 ms 8916 KB Output is correct
63 Correct 5 ms 8944 KB Output is correct
64 Correct 5 ms 8916 KB Output is correct
65 Correct 112 ms 21680 KB Output is correct
66 Correct 120 ms 19096 KB Output is correct
67 Correct 126 ms 21852 KB Output is correct
68 Correct 111 ms 14756 KB Output is correct
69 Correct 4 ms 8916 KB Output is correct
70 Correct 5 ms 8940 KB Output is correct
71 Correct 5 ms 8916 KB Output is correct
72 Correct 4 ms 8916 KB Output is correct
73 Correct 5 ms 8916 KB Output is correct
74 Correct 5 ms 8916 KB Output is correct
75 Correct 5 ms 8944 KB Output is correct
76 Correct 5 ms 8908 KB Output is correct
77 Correct 5 ms 8916 KB Output is correct
78 Correct 5 ms 8916 KB Output is correct
79 Correct 5 ms 8916 KB Output is correct
80 Correct 5 ms 8916 KB Output is correct
81 Correct 5 ms 8916 KB Output is correct
82 Correct 5 ms 8944 KB Output is correct
83 Correct 4 ms 8940 KB Output is correct