답안 #468022

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
468022 2021-08-25T22:31:06 Z Ozy Love Polygon (BOI18_polygon) C++17
75 / 100
335 ms 28268 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;
bool casoEsp;
map<string, lli> mapa;
vector<lli> hijos[MAX+2];

void dfs(lli pos, lli marca) {

    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) {

    casoEsp = false;
    while (visitados[pos] == 0) {
        visitados[pos] = 1;
        if (visitados[dir[pos]] == 1) {
            if (pos != dir[pos] && dir[dir[pos]] == pos) casoEsp = true;
            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);

        if(!casoEsp) {
            dfs(ult.first, 2);
            a = dp[1][ult.first];

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

            res += a;
        }
        else {

            visitados[ult.first] = 2;
            visitados[ult.second] = 2;

            for (auto h : hijos[ult.first]){
                if (h == ult.first || h == ult.second) continue;
                dfs(h, 2);
                a = dp[1][h];
                res += a;
            }
            for (auto h : hijos[ult.second]){
                if (h == ult.first || h == ult.second) continue;
                dfs(h, 2);
                a = dp[1][h];
                res += a;
            }

        }
    }

    cout << res;
}

Compilation message

polygon.cpp: In function 'std::pair<long long int, long long int> busca(long long int)':
polygon.cpp:66:1: warning: control reaches end of non-void function [-Wreturn-type]
   66 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 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 2676 KB Output is correct
10 Correct 2 ms 2636 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 2676 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 298 ms 26948 KB Output is correct
5 Correct 297 ms 18756 KB Output is correct
6 Correct 335 ms 28268 KB Output is correct
7 Correct 250 ms 16444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 281 ms 19388 KB Output is correct
2 Correct 279 ms 21100 KB Output is correct
3 Correct 201 ms 18768 KB Output is correct
4 Correct 254 ms 17132 KB Output is correct
5 Correct 300 ms 27532 KB Output is correct
6 Correct 260 ms 19496 KB Output is correct
7 Correct 264 ms 19580 KB Output is correct
8 Correct 233 ms 19356 KB Output is correct
9 Correct 262 ms 19656 KB Output is correct
10 Correct 180 ms 19576 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
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 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 2676 KB Output is correct
10 Correct 2 ms 2636 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 2676 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 298 ms 26948 KB Output is correct
20 Correct 297 ms 18756 KB Output is correct
21 Correct 335 ms 28268 KB Output is correct
22 Correct 250 ms 16444 KB Output is correct
23 Correct 281 ms 19388 KB Output is correct
24 Correct 279 ms 21100 KB Output is correct
25 Correct 201 ms 18768 KB Output is correct
26 Correct 254 ms 17132 KB Output is correct
27 Correct 300 ms 27532 KB Output is correct
28 Correct 260 ms 19496 KB Output is correct
29 Correct 264 ms 19580 KB Output is correct
30 Correct 233 ms 19356 KB Output is correct
31 Correct 262 ms 19656 KB Output is correct
32 Correct 180 ms 19576 KB Output is correct
33 Correct 2 ms 2636 KB Output is correct
34 Correct 2 ms 2636 KB Output is correct
35 Correct 2 ms 2636 KB Output is correct
36 Correct 2 ms 2636 KB Output is correct
37 Correct 287 ms 19064 KB Output is correct
38 Correct 286 ms 21024 KB Output is correct
39 Incorrect 280 ms 19572 KB Output isn't correct
40 Halted 0 ms 0 KB -