Submission #707714

# Submission time Handle Problem Language Result Execution time Memory
707714 2023-03-09T19:29:36 Z Kripton Love Polygon (BOI18_polygon) C++14
100 / 100
207 ms 19408 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
using namespace std;

map <string, int> cod;
string s1, s2;
vector <int> fii[100001];
int urm[100001], next_cyc[100001];
bool viz[100001], verif[100001], done[100001];
int dp[100001][2], dp2[100001][2];

void dfs(int nod)
{
    for(auto it:fii[nod])
        dfs(it);
    int max1 = -1e9;
    for(auto it:fii[nod])
    {
        dp[nod][0] += max(dp[it][0], dp[it][1]);
        dp[nod][1] += max(dp[it][0], dp[it][1]);
        max1 = max(max1, dp[it][0] - max(dp[it][0], dp[it][1]));
    }
    if(fii[nod].size())
        dp[nod][1] += 1 + max1;
}

int main()
{
#ifdef HOME
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
#endif // HOME
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n, cnt = 0;
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> s1 >> s2;
        if(!cod[s1])
            cod[s1] = ++cnt;
        if(!cod[s2])
            cod[s2] = ++cnt;
        urm[cod[s1]] = cod[s2];
    }
    if(n % 2)
    {
        cout << -1;
        return 0;
    }
    for(int i = 1; i <= n; i++)
        if(!viz[i])
        {
            int x = i;
            do{
                viz[x] = 1;
                x = urm[x];
            }while(!viz[x]);
            if(verif[x] || next_cyc[x])
            {
                int x = i;
                while(!verif[x] && !next_cyc[x])
                {
                    verif[x] = 1;
                    x = urm[x];
                }
            }
            else if(!next_cyc[x])
            {
                int cx = x;
                do{
                    next_cyc[x] = urm[x];
                    x = urm[x];
                }while(x != cx);
                int x = i;
                while(!next_cyc[x])
                {
                    verif[x] = 1;
                    x = urm[x];
                }
            }
        }
    for(int i = 1; i <= n; i++)
        if(!next_cyc[i])
            fii[urm[i]].push_back(i);
    for(int i = 1; i <= n; i++)
        if(next_cyc[i])
            dfs(i);
    int rez = 0;
    for(int i = 1; i <= n; i++)
        if(next_cyc[i] && !done[i])
        {
            if(next_cyc[i] == i)
            {
                done[i] = 1;
                rez += max(dp[i][0], dp[i][1]);
                continue;
            }
            int max1;
            ///leg i si urm[i]
            if(urm[urm[i]] == i)
                max1 = dp[i][0] + dp[urm[i]][0] + 2;
            else
            {
                dp2[urm[urm[i]]][0] = dp[urm[urm[i]]][0];
                dp2[urm[urm[i]]][1] = dp[urm[urm[i]]][1];
                for(int j = urm[urm[i]]; j != i; j = urm[j])
                {
                    dp2[urm[j]][0] = dp[urm[j]][0] + max(dp2[j][0], dp2[j][1]);
                    dp2[urm[j]][1] = max(dp[urm[j]][1] + max(dp2[j][0], dp2[j][1]), dp[urm[j]][0] + dp2[j][0] + 1);
                }
                max1 = dp2[i][0] + dp[urm[i]][0] + 1;
            }
            ///nu ii leg
            dp2[urm[i]][0] = dp[urm[i]][0];
            dp2[urm[i]][1] = dp[urm[i]][1];
            for(int j = urm[i]; j != i; j = urm[j])
            {
                dp2[urm[j]][0] = dp[urm[j]][0] + max(dp2[j][0], dp2[j][1]);
                dp2[urm[j]][1] = max(dp[urm[j]][1] + max(dp2[j][0], dp2[j][1]), dp[urm[j]][0] + dp2[j][0] + 1);
            }
            if(urm[urm[i]] != i)
                rez += max(max1, max(dp2[i][0], dp2[i][1]));
            else
                rez += max(max1, max(dp[i][0], dp[i][1]) + max(dp[urm[i]][0], dp[urm[i]][1]));
            int x = i;
            while(!done[x])
            {
                done[x] = 1;
                x = urm[x];
            }
        }
    cout << n - rez;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2608 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 175 ms 12208 KB Output is correct
5 Correct 196 ms 12488 KB Output is correct
6 Correct 183 ms 12164 KB Output is correct
7 Correct 150 ms 10828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 171 ms 14648 KB Output is correct
2 Correct 207 ms 15508 KB Output is correct
3 Correct 147 ms 11420 KB Output is correct
4 Correct 153 ms 10772 KB Output is correct
5 Correct 190 ms 19380 KB Output is correct
6 Correct 163 ms 13404 KB Output is correct
7 Correct 166 ms 13872 KB Output is correct
8 Correct 200 ms 13456 KB Output is correct
9 Correct 155 ms 12956 KB Output is correct
10 Correct 126 ms 11624 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 1 ms 2644 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2608 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 1 ms 2644 KB Output is correct
18 Correct 1 ms 2644 KB Output is correct
19 Correct 175 ms 12208 KB Output is correct
20 Correct 196 ms 12488 KB Output is correct
21 Correct 183 ms 12164 KB Output is correct
22 Correct 150 ms 10828 KB Output is correct
23 Correct 171 ms 14648 KB Output is correct
24 Correct 207 ms 15508 KB Output is correct
25 Correct 147 ms 11420 KB Output is correct
26 Correct 153 ms 10772 KB Output is correct
27 Correct 190 ms 19380 KB Output is correct
28 Correct 163 ms 13404 KB Output is correct
29 Correct 166 ms 13872 KB Output is correct
30 Correct 200 ms 13456 KB Output is correct
31 Correct 155 ms 12956 KB Output is correct
32 Correct 126 ms 11624 KB Output is correct
33 Correct 1 ms 2644 KB Output is correct
34 Correct 1 ms 2644 KB Output is correct
35 Correct 1 ms 2644 KB Output is correct
36 Correct 2 ms 2644 KB Output is correct
37 Correct 179 ms 15912 KB Output is correct
38 Correct 185 ms 14492 KB Output is correct
39 Correct 174 ms 14456 KB Output is correct
40 Correct 166 ms 13900 KB Output is correct
41 Correct 172 ms 13832 KB Output is correct
42 Correct 195 ms 14776 KB Output is correct
43 Correct 171 ms 14796 KB Output is correct
44 Correct 173 ms 14972 KB Output is correct
45 Correct 170 ms 14776 KB Output is correct
46 Correct 166 ms 14912 KB Output is correct
47 Correct 155 ms 13612 KB Output is correct
48 Correct 171 ms 14592 KB Output is correct
49 Correct 192 ms 15472 KB Output is correct
50 Correct 138 ms 11464 KB Output is correct
51 Correct 151 ms 10848 KB Output is correct
52 Correct 178 ms 19408 KB Output is correct
53 Correct 182 ms 13476 KB Output is correct
54 Correct 166 ms 13856 KB Output is correct
55 Correct 158 ms 13472 KB Output is correct
56 Correct 165 ms 12956 KB Output is correct
57 Correct 129 ms 11760 KB Output is correct
58 Correct 2 ms 2644 KB Output is correct
59 Correct 1 ms 2644 KB Output is correct
60 Correct 1 ms 2644 KB Output is correct
61 Correct 2 ms 2644 KB Output is correct
62 Correct 1 ms 2644 KB Output is correct
63 Correct 1 ms 2644 KB Output is correct
64 Correct 2 ms 2644 KB Output is correct
65 Correct 173 ms 12236 KB Output is correct
66 Correct 159 ms 12236 KB Output is correct
67 Correct 161 ms 12128 KB Output is correct
68 Correct 181 ms 10872 KB Output is correct
69 Correct 1 ms 2644 KB Output is correct
70 Correct 1 ms 2644 KB Output is correct
71 Correct 1 ms 2644 KB Output is correct
72 Correct 1 ms 2644 KB Output is correct
73 Correct 1 ms 2644 KB Output is correct
74 Correct 1 ms 2644 KB Output is correct
75 Correct 1 ms 2644 KB Output is correct
76 Correct 1 ms 2644 KB Output is correct
77 Correct 2 ms 2644 KB Output is correct
78 Correct 1 ms 2644 KB Output is correct
79 Correct 1 ms 2644 KB Output is correct
80 Correct 1 ms 2644 KB Output is correct
81 Correct 1 ms 2644 KB Output is correct
82 Correct 1 ms 2644 KB Output is correct
83 Correct 2 ms 2644 KB Output is correct