#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;
}
if(vis[cur]) continue;
vis[cur] = true;
dists[x][cur] = dist;
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: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[cur].size(); i++)
| ~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
99156 KB |
Output is correct |
2 |
Runtime error |
123 ms |
4028 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
99164 KB |
Output is correct |
2 |
Correct |
35 ms |
99168 KB |
Output is correct |
3 |
Correct |
48 ms |
99180 KB |
Output is correct |
4 |
Correct |
43 ms |
99192 KB |
Output is correct |
5 |
Correct |
49 ms |
99180 KB |
Output is correct |
6 |
Correct |
155 ms |
100116 KB |
Output is correct |
7 |
Correct |
662 ms |
101612 KB |
Output is correct |
8 |
Correct |
598 ms |
102976 KB |
Output is correct |
9 |
Execution timed out |
1589 ms |
105580 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
99164 KB |
Output is correct |
2 |
Correct |
35 ms |
99168 KB |
Output is correct |
3 |
Correct |
48 ms |
99180 KB |
Output is correct |
4 |
Correct |
43 ms |
99192 KB |
Output is correct |
5 |
Correct |
49 ms |
99180 KB |
Output is correct |
6 |
Correct |
155 ms |
100116 KB |
Output is correct |
7 |
Correct |
662 ms |
101612 KB |
Output is correct |
8 |
Correct |
598 ms |
102976 KB |
Output is correct |
9 |
Execution timed out |
1589 ms |
105580 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
99164 KB |
Output is correct |
2 |
Correct |
35 ms |
99168 KB |
Output is correct |
3 |
Correct |
48 ms |
99180 KB |
Output is correct |
4 |
Correct |
43 ms |
99192 KB |
Output is correct |
5 |
Correct |
49 ms |
99180 KB |
Output is correct |
6 |
Correct |
155 ms |
100116 KB |
Output is correct |
7 |
Correct |
662 ms |
101612 KB |
Output is correct |
8 |
Correct |
598 ms |
102976 KB |
Output is correct |
9 |
Execution timed out |
1589 ms |
105580 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
123 ms |
4044 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
99156 KB |
Output is correct |
2 |
Runtime error |
123 ms |
4028 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |