#include <bits/stdc++.h>
#define li long long int
#define MP make_pair
#define pb push_back
using namespace std;
const int N = 5e3+5;
const int Q = 1e5+5;
const int MOD = 1e9+7;
int n,m,q;
vector<pair<int,int> > events;
vector<int> adj[N];
vector<pair<int,int> > queries(Q);
int dists[N][N];
bool vis[N];
void bfs(int x) {
queue<int> nodes;
nodes.push(x);
nodes.push(-1);
for (int i = 1; i <= n; i++)
{
vis[i] = false;
}
int dist = 0;
int cur;
while (nodes.size() > 1) {
cur = nodes.front();
nodes.pop();
if(cur==-1) {
dist++;
nodes.push(-1);
continue;
}
dists[x][cur] = dist;
if(vis[cur]) continue;
vis[cur] = true;
for (int i = 0; i < adj[cur].size(); i++)
{
if(vis[adj[cur][i]]) continue;
nodes.push(adj[cur][i]);
}
}
}
void solve() {
cin>>n>>q;
events.pb(MP(-1,-1));
for (int i = 1; i <= n; i++)
{
int tmp1,tmp2;
cin>>tmp1>>tmp2;
events.pb(MP(tmp1,tmp2));
}
for (int i = 0; i < q; i++)
{
cin>>queries[i].first>>queries[i].second;
}
for (int i = 1; i <= n; i++)
{
for (int j = i+1; j <= n; j++)
{
if(events[i].second > events[j].second &&
events[j].second >= events[i].first) {
adj[j].pb(i);
//cout<<j<<" >> "<<i<<"\n";
}
else if(events[j].second > events[i].second &&
events[i].second >= events[j].first) {
adj[i].pb(j);
//cout<<i<<" >> "<<j<<"\n";
}
else if(events[j].second == events[i].second) {
adj[i].pb(j); adj[j].pb(i);
}
}
}
int res;
memset(dists, -1, sizeof(dists));
for (int i = 1; i <= n; i++)
{
bfs(i);
}
for (int i = 0; i < q; i++)
{
res = dists[queries[i].first][queries[i].second];
if(res == -1) cout<<"impossible\n";
else cout<<res<<"\n";
}
}
signed main() {
solve();
}
Compilation message
events.cpp: In function 'void bfs(int)':
events.cpp:39:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for (int i = 0; i < adj[cur].size(); i++)
| ~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
99148 KB |
Output is correct |
2 |
Runtime error |
127 ms |
5004 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
37 ms |
99156 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
37 ms |
99156 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
37 ms |
99156 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
132 ms |
4984 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
99148 KB |
Output is correct |
2 |
Runtime error |
127 ms |
5004 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |