#include <algorithm>
#include <climits>
#include <iostream>
#include <set>
#include <queue>
#include <vector>
#define int long long
using namespace std;
int const INF = INT_MAX / 3;
struct event {
int s, e, id;
};
enum point_type {
start,
end,
};
struct point {
point_type ty;
int tm;
int event_id;
};
vector<int> bfs(vector<vector<int>> const& graph, int const start) {
queue<int> q;
q.push(start);
vector<int> dist(graph.size(), INF);
dist[start] = 0;
while (!q.empty()) {
int const cur = q.front();
q.pop();
for (int const& neigh : graph[cur]) {
if (dist[neigh] != INF) {
continue;
}
dist[neigh] = dist[cur] + 1;
q.push(neigh);
}
}
return dist;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<event> events(n);
vector<point> points;
for (int i = 0; i < n; i++) {
cin >> events[i].s >> events[i].e;
events[i].id = i;
points.push_back({ point_type::start, events[i].s, i });
points.push_back({ point_type::end, events[i].e, i });
}
sort(points.begin(), points.end(), [](auto const& a, auto const& b) {
return a.tm < b.tm || (a.tm == b.tm && a.ty == point_type::start);
});
vector<vector<int>> graph(n);
{
set<int> cur;
vector<int> removed;
int last_tm = -1;
for (auto const& [ty, tm, e_id] : points) {
if (tm > last_tm) {
for (int const& r : removed) {
cur.erase(r);
}
removed.clear();
}
last_tm = tm;
if (ty == point_type::start) {
cur.insert(e_id);
}
else {
removed.push_back(e_id);
for (int const& c : cur) {
graph[e_id].push_back(c);
}
}
}
}
while (q--) {
int s, e;
cin >> s >> e;
s--; e--;
auto const dist = bfs(graph, s);
if (dist[e] == INF) {
cout << "impossible\n";
}
else {
cout << dist[e] << "\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1566 ms |
15724 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
7 ms |
1944 KB |
Output is correct |
7 |
Correct |
17 ms |
3668 KB |
Output is correct |
8 |
Correct |
22 ms |
5716 KB |
Output is correct |
9 |
Runtime error |
2 ms |
724 KB |
Execution killed with signal 6 |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
7 ms |
1944 KB |
Output is correct |
7 |
Correct |
17 ms |
3668 KB |
Output is correct |
8 |
Correct |
22 ms |
5716 KB |
Output is correct |
9 |
Runtime error |
2 ms |
724 KB |
Execution killed with signal 6 |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
7 ms |
1944 KB |
Output is correct |
7 |
Correct |
17 ms |
3668 KB |
Output is correct |
8 |
Correct |
22 ms |
5716 KB |
Output is correct |
9 |
Runtime error |
2 ms |
724 KB |
Execution killed with signal 6 |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1547 ms |
15796 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1566 ms |
15724 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |