답안 #505297

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
505297 2022-01-11T03:47:58 Z wiwiho Love Polygon (BOI18_polygon) C++14
100 / 100
225 ms 24152 KB
#include <bits/stdc++.h>

#define eb emplace_back
#define mp make_pair
#define F first
#define S second
#define printv(a, b) { \
    for(auto pv : a) b << pv << " "; \
    b << "\n"; \
}

using namespace std;

using pii = pair<int, int>;

const int MAX = 1e9;

map<string, int> id;
int num = 1;
int getid(string& s){
    if(id.find(s) == id.end()) id[s] = num++;
    return id[s];
}

int n;
vector<vector<int>> g;
vector<int> dp0, dp1;
vector<bool> vst, vst2;
vector<int> f;

void init(){
    g.resize(n + 1);
    dp0.resize(n + 1);
    dp1.resize(n + 1);
    vst.resize(n + 1);
    vst2.resize(n + 1);
    f.resize(n + 1);
}

void dfs(int now){
    vst[now] = true;
    int sum = 0, mn = n;
    int cnt = 0;
    for(int i : g[now]){
        if(vst2[i]) continue;
        dfs(i);
        cnt++;
        sum += dp1[i];
        mn = min(mn, dp0[i] - dp1[i]);
    }
    if(cnt == 0){
        dp0[now] = 0; dp1[now] = 1;
    }
    else{
        dp0[now] = sum;
        dp1[now] = sum + mn + 1;
    }
}

int solve(int s){
    //cerr << "solve\n";
    
    while(!vst[s]){
        vst[s] = true;
        s = f[s];
    }

    vector<int> cy;
    int v = s;
    do{
        vst2[v] = vst[v] = true;
        cy.eb(v);
        v = f[v];
    }
    while(v != s);
    //printv(cy, cerr);

    for(int i : cy) dfs(i);

    vector<vector<int>> dps(3, vector<int>(3, MAX));
    dps[0][0] = dp0[cy[0]];
    dps[1][1] = dp0[cy[0]] + 1;
    dps[2][2] = dp1[cy[0]];

    for(int i = 1; i < cy.size(); i++){
        int now = cy[i];
        vector<vector<int>> dps2(3, vector<int>(3, MAX));
        
        for(int fst = 0; fst < 3; fst++){
            dps2[fst][0] = min(dps[fst][1], dps[fst][2]) + dp0[now];
            dps2[fst][1] = min({dps[fst][0], dps[fst][1], dps[fst][2]}) + dp0[now] + 1;
            dps2[fst][2] = min(dps[fst][1], dps[fst][2]) + dp1[now];
        }

        dps = dps2;
    }

    int ans = MAX;
    ans = min({ans, dps[0][1], dps[0][2]});
    ans = min({ans, dps[1][0], dps[1][1], dps[1][2]});
    ans = min({ans, dps[2][1], dps[2][2]});
    if(cy.size() == 2) ans = min(ans, dp0[cy[0]] + dp0[cy[1]]);
    //cerr << "solve ok " << ans << "\n";
    return ans;
}

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

    cin >> n;
    init();

    for(int i = 1; i <= n; i++){
        string s, t;
        cin >> s >> t;
        int u = getid(s), v = getid(t);
        f[u] = v;
    }
    for(int i = 1; i <= n; i++){
        g[f[i]].eb(i);
    }
    //for(auto& i : id) cerr << i.F << " " << i.S << "\n";

    if(n % 2){
        cout << "-1\n";
        return 0;
    }

    int ans = 0;
    for(int i = 1; i <= n; i++){
        if(vst[i]) continue;
        ans += solve(i);
    }

    //printv(dp0, cerr);
    //printv(dp1, cerr);

    cout << ans << "\n";

    return 0;
}

Compilation message

polygon.cpp: In function 'int solve(int)':
polygon.cpp:85:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |     for(int i = 1; i < cy.size(); i++){
      |                    ~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 196 ms 15340 KB Output is correct
5 Correct 208 ms 16836 KB Output is correct
6 Correct 201 ms 17576 KB Output is correct
7 Correct 173 ms 16956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 178 ms 13964 KB Output is correct
2 Correct 212 ms 16128 KB Output is correct
3 Correct 185 ms 14756 KB Output is correct
4 Correct 184 ms 13188 KB Output is correct
5 Correct 192 ms 21976 KB Output is correct
6 Correct 185 ms 13200 KB Output is correct
7 Correct 172 ms 13292 KB Output is correct
8 Correct 200 ms 13636 KB Output is correct
9 Correct 157 ms 12792 KB Output is correct
10 Correct 136 ms 12260 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 196 ms 15340 KB Output is correct
20 Correct 208 ms 16836 KB Output is correct
21 Correct 201 ms 17576 KB Output is correct
22 Correct 173 ms 16956 KB Output is correct
23 Correct 178 ms 13964 KB Output is correct
24 Correct 212 ms 16128 KB Output is correct
25 Correct 185 ms 14756 KB Output is correct
26 Correct 184 ms 13188 KB Output is correct
27 Correct 192 ms 21976 KB Output is correct
28 Correct 185 ms 13200 KB Output is correct
29 Correct 172 ms 13292 KB Output is correct
30 Correct 200 ms 13636 KB Output is correct
31 Correct 157 ms 12792 KB Output is correct
32 Correct 136 ms 12260 KB Output is correct
33 Correct 0 ms 204 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 0 ms 204 KB Output is correct
36 Correct 0 ms 204 KB Output is correct
37 Correct 185 ms 16568 KB Output is correct
38 Correct 208 ms 15836 KB Output is correct
39 Correct 178 ms 15492 KB Output is correct
40 Correct 208 ms 15404 KB Output is correct
41 Correct 179 ms 15396 KB Output is correct
42 Correct 188 ms 15820 KB Output is correct
43 Correct 175 ms 15812 KB Output is correct
44 Correct 192 ms 15876 KB Output is correct
45 Correct 180 ms 15796 KB Output is correct
46 Correct 185 ms 15828 KB Output is correct
47 Correct 183 ms 15004 KB Output is correct
48 Correct 176 ms 16136 KB Output is correct
49 Correct 200 ms 18156 KB Output is correct
50 Correct 161 ms 16912 KB Output is correct
51 Correct 163 ms 15400 KB Output is correct
52 Correct 206 ms 24152 KB Output is correct
53 Correct 181 ms 15452 KB Output is correct
54 Correct 179 ms 15328 KB Output is correct
55 Correct 167 ms 15756 KB Output is correct
56 Correct 170 ms 14920 KB Output is correct
57 Correct 152 ms 14464 KB Output is correct
58 Correct 0 ms 312 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 0 ms 316 KB Output is correct
61 Correct 0 ms 204 KB Output is correct
62 Correct 0 ms 204 KB Output is correct
63 Correct 0 ms 204 KB Output is correct
64 Correct 0 ms 300 KB Output is correct
65 Correct 201 ms 17628 KB Output is correct
66 Correct 225 ms 16876 KB Output is correct
67 Correct 190 ms 17572 KB Output is correct
68 Correct 216 ms 16908 KB Output is correct
69 Correct 0 ms 204 KB Output is correct
70 Correct 1 ms 204 KB Output is correct
71 Correct 0 ms 204 KB Output is correct
72 Correct 0 ms 204 KB Output is correct
73 Correct 1 ms 312 KB Output is correct
74 Correct 0 ms 204 KB Output is correct
75 Correct 1 ms 312 KB Output is correct
76 Correct 1 ms 204 KB Output is correct
77 Correct 1 ms 204 KB Output is correct
78 Correct 0 ms 204 KB Output is correct
79 Correct 0 ms 204 KB Output is correct
80 Correct 0 ms 308 KB Output is correct
81 Correct 0 ms 300 KB Output is correct
82 Correct 1 ms 312 KB Output is correct
83 Correct 1 ms 204 KB Output is correct