답안 #482467

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
482467 2021-10-24T15:51:25 Z SamAnd Love Polygon (BOI18_polygon) C++17
100 / 100
238 ms 29824 KB
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
const int N = 100005;
 
int n;
pair<string, string> kk[N];
map<string, int> mp;
int h[N];
vector<int> a[N];
 
vector<vector<int> > cycles;
 
int c[N];
int s, f;
int p[N];
bool dfs0(int x)
{
    c[x] = 1;
    if (c[h[x]] == 1)
    {
        s = h[x];
        f = x;
        c[x] = 2;
        return true;
    }
    if (c[h[x]] == 0)
    {
        p[h[x]] = x;
        if (dfs0(h[x]))
        {
            c[x] = 2;
            return true;
        }
    }
    c[x] = 2;
    return false;
}
 
int dp[N][2];
void dfs(int x)
{
    for (int i = 0; i < a[x].size(); ++i)
    {
        int h = a[x][i];
        if (c[h])
            continue;
        dfs(h);
        dp[x][0] += (min(dp[h][0], dp[h][1]) + 1);
    }
    dp[x][1] = N;
    for (int i = 0; i < a[x].size(); ++i)
    {
        int h = a[x][i];
        if (c[h])
            continue;
        dp[x][0] -= (min(dp[h][0], dp[h][1]) + 1);
        dp[x][1] = min(dp[x][1], dp[h][0] + dp[x][0]);
        dp[x][0] += (min(dp[h][0], dp[h][1]) + 1);
    }
}
 
int dp1[N][2];
int solv(const vector<int>& v)
{
    if (v.size() == 2)
    {
        return min(dp[v[0]][0] + dp[v[1]][0],
                   dp[v[0]][1] + dp[v[1]][1] + 2);
    }
    dp1[v[0]][0] = dp[v[0]][0];
    dp1[v[0]][1] = dp[v[0]][1];
    for (int i = 1; i < v.size(); ++i)
    {
        dp1[v[i]][0] = min(dp1[v[i - 1]][0], dp1[v[i - 1]][1]) + 1 + dp[v[i]][0];
        dp1[v[i]][1] = min(min(dp1[v[i - 1]][0], dp1[v[i - 1]][1]) + 1 + dp[v[i]][1],
                           dp1[v[i - 1]][0] + dp[v[i]][0]);
    }
    return min(dp1[v.back()][0], dp1[v.back()][1]) + 1;
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin >> n;
    for (int i = 1; i <= n; ++i)
    {
        cin >> kk[i].first >> kk[i].second;
        mp[kk[i].first] = i;
    }
    if (n % 2 == 1)
    {
        cout << -1 << endl;
        return 0;
    }
    for (int i = 1; i <= n; ++i)
    {
        h[i] = mp[kk[i].second];
        a[mp[kk[i].second]].push_back(mp[kk[i].first]);
    }
    for (int i = 1; i <= n; ++i)
    {
        if (!c[i])
        {
            if (dfs0(i))
            {
                vector<int> v;
                for (int x = f; x != s; x = p[x])
                    v.push_back(x);
                v.push_back(s);
                reverse(v.begin(), v.end());
                cycles.push_back(v);
            }
        }
    }
    memset(c, 0, sizeof c);
    for (int i = 0; i < cycles.size(); ++i)
    {
        for (int j = 0; j < cycles[i].size(); ++j)
        {
            int x = cycles[i][j];
            c[x] = 1;
        }
    }
    for (int x = 1; x <= n; ++x)
    {
        if (c[x])
            dfs(x);
    }
    int ans = 0;
    for (int i = 0; i < cycles.size(); ++i)
    {
        int yans = solv(cycles[i]);
        int x = cycles[i].back();
        cycles[i].pop_back();
        reverse(cycles[i].begin(), cycles[i].end());
        cycles[i].push_back(x);
        reverse(cycles[i].begin(), cycles[i].end());
        yans = min(yans, solv(cycles[i]));
        ans += yans;
    }
    cout << ans << endl;
    return 0;
}

Compilation message

polygon.cpp: In function 'void dfs(int)':
polygon.cpp:43:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |     for (int i = 0; i < a[x].size(); ++i)
      |                     ~~^~~~~~~~~~~~~
polygon.cpp:52:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |     for (int i = 0; i < a[x].size(); ++i)
      |                     ~~^~~~~~~~~~~~~
polygon.cpp: In function 'int solv(const std::vector<int>&)':
polygon.cpp:73:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |     for (int i = 1; i < v.size(); ++i)
      |                     ~~^~~~~~~~~~
polygon.cpp: In function 'int main()':
polygon.cpp:117:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |     for (int i = 0; i < cycles.size(); ++i)
      |                     ~~^~~~~~~~~~~~~~~
polygon.cpp:119:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  119 |         for (int j = 0; j < cycles[i].size(); ++j)
      |                         ~~^~~~~~~~~~~~~~~~~~
polygon.cpp:131:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  131 |     for (int i = 0; i < cycles.size(); ++i)
      |                     ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9292 KB Output is correct
2 Correct 5 ms 9292 KB Output is correct
3 Correct 5 ms 9292 KB Output is correct
4 Correct 5 ms 9268 KB Output is correct
5 Correct 4 ms 8932 KB Output is correct
6 Correct 4 ms 8908 KB Output is correct
7 Correct 5 ms 9292 KB Output is correct
8 Correct 5 ms 9292 KB Output is correct
9 Correct 5 ms 9292 KB Output is correct
10 Correct 5 ms 9320 KB Output is correct
11 Correct 5 ms 9292 KB Output is correct
12 Correct 5 ms 9316 KB Output is correct
13 Correct 5 ms 9292 KB Output is correct
14 Correct 5 ms 9292 KB Output is correct
15 Correct 5 ms 9292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9292 KB Output is correct
2 Correct 5 ms 9292 KB Output is correct
3 Correct 7 ms 9292 KB Output is correct
4 Correct 198 ms 27832 KB Output is correct
5 Correct 238 ms 25900 KB Output is correct
6 Correct 224 ms 28456 KB Output is correct
7 Correct 77 ms 18880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 195 ms 23960 KB Output is correct
2 Correct 204 ms 24772 KB Output is correct
3 Correct 168 ms 29824 KB Output is correct
4 Correct 68 ms 18884 KB Output is correct
5 Correct 202 ms 29772 KB Output is correct
6 Correct 197 ms 22444 KB Output is correct
7 Correct 207 ms 23344 KB Output is correct
8 Correct 182 ms 26336 KB Output is correct
9 Correct 180 ms 21960 KB Output is correct
10 Correct 146 ms 21004 KB Output is correct
11 Correct 6 ms 9292 KB Output is correct
12 Correct 4 ms 9320 KB Output is correct
13 Correct 5 ms 9320 KB Output is correct
14 Correct 5 ms 9320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9292 KB Output is correct
2 Correct 5 ms 9292 KB Output is correct
3 Correct 5 ms 9292 KB Output is correct
4 Correct 5 ms 9268 KB Output is correct
5 Correct 4 ms 8932 KB Output is correct
6 Correct 4 ms 8908 KB Output is correct
7 Correct 5 ms 9292 KB Output is correct
8 Correct 5 ms 9292 KB Output is correct
9 Correct 5 ms 9292 KB Output is correct
10 Correct 5 ms 9320 KB Output is correct
11 Correct 5 ms 9292 KB Output is correct
12 Correct 5 ms 9316 KB Output is correct
13 Correct 5 ms 9292 KB Output is correct
14 Correct 5 ms 9292 KB Output is correct
15 Correct 5 ms 9292 KB Output is correct
16 Correct 5 ms 9292 KB Output is correct
17 Correct 5 ms 9292 KB Output is correct
18 Correct 7 ms 9292 KB Output is correct
19 Correct 198 ms 27832 KB Output is correct
20 Correct 238 ms 25900 KB Output is correct
21 Correct 224 ms 28456 KB Output is correct
22 Correct 77 ms 18880 KB Output is correct
23 Correct 195 ms 23960 KB Output is correct
24 Correct 204 ms 24772 KB Output is correct
25 Correct 168 ms 29824 KB Output is correct
26 Correct 68 ms 18884 KB Output is correct
27 Correct 202 ms 29772 KB Output is correct
28 Correct 197 ms 22444 KB Output is correct
29 Correct 207 ms 23344 KB Output is correct
30 Correct 182 ms 26336 KB Output is correct
31 Correct 180 ms 21960 KB Output is correct
32 Correct 146 ms 21004 KB Output is correct
33 Correct 6 ms 9292 KB Output is correct
34 Correct 4 ms 9320 KB Output is correct
35 Correct 5 ms 9320 KB Output is correct
36 Correct 5 ms 9320 KB Output is correct
37 Correct 193 ms 24384 KB Output is correct
38 Correct 199 ms 24044 KB Output is correct
39 Correct 201 ms 23804 KB Output is correct
40 Correct 196 ms 22680 KB Output is correct
41 Correct 193 ms 22772 KB Output is correct
42 Correct 214 ms 23364 KB Output is correct
43 Correct 195 ms 23488 KB Output is correct
44 Correct 197 ms 23492 KB Output is correct
45 Correct 208 ms 23568 KB Output is correct
46 Correct 201 ms 23492 KB Output is correct
47 Correct 186 ms 22728 KB Output is correct
48 Correct 206 ms 24132 KB Output is correct
49 Correct 201 ms 24900 KB Output is correct
50 Correct 172 ms 29796 KB Output is correct
51 Correct 74 ms 18808 KB Output is correct
52 Correct 218 ms 29764 KB Output is correct
53 Correct 194 ms 22472 KB Output is correct
54 Correct 206 ms 23248 KB Output is correct
55 Correct 198 ms 26264 KB Output is correct
56 Correct 190 ms 21912 KB Output is correct
57 Correct 142 ms 20964 KB Output is correct
58 Correct 5 ms 9292 KB Output is correct
59 Correct 5 ms 9320 KB Output is correct
60 Correct 5 ms 9292 KB Output is correct
61 Correct 5 ms 9320 KB Output is correct
62 Correct 5 ms 9292 KB Output is correct
63 Correct 5 ms 9292 KB Output is correct
64 Correct 5 ms 9292 KB Output is correct
65 Correct 202 ms 27976 KB Output is correct
66 Correct 196 ms 25896 KB Output is correct
67 Correct 197 ms 28392 KB Output is correct
68 Correct 76 ms 18824 KB Output is correct
69 Correct 6 ms 9292 KB Output is correct
70 Correct 5 ms 9320 KB Output is correct
71 Correct 4 ms 9292 KB Output is correct
72 Correct 5 ms 9292 KB Output is correct
73 Correct 4 ms 8908 KB Output is correct
74 Correct 4 ms 8908 KB Output is correct
75 Correct 5 ms 9292 KB Output is correct
76 Correct 6 ms 9316 KB Output is correct
77 Correct 5 ms 9292 KB Output is correct
78 Correct 5 ms 9316 KB Output is correct
79 Correct 5 ms 9292 KB Output is correct
80 Correct 5 ms 9292 KB Output is correct
81 Correct 5 ms 9292 KB Output is correct
82 Correct 5 ms 9292 KB Output is correct
83 Correct 5 ms 9292 KB Output is correct