Submission #467842

# Submission time Handle Problem Language Result Execution time Memory
467842 2021-08-24T23:15:12 Z Ozy Love Polygon (BOI18_polygon) C++17
29 / 100
272 ms 25336 KB
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define repa(i,a,b) for(int i = (a); i >= (b); i--)
#define lli long long int
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " << a << ", "

#define MAX 100000

pair<lli,lli> ult;
lli n,a,b,cont,res,cant;
lli dp[2][MAX+2],dir[MAX+2],visitados[MAX+2];
string A,B;
map<string, lli> mapa;
vector<lli> hijos[MAX+2];

void dfs(lli pos, lli marca) {

    cant++;
    visitados[pos] = marca;
    dp[0][pos] = 0;
    dp[1][pos] = 0;

    lli MIN = INT_MAX;
    lli sum = 0;
    lli x;

    bool tengoH = false;
    for(auto h : hijos[pos]) {
        if (visitados[h] == marca) continue;

        tengoH = true;
        dfs(h,marca);

        sum += dp[1][h];
        x = dp[0][h] - dp[1][h];
        if (x < MIN) MIN = x;
    }

    if (!tengoH) {
        dp[0][pos] = 0;
        dp[1][pos] = 1;
        return;
    }

    dp[0][pos] = sum;

    sum += MIN;
    dp[1][pos] = sum + 1;

}

pair<lli,lli> busca(lli pos) {
    while (visitados[pos] == 0) {
        visitados[pos] = 1;
        if (visitados[dir[pos]] == 1) {
            if (pos != dir[pos] && dir[dir[pos]] == pos) res--;
            return {pos,dir[pos]};
        }
        pos = dir[pos];
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n;

    cont = 1;

    rep(i,1,n) {
        cin >> A >> B;

        if (mapa.find(A) == mapa.end()) mapa[A] = cont++;
        if (mapa.find(B) == mapa.end()) mapa[B] = cont++;
        a = mapa[A];
        b = mapa[B];

        dir[a] = b;

        hijos[b].push_back(a);
        hijos[a].push_back(b);
    }
    if (n&1) {
        cout << "-1";
        return 0;
    }

    res = 0;
    rep(i,1,n) if (visitados[i] == 0) {

        ult = busca(i);

        cant = 0;
        dfs(ult.first, 2);
        a = dp[1][ult.first];
        if (cant == 2 && ult.first != ult.second) continue;

        if (ult.second != ult.first) {
            dfs(ult.second, 3);
            a = min(a, dp[1][ult.second]);
        }

        res += a;
    }

    cout << res;
}

Compilation message

polygon.cpp: In function 'std::pair<long long int, long long int> busca(long long int)':
polygon.cpp:64:1: warning: control reaches end of non-void function [-Wreturn-type]
   64 | }
      | ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2576 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Incorrect 2 ms 2636 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Incorrect 2 ms 2636 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 264 ms 17164 KB Output is correct
2 Correct 272 ms 18996 KB Output is correct
3 Correct 187 ms 16684 KB Output is correct
4 Correct 249 ms 15008 KB Output is correct
5 Correct 271 ms 25336 KB Output is correct
6 Correct 249 ms 17260 KB Output is correct
7 Correct 251 ms 17348 KB Output is correct
8 Correct 226 ms 17124 KB Output is correct
9 Correct 254 ms 17476 KB Output is correct
10 Correct 173 ms 17616 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2576 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Incorrect 2 ms 2636 KB Output isn't correct
11 Halted 0 ms 0 KB -