답안 #442535

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
442535 2021-07-08T06:40:06 Z abc864197532 Love Polygon (BOI18_polygon) C++17
75 / 100
344 ms 36312 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 && u != out) {
            dp1[v][1] = max(dp1[v][1], dp1[v][0] - max(dp1[u][0], dp1[u][1]) + dp1[u][0] + 1);
            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] = 0;
    };
    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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 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 204 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 318 ms 34764 KB Output is correct
5 Correct 305 ms 25472 KB Output is correct
6 Correct 311 ms 36312 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 319 ms 25428 KB Output is correct
2 Correct 329 ms 27700 KB Output is correct
3 Correct 177 ms 22928 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 323 ms 35472 KB Output is correct
6 Correct 278 ms 25376 KB Output is correct
7 Correct 283 ms 25540 KB Output is correct
8 Correct 232 ms 24912 KB Output is correct
9 Correct 256 ms 25668 KB Output is correct
10 Correct 184 ms 25720 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
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 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 204 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 1 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 318 ms 34764 KB Output is correct
20 Correct 305 ms 25472 KB Output is correct
21 Correct 311 ms 36312 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 319 ms 25428 KB Output is correct
24 Correct 329 ms 27700 KB Output is correct
25 Correct 177 ms 22928 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 323 ms 35472 KB Output is correct
28 Correct 278 ms 25376 KB Output is correct
29 Correct 283 ms 25540 KB Output is correct
30 Correct 232 ms 24912 KB Output is correct
31 Correct 256 ms 25668 KB Output is correct
32 Correct 184 ms 25720 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 344 ms 25384 KB Output is correct
38 Correct 301 ms 27224 KB Output is correct
39 Incorrect 280 ms 25452 KB Output isn't correct
40 Halted 0 ms 0 KB -