이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |