답안 #680500

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
680500 2023-01-11T01:49:10 Z jhwest2 Love Polygon (BOI18_polygon) C++17
100 / 100
355 ms 36156 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 101010;
int n, go[N], vis[N], done[N]; bool dead[N];
vector<int> g[N];

map<string, int> mp;
map<int, int> cant;

array<int, 3> dfs(int p, int v) {
    int s = 0, t = 1, sz = 1;
    done[v]++;
    for (int x : g[v]) {
        if (p == x || dead[x])
            continue;
        if (cant[v] == x)
            continue;
        
        auto [y, z, w] = dfs(v, x);
        s += max(y, z);
        t += y;
        sz += w;
    }
    return {s, t, sz};
}
int main() {
    cin.tie(0); ios_base::sync_with_stdio(0);
    cin >> n;
    int sz = 0;
    for (int i = 1; i <= n; i++) {
        string s, t; cin >> s >> t;
        int u = (mp[s] = (mp[s]) ? mp[s] : ++sz);
        int v = (mp[t] = (mp[t]) ? mp[t] : ++sz);
        go[u] = v;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    if (n % 2 == 1) 
        return !(cout << -1);

    int ans = 0;
    vector<int> vtx;
    for (int i = 1; i <= n; i++) {
        if (vis[i] == 2) 
            continue;

        int v = i;
        while (!vis[v]) {
            vtx.push_back(v);
            vis[v] = 1;
            v = go[v];
        }
        if (vis[v] == 1) {
            // not using the edge
            int s = 0;

            cant[go[v]] = v;
            cant[v] = go[v];
            auto [x, y, z] = dfs(v, v); 
            s = z - max(x, y);

            // using the edge
            if (go[v] != v) {
                int t = 1;

                // cycle is length 2
                if (v == go[go[v]])
                    t = 2;

                dead[v] = 1;
                dead[go[v]] = 1;
                
                for (int x : g[v])
                    if (go[v] != x) {
                        auto [a, b, c] = dfs(v, x);
                        t += c - max(a, b);
                    }
                for (int x : g[go[v]]) 
                    if (v != x && done[x] != 2) {
                        auto [a, b, c] = dfs(go[v], x);
                        t += c - max(a, b);
                    }
                
                ans += max(s, t);
            }
            else
                ans += s;
        }

        // initialize
        for (int x : vtx)
            vis[x] = 2;
        vector<int>().swap(vtx);
    }
    cout << n - ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 1 ms 2708 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2704 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2712 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 353 ms 33868 KB Output is correct
5 Correct 277 ms 21736 KB Output is correct
6 Correct 355 ms 36156 KB Output is correct
7 Correct 247 ms 16156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 282 ms 18404 KB Output is correct
2 Correct 299 ms 21960 KB Output is correct
3 Correct 181 ms 19424 KB Output is correct
4 Correct 275 ms 14124 KB Output is correct
5 Correct 259 ms 32696 KB Output is correct
6 Correct 242 ms 17216 KB Output is correct
7 Correct 292 ms 17196 KB Output is correct
8 Correct 247 ms 17796 KB Output is correct
9 Correct 223 ms 16332 KB Output is correct
10 Correct 150 ms 15180 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 1 ms 2708 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2704 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2712 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2704 KB Output is correct
16 Correct 1 ms 2644 KB Output is correct
17 Correct 1 ms 2644 KB Output is correct
18 Correct 1 ms 2644 KB Output is correct
19 Correct 353 ms 33868 KB Output is correct
20 Correct 277 ms 21736 KB Output is correct
21 Correct 355 ms 36156 KB Output is correct
22 Correct 247 ms 16156 KB Output is correct
23 Correct 282 ms 18404 KB Output is correct
24 Correct 299 ms 21960 KB Output is correct
25 Correct 181 ms 19424 KB Output is correct
26 Correct 275 ms 14124 KB Output is correct
27 Correct 259 ms 32696 KB Output is correct
28 Correct 242 ms 17216 KB Output is correct
29 Correct 292 ms 17196 KB Output is correct
30 Correct 247 ms 17796 KB Output is correct
31 Correct 223 ms 16332 KB Output is correct
32 Correct 150 ms 15180 KB Output is correct
33 Correct 2 ms 2644 KB Output is correct
34 Correct 2 ms 2644 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 2 ms 2644 KB Output is correct
37 Correct 275 ms 21324 KB Output is correct
38 Correct 291 ms 21940 KB Output is correct
39 Correct 327 ms 19680 KB Output is correct
40 Correct 288 ms 19376 KB Output is correct
41 Correct 287 ms 19592 KB Output is correct
42 Correct 322 ms 20008 KB Output is correct
43 Correct 344 ms 20024 KB Output is correct
44 Correct 339 ms 20076 KB Output is correct
45 Correct 302 ms 20164 KB Output is correct
46 Correct 289 ms 20044 KB Output is correct
47 Correct 248 ms 18672 KB Output is correct
48 Correct 304 ms 20440 KB Output is correct
49 Correct 275 ms 24132 KB Output is correct
50 Correct 212 ms 21656 KB Output is correct
51 Correct 217 ms 16284 KB Output is correct
52 Correct 313 ms 34896 KB Output is correct
53 Correct 261 ms 19596 KB Output is correct
54 Correct 293 ms 19384 KB Output is correct
55 Correct 257 ms 19912 KB Output is correct
56 Correct 212 ms 18484 KB Output is correct
57 Correct 208 ms 17360 KB Output is correct
58 Correct 2 ms 2704 KB Output is correct
59 Correct 1 ms 2644 KB Output is correct
60 Correct 2 ms 2696 KB Output is correct
61 Correct 2 ms 2644 KB Output is correct
62 Correct 3 ms 2644 KB Output is correct
63 Correct 2 ms 2644 KB Output is correct
64 Correct 3 ms 2644 KB Output is correct
65 Correct 312 ms 33840 KB Output is correct
66 Correct 261 ms 21772 KB Output is correct
67 Correct 340 ms 36064 KB Output is correct
68 Correct 259 ms 16184 KB Output is correct
69 Correct 2 ms 2708 KB Output is correct
70 Correct 2 ms 2708 KB Output is correct
71 Correct 3 ms 2644 KB Output is correct
72 Correct 1 ms 2644 KB Output is correct
73 Correct 2 ms 2772 KB Output is correct
74 Correct 2 ms 2644 KB Output is correct
75 Correct 2 ms 2700 KB Output is correct
76 Correct 2 ms 2708 KB Output is correct
77 Correct 2 ms 2644 KB Output is correct
78 Correct 2 ms 2644 KB Output is correct
79 Correct 2 ms 2644 KB Output is correct
80 Correct 2 ms 2644 KB Output is correct
81 Correct 3 ms 2644 KB Output is correct
82 Correct 2 ms 2644 KB Output is correct
83 Correct 2 ms 2644 KB Output is correct