# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
745889 | nguyentunglam | Railway Trip 2 (JOI22_ho_t4) | C++17 | 2 ms | 588 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 210;
int trace[N];
bool adj[N][N];
int n, m, k;
int bfs(int s, int t) {
for(int i = 1; i <= n; i++) trace[i] = 0;
queue<int> q;
q.push(s); trace[s] = -1;
while (!q.empty()) {
int u = q.front(); q.pop();
for(int v = 1; v <= n; v++) if (adj[u][v] && !trace[v]) {
trace[v] = u;
q.push(v);
}
}
if (trace[t] == 0) return -1;
vector<int> lst;
while (t != s) {
lst.push_back(t);
t = trace[t];
}
reverse(lst.begin(), lst.end());
int type = 0, pre = s, prepre = s, rev = 0;
for(int j : lst) {
if (prepre < pre && j < pre) rev++;
if (prepre > pre && j > pre) rev++;
prepre = pre;
pre = j;
}
if (rev > 1) assert(0);
return lst.size();
}
int main() {
#define task ""
cin.tie(0) -> sync_with_stdio(0);
if (fopen ("task.inp", "r")) {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
}
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
cin >> n >> k >> m;
for(int i = 1; i <= m; i++) {
int a, b; cin >> a >> b;
if (a < b) for(int j = a; j <= min(a + k - 1, b - 1); j++) for(int k = j + 1; k <= b; k++) adj[j][k] = 1;
else for(int j = a; j >= max(a - k + 1, b + 1); j--) for(int k = j - 1; k >= b; k--) adj[j][k] = 1;
}
int q; cin >> q;
while (q--) {
int s, t; cin >> s >> t;
cout << bfs(s, t) << endl;
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |