답안 #701332

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
701332 2023-02-20T23:55:00 Z Lobo Parking (CEOI22_parking) C++17
100 / 100
483 ms 67856 KB
#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 2e5+10;

int n, m, b[maxn], t[maxn], done[maxn], mark[maxn], rec[maxn];
vector<int> gin[maxn], got[maxn], need[maxn], has[maxn], pos[maxn], ept;
vector<pair<int,int>> ans;

void add(vector<int> &vec, int x) {
    for(auto v : vec) if(v == x) return;
    vec.pb(x);
}

void remove(vector<int> &vec, int x) {
    vector<int> newv;
    for(auto v : vec) if(v != x) newv.pb(v);
    vec = newv;
}

void dfs(int u) {
    mark[u] = 1;
    rec[u] = 1;

    if(got[u].size() == 2) {
        add(need[u],u);
    }

    for(auto v : gin[u]) {
        if(rec[v]) {

        }
        else {
            if(!mark[v]) dfs(v);
            for(auto x : need[v]) add(need[u],x);
        }
    }
    rec[u] = 0;
}

void solve() {
    cin >> n >> m;

    for(int i = 1; i <= m; i++) {
        cin >> b[i] >> t[i];
        if(b[i] != 0) pos[b[i]].pb(i);
        if(t[i] != 0) pos[t[i]].pb(i+m);
        if(b[i] == 0) {
            ept.pb(i);
        }
        else if(b[i] == t[i]) {
            done[b[i]] = 1;
        }
        else if(t[i] != 0) {
            got[t[i]].pb(b[i]);
            gin[b[i]].pb(t[i]);
        }
    }

    for(int i = 1; i <= n; i++) {
        if(!mark[i]) dfs(i);
    }

    priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
    for(int i = 1; i <= n; i++) {
        if(got[i].size() == 0) {
            pq.push(mp(need[i].size(),i));
        }

        for(auto x : need[i]) {
            has[x].pb(i);
        }
    }
    queue<int> q;
    

    for(int i = 1; i <= n; i++) {
        if(!done[i] && gin[i].size() == 0 && got[i].size() != 2) {
            q.push(i);
        }
    }
    while(pq.size()) {
        int i = pq.top().sc; pq.pop();
        if(done[i]) continue;
        if(ept.size() < need[i].size()) {
            cout << -1 << endl;
            return;
        }
        for(auto v : need[i]) {
            sort(all(pos[v]));
            int p1 = pos[v][0]-m;
            int p2 = pos[v][1]-m;
            int e = ept.back(); ept.pop_back();
            ans.pb(mp(p1,e));
            t[p1] = 0;
            b[e] = v;
            pos[v][0] = e;
            ans.pb(mp(p2,e));
            t[p2] = 0;
            t[e] = v;
            pos[v][1] = e+m;

            done[v] = 1;
            for(auto x : got[v]) {
                remove(gin[x],v);
                if(gin[x].size() == 0) {
                    q.push(x);
                }
            }

            for(auto x : has[v]) {
                remove(need[x],v);
                if(got[x].size() == 0) pq.push(mp(need[x].size(),x));
            }
        }

        while(q.size()) {
            int v = q.front(); q.pop(); if(done[v]) continue;
            if(got[v].size() == 1) {
                sort(all(pos[v]));
                int p1 = pos[v][0];
                int p2 = pos[v][1]-m;

                ans.pb(mp(p2,p1));
                t[p2] = 0;
                t[p1] = v;
                pos[v][1] = p1+m;

                done[v] = 1;
                for(auto x : got[v]) {
                    remove(gin[x],v);
                    if(gin[x].size() == 0) {
                        q.push(x);
                    }
                }
            }
            else {
                sort(all(pos[v]));
                int p1 = pos[v][0];
                int p2 = pos[v][1];

                ans.pb(mp(p2,p1));
                b[p2] = 0;
                ept.pb(p2);
                t[p1] = v;
                pos[v][1] = p1+m;

                done[v] = 1;
                for(auto x : got[v]) {
                    remove(gin[x],v);
                    if(gin[x].size() == 0) {
                        q.push(x);
                    }
                }
            }
        }
    }

    //solve cycles
    for(int i = 1; i <= n; i++) {
        if(!done[i]) {
            if(ept.size() < 1) {
                cout << -1 << endl;
                return;
            }
            queue<int> q;
            sort(all(pos[i]));
            int p1 = pos[i][0];
            int p2 = pos[i][1]-m;
            int e = ept.back(); ept.pop_back();
            ans.pb(mp(p2,e));
            t[p2] = 0;
            b[e] = i;
            pos[i][1] = e;
            remove(got[i],b[p2]);
            remove(gin[b[p2]],i);
            if(gin[b[p2]].size() == 0) {
                q.push(b[p2]);
            }

            while(q.size()) {
                int v = q.front(); q.pop();
                if(got[v].size() == 1) {
                    sort(all(pos[v]));
                    int p1 = pos[v][0];
                    int p2 = pos[v][1]-m;

                    ans.pb(mp(p2,p1));
                    t[p2] = 0;
                    t[p1] = v;
                    pos[v][1] = p1+m;

                    done[v] = 1;
                    for(auto x : got[v]) {
                        remove(gin[x],v);
                        if(gin[x].size() == 0) {
                            q.push(x);
                        }
                    }
                }
                else {
                    sort(all(pos[v]));
                    int p1 = pos[v][0];
                    int p2 = pos[v][1];

                    ans.pb(mp(p2,p1));
                    b[p2] = 0;
                    ept.pb(p2);
                    t[p1] = v;
                    pos[v][1] = p1+m;

                    done[v] = 1;
                    for(auto x : got[v]) {
                        remove(gin[x],v);
                        if(gin[x].size() == 0) {
                            q.push(x);
                        }
                    }
                }
            }
        }
    }

    cout << ans.size() << endl;
    for(auto x : ans) {
        cout << x.fr << " " << x.sc << endl;
    }
}

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(0);

    // freopen("in.in", "r", stdin);
    // freopen("out.out", "w", stdout);
    int tt = 1;
    // cin >> tt;
    while(tt--) {
        solve();
    }

}

Compilation message

Main.cpp: In function 'void solve()':
Main.cpp:177:17: warning: unused variable 'p1' [-Wunused-variable]
  177 |             int p1 = pos[i][0];
      |                 ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Correct 12 ms 23744 KB Output is correct
3 Correct 14 ms 23764 KB Output is correct
4 Correct 13 ms 23764 KB Output is correct
5 Correct 14 ms 23832 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 13 ms 23776 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Correct 12 ms 23764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 104 ms 37948 KB Output is correct
2 Correct 142 ms 40988 KB Output is correct
3 Correct 81 ms 35136 KB Output is correct
4 Correct 70 ms 34400 KB Output is correct
5 Correct 141 ms 40600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23892 KB Output is correct
2 Correct 15 ms 23892 KB Output is correct
3 Correct 13 ms 23964 KB Output is correct
4 Correct 13 ms 24020 KB Output is correct
5 Correct 15 ms 23904 KB Output is correct
6 Correct 15 ms 23892 KB Output is correct
7 Correct 16 ms 24020 KB Output is correct
8 Correct 13 ms 23948 KB Output is correct
9 Correct 15 ms 24020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23892 KB Output is correct
2 Correct 15 ms 23892 KB Output is correct
3 Correct 13 ms 23964 KB Output is correct
4 Correct 13 ms 24020 KB Output is correct
5 Correct 15 ms 23904 KB Output is correct
6 Correct 15 ms 23892 KB Output is correct
7 Correct 16 ms 24020 KB Output is correct
8 Correct 13 ms 23948 KB Output is correct
9 Correct 15 ms 24020 KB Output is correct
10 Correct 478 ms 64564 KB Output is correct
11 Correct 115 ms 44116 KB Output is correct
12 Correct 308 ms 54700 KB Output is correct
13 Correct 402 ms 62340 KB Output is correct
14 Correct 339 ms 58820 KB Output is correct
15 Correct 300 ms 57796 KB Output is correct
16 Correct 464 ms 67856 KB Output is correct
17 Correct 296 ms 58276 KB Output is correct
18 Correct 431 ms 65208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 24000 KB Output is correct
2 Correct 13 ms 23892 KB Output is correct
3 Correct 13 ms 23980 KB Output is correct
4 Correct 12 ms 23956 KB Output is correct
5 Correct 16 ms 23832 KB Output is correct
6 Correct 13 ms 23892 KB Output is correct
7 Correct 16 ms 23956 KB Output is correct
8 Correct 13 ms 24020 KB Output is correct
9 Correct 13 ms 24020 KB Output is correct
10 Correct 13 ms 23944 KB Output is correct
11 Correct 15 ms 23944 KB Output is correct
12 Correct 13 ms 23892 KB Output is correct
13 Correct 13 ms 23892 KB Output is correct
14 Correct 13 ms 23892 KB Output is correct
15 Correct 13 ms 23892 KB Output is correct
16 Correct 14 ms 24148 KB Output is correct
17 Correct 13 ms 23892 KB Output is correct
18 Correct 13 ms 24020 KB Output is correct
19 Correct 13 ms 24020 KB Output is correct
20 Correct 12 ms 24020 KB Output is correct
21 Correct 13 ms 23936 KB Output is correct
22 Correct 14 ms 24020 KB Output is correct
23 Correct 13 ms 23892 KB Output is correct
24 Correct 15 ms 24020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Correct 12 ms 23744 KB Output is correct
3 Correct 14 ms 23764 KB Output is correct
4 Correct 13 ms 23764 KB Output is correct
5 Correct 14 ms 23832 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 13 ms 23776 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Correct 12 ms 23764 KB Output is correct
11 Correct 104 ms 37948 KB Output is correct
12 Correct 142 ms 40988 KB Output is correct
13 Correct 81 ms 35136 KB Output is correct
14 Correct 70 ms 34400 KB Output is correct
15 Correct 141 ms 40600 KB Output is correct
16 Correct 15 ms 23892 KB Output is correct
17 Correct 15 ms 23892 KB Output is correct
18 Correct 13 ms 23964 KB Output is correct
19 Correct 13 ms 24020 KB Output is correct
20 Correct 15 ms 23904 KB Output is correct
21 Correct 15 ms 23892 KB Output is correct
22 Correct 16 ms 24020 KB Output is correct
23 Correct 13 ms 23948 KB Output is correct
24 Correct 15 ms 24020 KB Output is correct
25 Correct 478 ms 64564 KB Output is correct
26 Correct 115 ms 44116 KB Output is correct
27 Correct 308 ms 54700 KB Output is correct
28 Correct 402 ms 62340 KB Output is correct
29 Correct 339 ms 58820 KB Output is correct
30 Correct 300 ms 57796 KB Output is correct
31 Correct 464 ms 67856 KB Output is correct
32 Correct 296 ms 58276 KB Output is correct
33 Correct 431 ms 65208 KB Output is correct
34 Correct 17 ms 24000 KB Output is correct
35 Correct 13 ms 23892 KB Output is correct
36 Correct 13 ms 23980 KB Output is correct
37 Correct 12 ms 23956 KB Output is correct
38 Correct 16 ms 23832 KB Output is correct
39 Correct 13 ms 23892 KB Output is correct
40 Correct 16 ms 23956 KB Output is correct
41 Correct 13 ms 24020 KB Output is correct
42 Correct 13 ms 24020 KB Output is correct
43 Correct 13 ms 23944 KB Output is correct
44 Correct 15 ms 23944 KB Output is correct
45 Correct 13 ms 23892 KB Output is correct
46 Correct 13 ms 23892 KB Output is correct
47 Correct 13 ms 23892 KB Output is correct
48 Correct 13 ms 23892 KB Output is correct
49 Correct 14 ms 24148 KB Output is correct
50 Correct 13 ms 23892 KB Output is correct
51 Correct 13 ms 24020 KB Output is correct
52 Correct 13 ms 24020 KB Output is correct
53 Correct 12 ms 24020 KB Output is correct
54 Correct 13 ms 23936 KB Output is correct
55 Correct 14 ms 24020 KB Output is correct
56 Correct 13 ms 23892 KB Output is correct
57 Correct 15 ms 24020 KB Output is correct
58 Correct 388 ms 60932 KB Output is correct
59 Correct 452 ms 66560 KB Output is correct
60 Correct 363 ms 55200 KB Output is correct
61 Correct 399 ms 62524 KB Output is correct
62 Correct 108 ms 44584 KB Output is correct
63 Correct 333 ms 57196 KB Output is correct
64 Correct 306 ms 58564 KB Output is correct
65 Correct 437 ms 65680 KB Output is correct
66 Correct 465 ms 67384 KB Output is correct
67 Correct 290 ms 54348 KB Output is correct
68 Correct 379 ms 62780 KB Output is correct
69 Correct 379 ms 61608 KB Output is correct
70 Correct 291 ms 55896 KB Output is correct
71 Correct 330 ms 58508 KB Output is correct
72 Correct 311 ms 58236 KB Output is correct
73 Correct 483 ms 67504 KB Output is correct
74 Correct 323 ms 58180 KB Output is correct
75 Correct 461 ms 66304 KB Output is correct
76 Correct 458 ms 67040 KB Output is correct
77 Correct 440 ms 65872 KB Output is correct
78 Correct 332 ms 58024 KB Output is correct
79 Correct 437 ms 65588 KB Output is correct
80 Correct 298 ms 58548 KB Output is correct
81 Correct 404 ms 64408 KB Output is correct