#include<bits/stdc++.h>
using namespace std;
#define int long long
const int inf=1e18;
vector<int> tin;
vector<int> tout;
int t=0;
void dfs(int v, vector<bool>& vis, vector<vector<int> >& adi){
if(vis[v]) return;
vis[v]=true;
tin[v]=t;
t++;
for(auto u: adi[v]){
dfs(u, vis, adi);
}
tout[v]=t;
t++;
}
bool ancestor(int a, int b){
return tin[a]<tin[b] && tout[b]<tout[a];
}
int dist(int a, int b, vector<vector<int> >& up, int l){
if(a==b) return 0;
if(!ancestor(b, a)) return inf;
int ans=0;
//cout << "dist: " << a << " " << b << " " << ancestor(b, a) << "\n";
for(int x=l-1; x>=0; x--){
if(up[a][x]!=-1 && ancestor(b, up[a][x])){
ans+=(1<<x);
a=up[a][x];
//cout << a << " " << b << "\n" << flush;
}
}
assert(up[a][0]==b);
return ans+1;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<pair<int,int> > e(n);
map<int, pair<vector<int>, vector<int> > > time;
for(int i=0; i<n; i++){
int s, t;
cin>> s >> t;
e[i]={s, t};
time[s].first.push_back(i);
time[t].second.push_back(i);
}
vector<vector<int> > adi(n);
vector<int> p(n, -1);
set<int> active;
for(auto m: time){
for(auto e: m.second.first){
active.insert(e);
}
for(auto e: m.second.second){
for(auto x: active){
if(x==e) continue;
p[e]=x;
adi[x].push_back(e);
}
}
for(auto e: m.second.second){
active.erase(e);
}
}
/*for(int i=0; i<n; i++){
for(auto e: adi[i]){
cout << e << " ";
}
cout << "\n" << flush;
}*/
int l=25;
vector<vector<int> > up(n, vector<int> (l, -1));
for(int i=0; i<n; i++){
up[i][0]=p[i];
}
for(int x=1; x<l; x++){
for(int i=0; i<n; i++){
if(up[i][x-1]!=-1){
up[i][x]=up[up[i][x-1]][x-1];
}
}
}
tin.resize(n, inf);
tout.resize(n, inf);
vector<bool> vis(n);
for(int i=0; i<n; i++){
if(p[i]==-1){
//cout << "start: " << i << "\n" << flush;
dfs(i, vis, adi);
}
}
/*cout << "achtung:\n";
for(int i=0; i<n; i++){
cout << tin[i] << " " << tout[i] << "\n" << flush;
}*/
for(int i=0; i<q; i++){
int a, b;
cin >> a >> b;
if(e[a-1].second==e[b-1].second){
cout << 1 << "\n";
continue;
}
int ans=dist(a-1, b-1, up, l);
if(ans==inf){
cout << "impossible\n";
}
else{
cout << ans << "\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
244 ms |
53312 KB |
Output is correct |
2 |
Correct |
286 ms |
53300 KB |
Output is correct |
3 |
Correct |
340 ms |
53276 KB |
Output is correct |
4 |
Correct |
198 ms |
55392 KB |
Output is correct |
5 |
Incorrect |
305 ms |
51384 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |