답안 #856075

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
856075 2023-10-02T16:20:03 Z RiverFlow Love Polygon (BOI18_polygon) C++14
75 / 100
170 ms 25956 KB
#include <bits/stdc++.h>

#define nl "\n"
#define no "NO"
#define yes "YES"
#define fi first
#define se second
#define vec vector
#define task "main"
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())

using namespace std;

template<typename U, typename V> bool maxi(U &a, V b) {
    if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool mini(U &a, V b) {
    if (a > b or a == -1) { a = b; return 1; } return 0;
}

const int N = (int)2e5 + 9;
const int mod = (int)1e9 + 7;

void prepare(); void main_code();

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    const bool MULTITEST = 0; prepare();
    int num_test = 1; if (MULTITEST) cin >> num_test;
    while (num_test--) { main_code(); }
}

void prepare() {};

int n, g[N], deg[N];

vector<int> adj[N];

stack<int> st;

bool on[N], on_cycle[N], vis[N];

void dfs(int u) {
    st.push(u); vis[u] = 1; on[u] = 1;
    int v = g[u];
    if (!vis[v]) {
        dfs(v);
    }
    if (on[v]) {
        while (st.size()) {
            int k = st.top(); on_cycle[k] = 1;
            st.pop(); on[k] = 0; if (k == v) break ;
        }
    }
    if (sz(st) and st.top() == u) st.pop();
    on[u] = 0;
}

int cnt[N], dp[N][2][2];

void main_code() {
    cin >> n; map<string, int> mp;
    if (n % 2 == 1) evoid(-1);
    for(int i = 1, cnt = 0; i <= n; ++i) {
        string x, y; cin >> x >> y;
        if (mp.find(x) == mp.end()) {
            mp[x] = ++cnt;
        }
        if (mp.find(y) == mp.end()) {
            mp[y] = ++cnt;
        }
        int a = mp[x], b = mp[y];
        g[a] = b;
    }
    FOR(i, 1, n) if (!vis[i]) dfs(i);

    FOR(i, 1, n) {
        ++deg[g[i]];
    }

    queue<int> q;
    int res = 0;
    FOR(i, 1, n) if (deg[i] == 0) {
        q.push(i);
    }
    vec<bool> c(n + 1, 0);
    while (!q.empty()) {
        int u = q.front(); q.pop();
        --deg[g[u]]; if (deg[g[u]] == 0) q.push(g[u]);
        if (!c[u]) {
            if (on_cycle[g[u]]) {
                ++cnt[g[u]];
                continue ;
            }
            ++res; if (!c[g[u]] and on_cycle[g[u]] == 0) c[g[u]] = 1;
        }
    }
    vec<bool> mark(n + 1, 0);
    FOR(i, 1, n) if (on_cycle[i] and !mark[i]) {
        vec<int> cy;
        cy.push_back(i);
        for(int j = g[i]; j != i; j = g[j]) {
            cy.push_back(j);
        }
        for(int k : cy) {
            if (cnt[k] > 1) {
                res += cnt[k] - 1;
                cnt[k] = 1;
            }
            mark[k] = 1;
        }
        if (sz(cy) == 1) {
            ++res; continue ;
        }
        if (sz(cy) == 2) {
            for(int k : cy) res += cnt[k];
            continue ;
        }

        int m = sz(cy);

        FOR(i, 0, m) FOR(j, 0, 1) FOR(k, 0, 1) dp[i][j][k] = n;

        dp[0][0][0] = 1;
        dp[0][0][1] = cnt[ cy[0] ];
        dp[0][1][0] = cnt[ cy[0] ] + 1;

        FOR(i, 0, m - 3) FOR(j, 0, 1) FOR(k, 0, 1) {
            if (k == 0) {
                // no match (i, i + 1)
                mini(dp[i + 1][j][0], dp[i][j][k] + 1);
                mini(dp[i + 1][j][1], dp[i][j][k] + cnt[ cy[i + 1] ]);
            } else {
                mini(dp[i + 1][j][0], dp[i][j][k] + cnt[ cy[i + 1] ] + 1);
            }
        }

        // match (0, m - 1)
        mini(dp[m - 1][1][1], dp[m - 2][1][0] + cnt[ cy[m - 1] ]);

        // match (m - 1, m - 2)
        mini(dp[m - 1][0][0], dp[m - 2][0][0] + cnt[ cy[m - 1] ] + 1);

        // match (m - 1, it'child)
        mini(dp[m - 1][0][0], dp[m - 2][0][0] + 1);

        int l = n; FOR(j, 0, 1) FOR(k, 0, 1) l = min(l, dp[m - 1][j][k]);

        res += l;
    }
    cout << res;
}


/*     Let the river flows naturally     */

Compilation message

polygon.cpp: In function 'int main()':
polygon.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
polygon.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6492 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Correct 2 ms 6620 KB Output is correct
4 Correct 1 ms 6488 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 1 ms 6496 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 2 ms 6492 KB Output is correct
9 Correct 2 ms 6620 KB Output is correct
10 Correct 2 ms 6488 KB Output is correct
11 Correct 1 ms 6632 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 2 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 2 ms 6644 KB Output is correct
3 Correct 1 ms 6624 KB Output is correct
4 Correct 147 ms 25076 KB Output is correct
5 Correct 170 ms 16752 KB Output is correct
6 Correct 152 ms 25956 KB Output is correct
7 Correct 2 ms 6488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 148 ms 17140 KB Output is correct
2 Correct 143 ms 17844 KB Output is correct
3 Correct 125 ms 16596 KB Output is correct
4 Correct 1 ms 6488 KB Output is correct
5 Correct 168 ms 19164 KB Output is correct
6 Correct 139 ms 17048 KB Output is correct
7 Correct 137 ms 17232 KB Output is correct
8 Correct 125 ms 17236 KB Output is correct
9 Correct 124 ms 16792 KB Output is correct
10 Correct 125 ms 17240 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 2 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6492 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Correct 2 ms 6620 KB Output is correct
4 Correct 1 ms 6488 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 1 ms 6496 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 2 ms 6492 KB Output is correct
9 Correct 2 ms 6620 KB Output is correct
10 Correct 2 ms 6488 KB Output is correct
11 Correct 1 ms 6632 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 2 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 2 ms 6644 KB Output is correct
18 Correct 1 ms 6624 KB Output is correct
19 Correct 147 ms 25076 KB Output is correct
20 Correct 170 ms 16752 KB Output is correct
21 Correct 152 ms 25956 KB Output is correct
22 Correct 2 ms 6488 KB Output is correct
23 Correct 148 ms 17140 KB Output is correct
24 Correct 143 ms 17844 KB Output is correct
25 Correct 125 ms 16596 KB Output is correct
26 Correct 1 ms 6488 KB Output is correct
27 Correct 168 ms 19164 KB Output is correct
28 Correct 139 ms 17048 KB Output is correct
29 Correct 137 ms 17232 KB Output is correct
30 Correct 125 ms 17236 KB Output is correct
31 Correct 124 ms 16792 KB Output is correct
32 Correct 125 ms 17240 KB Output is correct
33 Correct 1 ms 6492 KB Output is correct
34 Correct 1 ms 6492 KB Output is correct
35 Correct 1 ms 6492 KB Output is correct
36 Correct 2 ms 6492 KB Output is correct
37 Correct 140 ms 17156 KB Output is correct
38 Incorrect 137 ms 20560 KB Output isn't correct
39 Halted 0 ms 0 KB -