답안 #924677

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
924677 2024-02-09T12:33:50 Z aykhn Love Polygon (BOI18_polygon) C++17
0 / 100
915 ms 1048576 KB
#include <bits/stdc++.h>
// author: aykhn
using namespace std;
#define int long long
#define inf 0x3F3F3F3F

const int MXN = 1e5 + 5;

int n;
vector<int> adj[MXN], g[MXN];
map<string, int> mp;
string name[MXN];
int to[MXN], dp[MXN][2];
int col[MXN], in[MXN], mark[MXN], p[MXN];
vector<int> id[MXN];
int idx = 0;

int cyc(int a)
{
    col[a] = 1;
    for (int v : g[a])
    {
        if (col[v] == 2) 
        {
            continue;
        }
        if (col[v] == 1)
        {
            idx++;
            int s = a;
            int e = v;
            while (1)
            {
                in[s] = 1;
                id[idx].push_back(s);
                if (s == e) break;
                s = p[s];
            }
            return 1;
        }
        else
        {
            p[v] = a;
            if (cyc(v)) 
            {
                col[a] = 2;
                return 1;
            }
        }
    }
    col[a] = 2;
    return 0;
}

void dfs(int a)
{
    if (adj[a].empty())
    {
        dp[a][0] = dp[a][1] = 1;
        return;
    }
    dp[a][0] = 0;
    dp[a][1] = inf;
    for (int v : adj[a])
    {
        dfs(v);
        dp[a][0] += min(dp[v][0], dp[v][1]);
    }
    for (int v : adj[a])
    {
        dp[a][1] = min(dp[a][1], dp[a][0] - min(dp[v][0], dp[v][1]) + dp[v][0] - 1);
    }
    dp[a][0]++;
    assert(dp[a][0] >= dp[a][1]);
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    int n, m = 0, cnt = 0;
    cin >> n;
    if (n & 1)
    {
        cout << -1 << '\n';
        return 0;
    }
    for (int i = 1; i <= n; i++)
    {
        string a, b;
        cin >> a >> b;
        if (mp.find(a) == mp.end()) mp[a] = ++m, name[m] = a;
        if (mp.find(b) == mp.end()) mp[b] = ++m, name[m] = b;
        to[mp[a]] = mp[b];
    }
    vector<int> r;
    int res = 0;
    for (int i = 1; i <= m; i++)
    {
        if (to[to[i]] == i) 
        {
            res--;
            mark[i] = 1, mark[to[i]] = 1;
            continue;
        }
        if (to[to[to[i]]] == to[i]) to[i] = i;
        g[i].push_back(to[i]);
    }
    for (int i = 1; i <= m; i++)
    {
        if (mark[i]) continue;
        if (!col[i]) 
        {
            p[i] = i;
            cyc(i);
        }
    }
    for (int i = 1; i <= m; i++)
    {
        if (mark[i]) continue;
        if (!(in[i] && in[to[i]])) 
        {
            adj[to[i]].push_back(i);
        }
    }
    for (int i = 1; i <= m; i++)
    {
        if (in[i]) dfs(i);
    }
    for (int c = 1; c <= idx; c++)
    {
        if (id[c].size() == 1)
        {
            res += dp[id[c][0]][1];
            continue;
        }
        int ans = 0, sz = (int)id[c].size(), mn = 0;
        for (int x : id[c]) ans += dp[x][1];
        vector<array<int, 2>> dpp(id[c].size(), {inf, inf}), dps(id[c].size(), {inf, inf});
        dpp[0][1] = dp[id[c][0]][0] - dp[id[c][1]][1];
        dpp[0][0] = 0;
        for (int i = 1; i < id[c].size(); i++)
        {
            dpp[i][0] = min(0LL, dpp[i - 1][1] + dp[id[c][i]][0] - dp[id[c][i]][1] - 2);
            dpp[i][1] = dpp[i - 1][0] + dp[id[c][i]][0] - dp[id[c][i]][1];
            mn = min(mn, dp[i][0]);
        }
        dps[sz - 1][1] = dp[id[c][sz - 1]][0] - dp[id[c][sz - 1]][1];
        for (int i = sz - 1; i >= 0; i--)
        {
            dps[i][0] = min(0LL, dps[i + 1][1] + dp[id[c][i]][0] - dp[id[c][i]][1] - 2);
            dps[i][1] = dps[i + 1][0] + dp[id[c][i]][0] - dp[id[c][i]][1];
        }
    }
}

Compilation message

polygon.cpp: In function 'int main()':
polygon.cpp:142:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  142 |         for (int i = 1; i < id[c].size(); i++)
      |                         ~~^~~~~~~~~~~~~~
polygon.cpp:81:19: warning: unused variable 'cnt' [-Wunused-variable]
   81 |     int n, m = 0, cnt = 0;
      |                   ^~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 13912 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 13916 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 915 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 13912 KB Output isn't correct
2 Halted 0 ms 0 KB -