답안 #707713

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
707713 2023-03-09T19:26:03 Z Kripton Love Polygon (BOI18_polygon) C++14
100 / 100
122 ms 21208 KB
#include <bits/stdc++.h>
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
    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 2652 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 2 ms 2656 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2656 KB Output is correct
11 Correct 2 ms 2652 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 2 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 108 ms 11972 KB Output is correct
5 Correct 112 ms 11984 KB Output is correct
6 Correct 104 ms 12016 KB Output is correct
7 Correct 104 ms 10612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 110 ms 14380 KB Output is correct
2 Correct 114 ms 15304 KB Output is correct
3 Correct 95 ms 11216 KB Output is correct
4 Correct 103 ms 10548 KB Output is correct
5 Correct 119 ms 19112 KB Output is correct
6 Correct 116 ms 13240 KB Output is correct
7 Correct 112 ms 13596 KB Output is correct
8 Correct 107 ms 13272 KB Output is correct
9 Correct 115 ms 12816 KB Output is correct
10 Correct 93 ms 11708 KB Output is correct
11 Correct 2 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 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 2652 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 2 ms 2656 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2656 KB Output is correct
11 Correct 2 ms 2652 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 2 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 108 ms 11972 KB Output is correct
20 Correct 112 ms 11984 KB Output is correct
21 Correct 104 ms 12016 KB Output is correct
22 Correct 104 ms 10612 KB Output is correct
23 Correct 110 ms 14380 KB Output is correct
24 Correct 114 ms 15304 KB Output is correct
25 Correct 95 ms 11216 KB Output is correct
26 Correct 103 ms 10548 KB Output is correct
27 Correct 119 ms 19112 KB Output is correct
28 Correct 116 ms 13240 KB Output is correct
29 Correct 112 ms 13596 KB Output is correct
30 Correct 107 ms 13272 KB Output is correct
31 Correct 115 ms 12816 KB Output is correct
32 Correct 93 ms 11708 KB Output is correct
33 Correct 2 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 1 ms 2644 KB Output is correct
37 Correct 116 ms 17744 KB Output is correct
38 Correct 115 ms 16576 KB Output is correct
39 Correct 118 ms 16484 KB Output is correct
40 Correct 114 ms 15792 KB Output is correct
41 Correct 111 ms 15784 KB Output is correct
42 Correct 118 ms 16636 KB Output is correct
43 Correct 119 ms 16928 KB Output is correct
44 Correct 116 ms 16888 KB Output is correct
45 Correct 115 ms 16764 KB Output is correct
46 Correct 112 ms 16876 KB Output is correct
47 Correct 109 ms 15508 KB Output is correct
48 Correct 115 ms 16572 KB Output is correct
49 Correct 115 ms 17388 KB Output is correct
50 Correct 97 ms 13296 KB Output is correct
51 Correct 105 ms 12768 KB Output is correct
52 Correct 115 ms 21208 KB Output is correct
53 Correct 115 ms 15316 KB Output is correct
54 Correct 113 ms 15780 KB Output is correct
55 Correct 106 ms 15392 KB Output is correct
56 Correct 122 ms 14848 KB Output is correct
57 Correct 96 ms 13920 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 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 2656 KB Output is correct
65 Correct 108 ms 14168 KB Output is correct
66 Correct 106 ms 14172 KB Output is correct
67 Correct 121 ms 14188 KB Output is correct
68 Correct 98 ms 12748 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 2 ms 2644 KB Output is correct
74 Correct 1 ms 2644 KB Output is correct
75 Correct 2 ms 2688 KB Output is correct
76 Correct 1 ms 2644 KB Output is correct
77 Correct 2 ms 2648 KB Output is correct
78 Correct 2 ms 2660 KB Output is correct
79 Correct 2 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 2656 KB Output is correct