Submission #442537

# Submission time Handle Problem Language Result Execution time Memory
442537 2021-07-08T06:42:29 Z abc864197532 Love Polygon (BOI18_polygon) C++17
100 / 100
333 ms 38476 KB
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define X first
#define Y second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define test(args...) abc("[" + string(#args) + "]", args)
void abc() {cerr << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
    cerr << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
    while (l != r) cerr << *l << " \n"[++l == r];
}
const int mod = 1e9 + 7, N = 200000;

int dsu[N];

int Find(int x) {
    if (x == dsu[x]) return x;
    return dsu[x] = Find(dsu[x]);
}

bool Union(int u, int v) {
    u = Find(u), v = Find(v);
    if (u == v) return false;
    dsu[u] = v;
    return true;
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, _id = 0;
    cin >> n;
    if (n & 1) return cout << -1 << endl, 0;
    map <string, int> m1;
    auto get = [&](string s) {
        if (m1.count(s)) return m1[s];
        return m1[s] = _id++;
    };
    for (int i = 0; i < n; ++i) dsu[i] = i;
    vector <vector <int>> adj(n);
    vector <int> pt(n);
    string s, t;
    vector <pii> oth;
    for (int i = 0; i < n; ++i) {
        cin >> s >> t;
        int u = get(s), v = get(t);
        pt[u] = v;
        if (Union(u, v)) adj[v].pb(u), adj[u].pb(v);
        else oth.eb(v, u);
    }
    vector <int> cur;
    vector <vector <int>> dp1(n, vector <int>(2, 0));
    vector <vector <int>> dp2(n, vector <int>(2, 0));
    // 0 not choose, 1 choose
    vector <bool> vis(n, false);
    int cnt = 0;
    function<void(int, int, int)> dfs = [&](int v, int pa, int out) {
        cnt++;
        for (int u : adj[v]) if (u != pa) {
            dfs(u, v, out);
            dp1[v][0] += max(dp1[u][0], dp1[u][1]);
            dp2[v][0] += max(dp2[u][0], dp2[u][1]);
        }
        for (int u : adj[v]) if (u != pa) {
            dp1[v][1] = max(dp1[v][1], dp1[v][0] - max(dp1[u][0], dp1[u][1]) + dp1[u][0] + 1);
            if (u != out) dp2[v][1] = max(dp2[v][1], dp2[v][0] - max(dp2[u][0], dp2[u][1]) + dp2[u][0] + 1);
        }
        if (v == out) dp2[v][1] = -1 << 30;
    };
    int ans = n;
    for (pii A : oth) {
        cnt = 0;
        dfs(A.X, -1, A.Y);
        if (A.X != A.Y) ans -= max({dp1[A.X][0], dp1[A.X][1], dp2[A.X][0] + (pt[A.X] == A.Y && pt[A.Y] == A.X ? 2 : 1)});
        else ans -= max(dp1[A.X][0], dp1[A.X][1]);
    }
    cout << ans << endl;
}
# Verdict Execution time Memory 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 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 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 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 315 ms 34868 KB Output is correct
5 Correct 280 ms 25380 KB Output is correct
6 Correct 310 ms 36232 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 291 ms 25348 KB Output is correct
2 Correct 324 ms 27864 KB Output is correct
3 Correct 169 ms 22968 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 308 ms 35396 KB Output is correct
6 Correct 306 ms 25404 KB Output is correct
7 Correct 277 ms 25480 KB Output is correct
8 Correct 249 ms 25020 KB Output is correct
9 Correct 266 ms 25668 KB Output is correct
10 Correct 176 ms 25652 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory 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 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 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 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 315 ms 34868 KB Output is correct
20 Correct 280 ms 25380 KB Output is correct
21 Correct 310 ms 36232 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 291 ms 25348 KB Output is correct
24 Correct 324 ms 27864 KB Output is correct
25 Correct 169 ms 22968 KB Output is correct
26 Correct 0 ms 204 KB Output is correct
27 Correct 308 ms 35396 KB Output is correct
28 Correct 306 ms 25404 KB Output is correct
29 Correct 277 ms 25480 KB Output is correct
30 Correct 249 ms 25020 KB Output is correct
31 Correct 266 ms 25668 KB Output is correct
32 Correct 176 ms 25652 KB Output is correct
33 Correct 0 ms 204 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 0 ms 204 KB Output is correct
36 Correct 0 ms 204 KB Output is correct
37 Correct 315 ms 25452 KB Output is correct
38 Correct 292 ms 27216 KB Output is correct
39 Correct 304 ms 25508 KB Output is correct
40 Correct 279 ms 27588 KB Output is correct
41 Correct 283 ms 27572 KB Output is correct
42 Correct 296 ms 27588 KB Output is correct
43 Correct 282 ms 27548 KB Output is correct
44 Correct 323 ms 27648 KB Output is correct
45 Correct 297 ms 27636 KB Output is correct
46 Correct 310 ms 27692 KB Output is correct
47 Correct 318 ms 27884 KB Output is correct
48 Correct 287 ms 27544 KB Output is correct
49 Correct 321 ms 30056 KB Output is correct
50 Correct 190 ms 25172 KB Output is correct
51 Correct 1 ms 332 KB Output is correct
52 Correct 326 ms 37744 KB Output is correct
53 Correct 302 ms 27548 KB Output is correct
54 Correct 312 ms 27596 KB Output is correct
55 Correct 246 ms 27240 KB Output is correct
56 Correct 282 ms 27760 KB Output is correct
57 Correct 185 ms 27836 KB Output is correct
58 Correct 0 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 0 ms 332 KB Output is correct
63 Correct 1 ms 204 KB Output is correct
64 Correct 0 ms 204 KB Output is correct
65 Correct 333 ms 37020 KB Output is correct
66 Correct 306 ms 27624 KB Output is correct
67 Correct 310 ms 38476 KB Output is correct
68 Correct 1 ms 332 KB Output is correct
69 Correct 0 ms 204 KB Output is correct
70 Correct 0 ms 332 KB Output is correct
71 Correct 1 ms 324 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 332 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 204 KB Output is correct
80 Correct 1 ms 204 KB Output is correct
81 Correct 1 ms 204 KB Output is correct
82 Correct 0 ms 204 KB Output is correct
83 Correct 1 ms 204 KB Output is correct