Submission #701331

# Submission time Handle Problem Language Result Execution time Memory
701331 2023-02-20T23:52:58 Z Lobo Parking (CEOI22_parking) C++17
60 / 100
2000 ms 60504 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;
    

    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));
            }
        }

        for(int i = 1; i <= n; i++) {
            if(!done[i] && gin[i].size() == 0 && got[i].size() != 2) {
                q.push(i);
            }
        }
        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];
      |                 ^~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 13 ms 23836 KB Output is correct
3 Correct 13 ms 23764 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23764 KB Output is correct
6 Correct 12 ms 23840 KB Output is correct
7 Correct 13 ms 23764 KB Output is correct
8 Correct 13 ms 23832 KB Output is correct
9 Correct 14 ms 23764 KB Output is correct
10 Correct 13 ms 23764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1000 ms 37928 KB Output is correct
2 Execution timed out 2087 ms 39960 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23892 KB Output is correct
2 Correct 13 ms 24020 KB Output is correct
3 Correct 13 ms 23944 KB Output is correct
4 Correct 14 ms 23944 KB Output is correct
5 Correct 13 ms 23944 KB Output is correct
6 Correct 13 ms 23952 KB Output is correct
7 Correct 15 ms 24020 KB Output is correct
8 Correct 13 ms 23952 KB Output is correct
9 Correct 17 ms 24076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23892 KB Output is correct
2 Correct 13 ms 24020 KB Output is correct
3 Correct 13 ms 23944 KB Output is correct
4 Correct 14 ms 23944 KB Output is correct
5 Correct 13 ms 23944 KB Output is correct
6 Correct 13 ms 23952 KB Output is correct
7 Correct 15 ms 24020 KB Output is correct
8 Correct 13 ms 23952 KB Output is correct
9 Correct 17 ms 24076 KB Output is correct
10 Execution timed out 2060 ms 60504 KB Time limit exceeded
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 24020 KB Output is correct
2 Correct 13 ms 23964 KB Output is correct
3 Correct 13 ms 23884 KB Output is correct
4 Correct 13 ms 23892 KB Output is correct
5 Correct 12 ms 23892 KB Output is correct
6 Correct 15 ms 23892 KB Output is correct
7 Correct 13 ms 23892 KB Output is correct
8 Correct 14 ms 24064 KB Output is correct
9 Correct 14 ms 24076 KB Output is correct
10 Correct 15 ms 23928 KB Output is correct
11 Correct 16 ms 24024 KB Output is correct
12 Correct 14 ms 24020 KB Output is correct
13 Correct 13 ms 23944 KB Output is correct
14 Correct 13 ms 23892 KB Output is correct
15 Correct 13 ms 23940 KB Output is correct
16 Correct 15 ms 24020 KB Output is correct
17 Correct 17 ms 23904 KB Output is correct
18 Correct 15 ms 24020 KB Output is correct
19 Correct 14 ms 24044 KB Output is correct
20 Correct 16 ms 24020 KB Output is correct
21 Correct 13 ms 23892 KB Output is correct
22 Correct 14 ms 24040 KB Output is correct
23 Correct 13 ms 23944 KB Output is correct
24 Correct 13 ms 24020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 13 ms 23836 KB Output is correct
3 Correct 13 ms 23764 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23764 KB Output is correct
6 Correct 12 ms 23840 KB Output is correct
7 Correct 13 ms 23764 KB Output is correct
8 Correct 13 ms 23832 KB Output is correct
9 Correct 14 ms 23764 KB Output is correct
10 Correct 13 ms 23764 KB Output is correct
11 Correct 1000 ms 37928 KB Output is correct
12 Execution timed out 2087 ms 39960 KB Time limit exceeded
13 Halted 0 ms 0 KB -