답안 #371528

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
371528 2021-02-26T20:12:31 Z mihai145 Love Polygon (BOI18_polygon) C++14
100 / 100
261 ms 31760 KB
//
// Created by mihai145 on 26.02.2021.
//

#include <iostream>
#include <vector>
#include <unordered_map>
#include <stack>
#include <algorithm>

using namespace std;

const int NMAX = 1e5;

int N, k;
unordered_map<string, int> names;

vector<int> g[NMAX + 5];
vector<int> bi[NMAX + 5];

bool solvedFor[NMAX + 5];

int Get(string s) {
    if (!names[s]) {
        names[s] = ++k;
    }

    return names[s];
}

void dfsConex(int node) {
    solvedFor[node] = true;
    for (int son : bi[node]) {
        if (!solvedFor[son]) {
            dfsConex(son);
        }
    }
}

vector<int> cycle;
bool inStack[NMAX + 5], cycleNode[NMAX + 5];
stack<int> st;

void dfsFindCycle(int node) {
    inStack[node] = true;
    st.push(node);

    for (int son : g[node]) {
        if (inStack[son] == true) {

            while (!st.empty() && st.top() != son) {
                cycle.push_back(st.top());
                cycleNode[st.top()] = true;
                st.pop();
            }

            cycle.push_back(st.top());
            cycleNode[st.top()] = true;

            return;

        } else {
            dfsFindCycle(son);
        }
    }
}

int dpTree[NMAX + 5][2], dpCycle[NMAX + 2][2];

void CalcDpTree(int node, int par = -1) {

    int nrSons = 0;

    for (int son : bi[node]) {
        if (son != par && !cycleNode[son]) {
            nrSons++;
            CalcDpTree(son, node);
        }
    }

    if (nrSons == 0) {
        dpTree[node][0] = dpTree[node][1] = 0;
    } else {

        int maxMatch = -N - 1;
        for (int son : bi[node]) {
            if (son != par && !cycleNode[son]) {
                dpTree[node][0] += max(dpTree[son][0], dpTree[son][1]);
                dpTree[node][1] += max(dpTree[son][0], dpTree[son][1]);
                maxMatch = max(maxMatch, -max(dpTree[son][0], dpTree[son][1]) + dpTree[son][0]);
            }
        }

        dpTree[node][1] += maxMatch;
        dpTree[node][1] += 1;
    }
}

int keeps;

void solve(int node) {
    dfsConex(node);

    while (!st.empty()) {
        st.pop();
    }
    cycle.clear();

    dfsFindCycle(node);

//    cout << "New cycle:\n";
//    for(auto it : cycle) {
//        cout << it << ' ';
//    }
//
//    cout << '\n' << '\n';

    for (int vert : cycle) {
        CalcDpTree(vert);
    }

    if ((int) cycle.size() == 1) {
        keeps += max(dpTree[cycle[0]][0], dpTree[cycle[0]][1]);
        return;
    }

    reverse(cycle.begin(), cycle.end());

    int maxKeeps = 0;

    ///we do not match the first node in the cycle with the last one
    dpCycle[cycle[0]][0] = dpTree[cycle[0]][0];
    dpCycle[cycle[0]][1] = dpTree[cycle[0]][1];
    for (int i = 1; i < (int) cycle.size(); i++) {
        int maxPrev = max(dpCycle[cycle[i - 1]][0], dpCycle[cycle[i - 1]][1]);
        dpCycle[cycle[i]][0] = maxPrev + dpTree[cycle[i]][0];
        dpCycle[cycle[i]][1] = max(maxPrev + dpTree[cycle[i]][1], dpCycle[cycle[i - 1]][0] + 1 + dpTree[cycle[i]][0]);
    }
    maxKeeps = max(dpCycle[cycle.back()][0], dpCycle[cycle.back()][1]);

    ///we match the first node in the cycle with the last one
    dpCycle[cycle[0]][0] = dpTree[cycle[0]][0];
    dpCycle[cycle[0]][1] = 0;
    for (int i = 1; i < (int) cycle.size(); i++) {
        int maxPrev = max(dpCycle[cycle[i - 1]][0], dpCycle[cycle[i - 1]][1]);
        dpCycle[cycle[i]][0] = maxPrev + dpTree[cycle[i]][0];

        if (i > 1) {
            dpCycle[cycle[i]][1] = max(maxPrev + dpTree[cycle[i]][1],
                                       dpCycle[cycle[i - 1]][0] + 1 + dpTree[cycle[i]][0]);
        } else {
            dpCycle[cycle[i]][1] = maxPrev + dpTree[cycle[i]][1];
        }
    }
    maxKeeps = max(maxKeeps, dpCycle[cycle.back()][1]);
    maxKeeps = max(maxKeeps, dpCycle[cycle.back()][0] + 1);


    if ((int) cycle.size() == 2) {
        maxKeeps = max(maxKeeps, 2 + dpTree[cycle[0]][0] + dpTree[cycle[1]][0]);
    }

    keeps += maxKeeps;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> N;

    if (N % 2 == 1) {
        cout << -1 << '\n';
        return 0;
    }

    for (int i = 1; i <= N; i++) {
        string s, t;
        cin >> s >> t;

        g[Get(s)].push_back(Get(t));

        bi[Get(s)].push_back(Get(t));
        bi[Get(t)].push_back(Get(s));
    }

    for (int i = 1; i <= N; i++) {
        if (!solvedFor[i]) {
            solve(i);
        }
    }

    cout << N - keeps << '\n';

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 3 ms 5100 KB Output is correct
3 Correct 3 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 3 ms 5100 KB Output is correct
6 Correct 3 ms 5100 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5228 KB Output is correct
12 Correct 3 ms 5100 KB Output is correct
13 Correct 3 ms 5100 KB Output is correct
14 Correct 3 ms 5100 KB Output is correct
15 Correct 4 ms 5100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5100 KB Output is correct
2 Correct 3 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 238 ms 30608 KB Output is correct
5 Correct 226 ms 22692 KB Output is correct
6 Correct 252 ms 31760 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 230 ms 21988 KB Output is correct
2 Correct 229 ms 23568 KB Output is correct
3 Correct 140 ms 21880 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 237 ms 29072 KB Output is correct
6 Correct 223 ms 21776 KB Output is correct
7 Correct 210 ms 22032 KB Output is correct
8 Correct 186 ms 21776 KB Output is correct
9 Correct 180 ms 21904 KB Output is correct
10 Correct 132 ms 22024 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 3 ms 5100 KB Output is correct
13 Correct 3 ms 5100 KB Output is correct
14 Correct 4 ms 5228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 3 ms 5100 KB Output is correct
3 Correct 3 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 3 ms 5100 KB Output is correct
6 Correct 3 ms 5100 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5228 KB Output is correct
12 Correct 3 ms 5100 KB Output is correct
13 Correct 3 ms 5100 KB Output is correct
14 Correct 3 ms 5100 KB Output is correct
15 Correct 4 ms 5100 KB Output is correct
16 Correct 3 ms 5100 KB Output is correct
17 Correct 3 ms 5100 KB Output is correct
18 Correct 4 ms 5100 KB Output is correct
19 Correct 238 ms 30608 KB Output is correct
20 Correct 226 ms 22692 KB Output is correct
21 Correct 252 ms 31760 KB Output is correct
22 Correct 4 ms 5120 KB Output is correct
23 Correct 230 ms 21988 KB Output is correct
24 Correct 229 ms 23568 KB Output is correct
25 Correct 140 ms 21880 KB Output is correct
26 Correct 4 ms 5100 KB Output is correct
27 Correct 237 ms 29072 KB Output is correct
28 Correct 223 ms 21776 KB Output is correct
29 Correct 210 ms 22032 KB Output is correct
30 Correct 186 ms 21776 KB Output is correct
31 Correct 180 ms 21904 KB Output is correct
32 Correct 132 ms 22024 KB Output is correct
33 Correct 4 ms 5100 KB Output is correct
34 Correct 3 ms 5100 KB Output is correct
35 Correct 3 ms 5100 KB Output is correct
36 Correct 4 ms 5228 KB Output is correct
37 Correct 259 ms 22672 KB Output is correct
38 Correct 217 ms 24208 KB Output is correct
39 Correct 212 ms 22672 KB Output is correct
40 Correct 216 ms 22032 KB Output is correct
41 Correct 215 ms 22032 KB Output is correct
42 Correct 219 ms 22416 KB Output is correct
43 Correct 219 ms 22544 KB Output is correct
44 Correct 223 ms 22544 KB Output is correct
45 Correct 229 ms 22448 KB Output is correct
46 Correct 246 ms 22632 KB Output is correct
47 Correct 217 ms 22512 KB Output is correct
48 Correct 213 ms 21776 KB Output is correct
49 Correct 226 ms 23568 KB Output is correct
50 Correct 132 ms 21904 KB Output is correct
51 Correct 4 ms 5100 KB Output is correct
52 Correct 261 ms 29056 KB Output is correct
53 Correct 217 ms 21808 KB Output is correct
54 Correct 210 ms 22032 KB Output is correct
55 Correct 174 ms 21776 KB Output is correct
56 Correct 192 ms 22032 KB Output is correct
57 Correct 137 ms 22024 KB Output is correct
58 Correct 3 ms 5100 KB Output is correct
59 Correct 4 ms 5100 KB Output is correct
60 Correct 3 ms 5100 KB Output is correct
61 Correct 4 ms 5120 KB Output is correct
62 Correct 3 ms 5100 KB Output is correct
63 Correct 4 ms 4980 KB Output is correct
64 Correct 4 ms 5100 KB Output is correct
65 Correct 257 ms 30608 KB Output is correct
66 Correct 254 ms 22600 KB Output is correct
67 Correct 246 ms 31760 KB Output is correct
68 Correct 4 ms 5100 KB Output is correct
69 Correct 3 ms 5228 KB Output is correct
70 Correct 3 ms 5100 KB Output is correct
71 Correct 4 ms 5100 KB Output is correct
72 Correct 4 ms 5100 KB Output is correct
73 Correct 3 ms 5100 KB Output is correct
74 Correct 3 ms 5100 KB Output is correct
75 Correct 4 ms 5100 KB Output is correct
76 Correct 4 ms 5100 KB Output is correct
77 Correct 3 ms 5100 KB Output is correct
78 Correct 3 ms 5100 KB Output is correct
79 Correct 4 ms 5100 KB Output is correct
80 Correct 4 ms 5100 KB Output is correct
81 Correct 4 ms 5100 KB Output is correct
82 Correct 4 ms 5100 KB Output is correct
83 Correct 4 ms 5100 KB Output is correct