Submission #1044264

# Submission time Handle Problem Language Result Execution time Memory
1044264 2024-08-05T08:26:53 Z 우민규(#11006) Parking (CEOI22_parking) C++17
100 / 100
185 ms 32184 KB
#include <bits/stdc++.h>
using namespace std;

int n, m;
vector<int> fst, snd;
// bottom -> up then true, up -> bottom then false
vector<vector<int>> adj;
vector<int> type;
// determine type

vector<bool> visited, is_selfloop;
vector<vector<int>> locs;

enum Component {
    TrivialPath,
    SplitPath,
    PerfectCycle,
    SplitCycle,
    ComplexCycle,
    SelfLoop,
    EmptySelfLoop,
};

// {amt of top, saw path}
pair<int, bool> dfs(int node) {
    visited[node] = true;
    int top_amt = type[node] == 3;
    bool is_path = false;
    for (auto v : adj[node]) {
        if (visited[v]) continue;
        if (v == 0) {
            is_path = true;
        } else {
            auto [tt, ip] = dfs(v);
            top_amt += tt;
            is_path |= ip;
        }
    }
    return {top_amt, is_path};
}

Component component_type(int node) {
    if (is_selfloop[node]) return SelfLoop;

    auto [top_amt, is_path] = dfs(node);
    if (is_path) {
        if (top_amt) return SplitPath;
        return TrivialPath;
    }
    if (top_amt > 1) return ComplexCycle;
    if (top_amt) return SplitCycle;
    return PerfectCycle;
}

vector<pair<int, int>> drives;

vector<int> auxs;

void trivial_contraction(int node) {
    if (node == 0 || type[node] == 3) return;
    int a = locs[node][0];
    int b = locs[node][1];
    if (a == b) return;
    bool can_a_pop = snd[a] == 0 || snd[a] == node;
    bool can_b_pop = snd[b] == 0 || snd[b] == node;
    bool can_a_recv = fst[a] == node && snd[a] == 0;
    bool can_b_recv = fst[b] == node && snd[b] == 0;
    if (can_b_pop && can_a_recv) {
        drives.push_back({b, a});
        fst[a] = snd[a] = node;
        locs[node] = {a, a};
        if (snd[b] == node) {
            snd[b] = 0;
            trivial_contraction(fst[b]);
        } else {
            assert(fst[b] == node);
            fst[b] = 0;
            auxs.push_back(b);
        }
    } else if (can_a_pop && can_b_recv) {
        drives.push_back({a, b});
        fst[b] = snd[b] = node;
        locs[node] = {b, b};
        if (snd[a] == node) {
            snd[a] = 0;
            trivial_contraction(fst[a]);
        } else {
            assert(fst[a] == node);
            fst[a] = 0;
            auxs.push_back(a);
        }
    }
}

void trivial_component_contraction(int node) {
    visited[node] = true;
    trivial_contraction(node);
    for (auto v : adj[node]) {
        if (v == 0 || visited[v]) continue;
        trivial_component_contraction(v);
    }
}

void perfect_cycle_contraction(int node) {
    assert(!auxs.empty());
    int aux = auxs.back();
    auxs.pop_back();
    
    int a = locs[node][0], b = locs[node][1];
    if (snd[a] == node) {
        drives.push_back({a, aux});
        snd[a] = 0;
        fst[aux] = node;
        locs[node][0] = aux;
        trivial_contraction(fst[a]);
    }
    if (snd[b] == node) {
        drives.push_back({b, aux});
        snd[b] = 0;
        fst[aux] = node;
        locs[node][1] = aux;
        trivial_contraction(fst[b]);
    }
}

vector<int> component_top_nodes;
void find_top_node_and_contract_ends(int node) {
    visited[node] = true;
    trivial_contraction(node);
    for (auto v : adj[node]) if (v != 0 && !visited[v]) find_top_node_and_contract_ends(v);
    if (type[node] == 3) component_top_nodes.push_back(node);
}

void with_top_contraction(int node) {
    component_top_nodes.clear();
    find_top_node_and_contract_ends(node);
    for (auto v : component_top_nodes) {
        assert(!auxs.empty());
        int aux = auxs.back();
        auxs.pop_back();

        int a = locs[v][0];
        int b = locs[v][1];
        locs[v][0] = locs[v][1] = aux;

        drives.push_back({a, aux});
        drives.push_back({b, aux});
        snd[a] = 0, snd[b] = 0;
        fst[aux] = snd[aux] = v;
        
        trivial_contraction(fst[a]);
        trivial_contraction(fst[b]);
    }
}


void solve() {
    cin >> n >> m;
    adj.assign(n + 1, {}), type.assign(n + 1, 0), visited.assign(n + 1, false), is_selfloop.assign(n + 1, false);
    locs.assign(n + 1, {});
    int num_type[7]{};
    for (int i = 0; i < m; ++i) {
        int a, b;
        cin >> a >> b;
        if (a) type[a] = 2 * type[a];
        if (b) type[b] = 2 * type[b] + 1;
        fst.push_back(a), snd.push_back(b);
        adj[a].push_back(b), adj[b].push_back(a);
        locs[a].push_back(i), locs[b].push_back(i);
        if (a == 0 && b == 0) {
            num_type[EmptySelfLoop] += 1;
            auxs.push_back(i);
        }
        if (a == b) {
            is_selfloop[a] = true;
        }
    }
    // determine all endpoints
    vector<pair<Component, int>> sources;
    for (int i = 1; i <= n; ++i) {
        if (!visited[i]) {
            Component cur = component_type(i);
            num_type[cur] += 1;
            sources.push_back({cur, i});
        }
    }

    int req_moves = num_type[PerfectCycle];
    for (int i = 1; i <= n; ++i) {
        if (is_selfloop[i]) continue;
        req_moves += 1;
        if (type[i] == 3) req_moves += 1;
    }

    // Check if it's possible
    num_type[EmptySelfLoop] += num_type[TrivialPath];
    num_type[TrivialPath] = 0;
    if (num_type[EmptySelfLoop] == 0 &&
        (num_type[SplitPath] > 0 || num_type[PerfectCycle] > 0 ||
         num_type[SplitCycle] > 0 || num_type[ComplexCycle] > 0)) {
        cout << "-1\n";
        return;
    }
    num_type[EmptySelfLoop] += num_type[SplitPath];
    if (num_type[ComplexCycle] > 0) {
        if (num_type[EmptySelfLoop] < 2) {
            cout << "-1\n";
            return;
        }
    }
    // do the thing
    visited.assign(n + 1, false);
    for (auto [type, idx] : sources) {
        if (type == TrivialPath) trivial_component_contraction(idx);
    }
    for (auto [type, idx] : sources) {
        if (type == SplitPath || type == SplitCycle) with_top_contraction(idx);
        if (type == PerfectCycle) perfect_cycle_contraction(idx);
    }
    for (auto [type, idx] : sources) {
        if (type == ComplexCycle) with_top_contraction(idx);
    }
    // assert(req_moves <= drives.size());
    // for (int i = 0; i < m; ++i) {
    //     assert(fst[i] == snd[i]);
    // }
    cout << drives.size() << "\n";
    for (auto [u, v] : drives) cout << u + 1 << " " << v + 1 << "\n";
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    solve();
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:233:9: warning: unused variable 't' [-Wunused-variable]
  233 |     int t = 1;
      |         ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 16176 KB Output is correct
2 Correct 61 ms 18528 KB Output is correct
3 Correct 39 ms 13596 KB Output is correct
4 Correct 35 ms 13056 KB Output is correct
5 Correct 58 ms 18600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 600 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 600 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 138 ms 29668 KB Output is correct
11 Correct 85 ms 26048 KB Output is correct
12 Correct 104 ms 23860 KB Output is correct
13 Correct 159 ms 28320 KB Output is correct
14 Correct 113 ms 24548 KB Output is correct
15 Correct 86 ms 23612 KB Output is correct
16 Correct 167 ms 29828 KB Output is correct
17 Correct 79 ms 23632 KB Output is correct
18 Correct 166 ms 29268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 0 ms 600 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 0 ms 604 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 0 ms 604 KB Output is correct
20 Correct 1 ms 600 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 47 ms 16176 KB Output is correct
12 Correct 61 ms 18528 KB Output is correct
13 Correct 39 ms 13596 KB Output is correct
14 Correct 35 ms 13056 KB Output is correct
15 Correct 58 ms 18600 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 0 ms 600 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 600 KB Output is correct
25 Correct 138 ms 29668 KB Output is correct
26 Correct 85 ms 26048 KB Output is correct
27 Correct 104 ms 23860 KB Output is correct
28 Correct 159 ms 28320 KB Output is correct
29 Correct 113 ms 24548 KB Output is correct
30 Correct 86 ms 23612 KB Output is correct
31 Correct 167 ms 29828 KB Output is correct
32 Correct 79 ms 23632 KB Output is correct
33 Correct 166 ms 29268 KB Output is correct
34 Correct 1 ms 600 KB Output is correct
35 Correct 0 ms 600 KB Output is correct
36 Correct 0 ms 344 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 0 ms 344 KB Output is correct
41 Correct 0 ms 604 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 604 KB Output is correct
45 Correct 0 ms 604 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 1 ms 600 KB Output is correct
50 Correct 1 ms 348 KB Output is correct
51 Correct 1 ms 604 KB Output is correct
52 Correct 0 ms 604 KB Output is correct
53 Correct 1 ms 600 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Correct 1 ms 604 KB Output is correct
56 Correct 0 ms 348 KB Output is correct
57 Correct 1 ms 604 KB Output is correct
58 Correct 135 ms 30096 KB Output is correct
59 Correct 148 ms 31772 KB Output is correct
60 Correct 112 ms 27244 KB Output is correct
61 Correct 164 ms 30772 KB Output is correct
62 Correct 73 ms 29368 KB Output is correct
63 Correct 140 ms 30144 KB Output is correct
64 Correct 100 ms 26852 KB Output is correct
65 Correct 132 ms 31044 KB Output is correct
66 Correct 125 ms 32180 KB Output is correct
67 Correct 73 ms 25912 KB Output is correct
68 Correct 122 ms 31136 KB Output is correct
69 Correct 113 ms 30136 KB Output is correct
70 Correct 71 ms 25892 KB Output is correct
71 Correct 84 ms 26840 KB Output is correct
72 Correct 85 ms 26420 KB Output is correct
73 Correct 185 ms 32184 KB Output is correct
74 Correct 95 ms 26168 KB Output is correct
75 Correct 133 ms 31284 KB Output is correct
76 Correct 169 ms 31948 KB Output is correct
77 Correct 138 ms 31164 KB Output is correct
78 Correct 83 ms 26904 KB Output is correct
79 Correct 146 ms 31028 KB Output is correct
80 Correct 83 ms 26172 KB Output is correct
81 Correct 124 ms 31160 KB Output is correct