답안 #707715

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
707715 2023-03-09T19:29:59 Z Kripton Love Polygon (BOI18_polygon) C++14
100 / 100
87 ms 19136 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
using namespace std;

unordered_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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 1 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 1 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 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 61 ms 11996 KB Output is correct
5 Correct 59 ms 12004 KB Output is correct
6 Correct 62 ms 12016 KB Output is correct
7 Correct 56 ms 10588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 14396 KB Output is correct
2 Correct 76 ms 15320 KB Output is correct
3 Correct 47 ms 11200 KB Output is correct
4 Correct 63 ms 10572 KB Output is correct
5 Correct 76 ms 19096 KB Output is correct
6 Correct 66 ms 13216 KB Output is correct
7 Correct 67 ms 13632 KB Output is correct
8 Correct 65 ms 13236 KB Output is correct
9 Correct 62 ms 12780 KB Output is correct
10 Correct 51 ms 11760 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 1 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 1 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 1 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 61 ms 11996 KB Output is correct
20 Correct 59 ms 12004 KB Output is correct
21 Correct 62 ms 12016 KB Output is correct
22 Correct 56 ms 10588 KB Output is correct
23 Correct 69 ms 14396 KB Output is correct
24 Correct 76 ms 15320 KB Output is correct
25 Correct 47 ms 11200 KB Output is correct
26 Correct 63 ms 10572 KB Output is correct
27 Correct 76 ms 19096 KB Output is correct
28 Correct 66 ms 13216 KB Output is correct
29 Correct 67 ms 13632 KB Output is correct
30 Correct 65 ms 13236 KB Output is correct
31 Correct 62 ms 12780 KB Output is correct
32 Correct 51 ms 11760 KB Output is correct
33 Correct 1 ms 2644 KB Output is correct
34 Correct 1 ms 2644 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 1 ms 2644 KB Output is correct
37 Correct 75 ms 15600 KB Output is correct
38 Correct 73 ms 14248 KB Output is correct
39 Correct 67 ms 14252 KB Output is correct
40 Correct 80 ms 13696 KB Output is correct
41 Correct 70 ms 13664 KB Output is correct
42 Correct 69 ms 14528 KB Output is correct
43 Correct 70 ms 14576 KB Output is correct
44 Correct 69 ms 14696 KB Output is correct
45 Correct 87 ms 14620 KB Output is correct
46 Correct 74 ms 14652 KB Output is correct
47 Correct 67 ms 13360 KB Output is correct
48 Correct 78 ms 14380 KB Output is correct
49 Correct 70 ms 15264 KB Output is correct
50 Correct 46 ms 11200 KB Output is correct
51 Correct 53 ms 10584 KB Output is correct
52 Correct 79 ms 19136 KB Output is correct
53 Correct 73 ms 13240 KB Output is correct
54 Correct 68 ms 13636 KB Output is correct
55 Correct 59 ms 13236 KB Output is correct
56 Correct 63 ms 12764 KB Output is correct
57 Correct 49 ms 11680 KB Output is correct
58 Correct 1 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 1 ms 2644 KB Output is correct
62 Correct 1 ms 2644 KB Output is correct
63 Correct 2 ms 2644 KB Output is correct
64 Correct 1 ms 2644 KB Output is correct
65 Correct 60 ms 12036 KB Output is correct
66 Correct 59 ms 11964 KB Output is correct
67 Correct 61 ms 11968 KB Output is correct
68 Correct 67 ms 10636 KB Output is correct
69 Correct 2 ms 2644 KB Output is correct
70 Correct 1 ms 2644 KB Output is correct
71 Correct 2 ms 2644 KB Output is correct
72 Correct 2 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 1 ms 2644 KB Output is correct
78 Correct 2 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 2 ms 2644 KB Output is correct
82 Correct 1 ms 2644 KB Output is correct
83 Correct 1 ms 2644 KB Output is correct