#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define uniq(x) x.erase(unique(all(x)), x.end())
#define rall(x) x.rbegin(), x.rend()
//#define int long long
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
const double eps = 1e-9;
void setIO() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
int n, q;
struct Event {
int a, b, id;
bool operator<(Event &e) {
return a < e.a;
}
};
vector<Event> read_input(int n) {
vector<Event> v;
set<int> s;
for(int i=0; i<n; i++) {
int a, b;
cin >> a >> b;
v.push_back(Event{a, b, i+1});
s.insert(a); s.insert(b);
}
vector<int> comp(all(s));
for(Event &e : v) {
e.a = lower_bound(all(comp), e.a) - comp.begin();
e.b = lower_bound(all(comp), e.b) - comp.begin();
}
sort(all(v));
return v;
}
void solve_bfs() {
vector<pii> v(n+1);
for(int i=1; i<=n; i++) cin >> v[i].first >> v[i].second;
vector<vector<int> > graph(n+1);
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
if(i == j) continue;
if(v[j].first <= v[i].second && v[i].second <= v[j].second)
graph[i].push_back(j);
}
}
auto bfs = [&](int a, int b) {
vector<bool> vis(n+1);
vector<int> dist(n+1, 0);
queue<int> q;
vis[a] = 1;
q.push(a);
while(!q.empty()) {
int u = q.front();
q.pop();
if(u == b) return dist[b];
for(int &v : graph[u]) {
if(vis[v]) continue;
dist[v] = dist[u] + 1;
vis[v] = 1;
q.push(v);
}
}
return -1;
};
while(q--) {
int a, b;
cin >> a >> b;
int ans = bfs(a, b);
if(ans == -1) cout << "impossible\n";
else cout << ans << '\n';
}
}
void solve_tree() {
}
int32_t main() {
setIO();
cin >> n >> q;
if(n <= 1000) solve_bfs();
else solve_tree();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
5 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
348 KB |
Output is correct |
6 |
Correct |
9 ms |
1116 KB |
Output is correct |
7 |
Correct |
15 ms |
2112 KB |
Output is correct |
8 |
Correct |
19 ms |
3164 KB |
Output is correct |
9 |
Correct |
47 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
5 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
348 KB |
Output is correct |
6 |
Correct |
9 ms |
1116 KB |
Output is correct |
7 |
Correct |
15 ms |
2112 KB |
Output is correct |
8 |
Correct |
19 ms |
3164 KB |
Output is correct |
9 |
Correct |
47 ms |
4444 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
348 KB |
Output is correct |
13 |
Correct |
4 ms |
348 KB |
Output is correct |
14 |
Correct |
4 ms |
348 KB |
Output is correct |
15 |
Correct |
8 ms |
1300 KB |
Output is correct |
16 |
Correct |
16 ms |
1884 KB |
Output is correct |
17 |
Correct |
16 ms |
3160 KB |
Output is correct |
18 |
Correct |
47 ms |
4444 KB |
Output is correct |
19 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
5 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
348 KB |
Output is correct |
6 |
Correct |
9 ms |
1116 KB |
Output is correct |
7 |
Correct |
15 ms |
2112 KB |
Output is correct |
8 |
Correct |
19 ms |
3164 KB |
Output is correct |
9 |
Correct |
47 ms |
4444 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
4 ms |
348 KB |
Output is correct |
13 |
Correct |
4 ms |
344 KB |
Output is correct |
14 |
Correct |
4 ms |
344 KB |
Output is correct |
15 |
Correct |
8 ms |
1116 KB |
Output is correct |
16 |
Correct |
15 ms |
1884 KB |
Output is correct |
17 |
Correct |
16 ms |
3164 KB |
Output is correct |
18 |
Correct |
47 ms |
4444 KB |
Output is correct |
19 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |