Submission #1228897

#TimeUsernameProblemLanguageResultExecution timeMemory
1228897marsasgrgCircle Passing (EGOI24_circlepassing)C++20
20 / 100
2094 ms844452 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<vector<int>> adj;
int N;
int left(const int x) { //-1
    if (x == 0) return 2 * N - 1;
    return  x- 1;
}
int right(const int x) { //+1
    if (x == 2 * N - 1) return 0;
    return x + 1;
}
int opp(const int x) {
    return (x + N) % (2 * N);
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int m, q;
    cin >> N >> m >> q;
    adj.resize(2 * N);
    for (int i = 0; i <= 2 * N - 1; i++) {
        adj[i].push_back(right(i));
        adj[i].push_back(left(i));
    }
    for (int i = 0; i < m; i++) {
        int k;
        cin >> k;
        adj[k].push_back(opp(k));
        adj[opp(k)].push_back(k);
    }
    for (int qq = 0; qq < q; qq++){
        int x, y;
        cin >> x >> y;
        queue<int> bfs;
        bfs.push(x);
        vector<int> dist(2 * N, LLONG_MAX);
        dist[x] = 0;
        while (!bfs.empty()) {
            const int z = bfs.front();
            bfs.pop();
            for (auto i: adj[z]) {
                if (dist[i] > dist[z] + 1) {
                    dist[i] = dist[z] + 1;
                    bfs.push(i);
                }
            }
        }
        cout << dist[y] << "\n";
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...