#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define OPT ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define endl "\n"
#define all(v) v.begin(), v.end()
#define mpr make_pair
#define pb push_back
#define ts to_string
#define fi first
#define se second
#define inf 0x3F3F3F3F
#define bpc __builtin_popcount
#define print(v) for(int i = 0; i < v.size(); i++) \
cout << v[i] << " "; \
cout<<endl;
vector<int> adj[1001];
int n;
int bfs(int s, int f)
{
vector<int> dist(1001, -1);
queue<int> q;
q.push(s);
dist[s] = 0;
while (!q.empty())
{
int x = q.front();
q.pop();
for (int i = 0; i < adj[x].size(); i++)
{
if (dist[adj[x][i]] == -1)
{
dist[adj[x][i]] = dist[x] + 1;
q.push(adj[x][i]);
}
}
}
return dist[f];
}
int main()
{
int q;
cin >> n >> q;
set<int> ts;
vector<pii> v;
vector<pair<int, pii>> sorted;
for (int i = 0; i < n; i++)
{
int a, b;
cin >> a >> b;
ts.insert(a);
ts.insert(b);
v.pb(mpr(a, b));
sorted.pb(mpr(a, mpr(b, i)));
}
cout << endl;
sort(all(sorted));
unordered_set<int> on;
unordered_set<int> of;
int ind = 0;
for (int i : ts)
{
while (ind < n && sorted[ind].fi <= i)
{
if (sorted[ind].fi >= i)
{
on.insert(sorted[ind].se.se);
}
ind++;
}
for (int x : on)
{
if (of.find(x) != of.end()) continue;
if (v[x].se == i)
{
for (auto j : on)
{
if (v[j].se != i && of.find(j) != of.end()) continue;
if (x == j) continue;
adj[x+1].pb(j+1);
}
of.insert(x);
}
}
}
while (q--)
{
int a, b;
cin >> a >> b;
int d = bfs(a, b);
if (d == -1)
{
cout << "impossible\n";
}
else
{
cout << d << endl;
}
}
}
Compilation message
events.cpp: In function 'int bfs(int, int)':
events.cpp:40:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for (int i = 0; i < adj[x].size(); i++)
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Runtime error |
122 ms |
13856 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
9 ms |
468 KB |
Output is correct |
4 |
Correct |
11 ms |
560 KB |
Output is correct |
5 |
Correct |
12 ms |
468 KB |
Output is correct |
6 |
Correct |
20 ms |
1236 KB |
Output is correct |
7 |
Correct |
44 ms |
2008 KB |
Output is correct |
8 |
Correct |
43 ms |
3076 KB |
Output is correct |
9 |
Correct |
148 ms |
4436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
9 ms |
468 KB |
Output is correct |
4 |
Correct |
11 ms |
560 KB |
Output is correct |
5 |
Correct |
12 ms |
468 KB |
Output is correct |
6 |
Correct |
20 ms |
1236 KB |
Output is correct |
7 |
Correct |
44 ms |
2008 KB |
Output is correct |
8 |
Correct |
43 ms |
3076 KB |
Output is correct |
9 |
Correct |
148 ms |
4436 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
9 ms |
468 KB |
Output is correct |
13 |
Correct |
8 ms |
472 KB |
Output is correct |
14 |
Correct |
9 ms |
468 KB |
Output is correct |
15 |
Correct |
19 ms |
1236 KB |
Output is correct |
16 |
Correct |
43 ms |
2016 KB |
Output is correct |
17 |
Correct |
43 ms |
3148 KB |
Output is correct |
18 |
Correct |
124 ms |
4332 KB |
Output is correct |
19 |
Runtime error |
5 ms |
1236 KB |
Execution killed with signal 11 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
9 ms |
468 KB |
Output is correct |
4 |
Correct |
11 ms |
560 KB |
Output is correct |
5 |
Correct |
12 ms |
468 KB |
Output is correct |
6 |
Correct |
20 ms |
1236 KB |
Output is correct |
7 |
Correct |
44 ms |
2008 KB |
Output is correct |
8 |
Correct |
43 ms |
3076 KB |
Output is correct |
9 |
Correct |
148 ms |
4436 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
9 ms |
468 KB |
Output is correct |
13 |
Correct |
10 ms |
512 KB |
Output is correct |
14 |
Correct |
8 ms |
468 KB |
Output is correct |
15 |
Correct |
18 ms |
1236 KB |
Output is correct |
16 |
Correct |
44 ms |
2132 KB |
Output is correct |
17 |
Correct |
45 ms |
3076 KB |
Output is correct |
18 |
Correct |
120 ms |
4436 KB |
Output is correct |
19 |
Runtime error |
130 ms |
13952 KB |
Execution killed with signal 11 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
129 ms |
13956 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Runtime error |
122 ms |
13856 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |