This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+1, M = N+N;
int n, m, x[N], y[N], a[M], b[M], up[N], dn[N], lf[N], rt[N], d[M*2];
bool vertical[M], vis[M*2];
vector<int> g[M*2], ans;
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i=1; i<=n; ++i)
cin >> x[i] >> y[i];
cin >> m;
for(int i=1; i<=m; ++i){
int &u = a[i], &v = b[i];
cin >> u >> v;
if((vertical[i] = (x[u] == x[v]))){
if(y[u] > y[v]) swap(u, v);
up[u] = dn[v] = i;
}else{
if(x[u] > x[v]) swap(u, v);
rt[u] = lf[v] = i;
}
}
vis[0] = 1;
for(int i=1; i<=m+m; ++i){
int u = i, v;
while(!vis[u]){
vis[u] = !(v = 0);
int w = u;
if(vertical[((u-1) % m) + 1]){
if(u > m){ // right
u -= m;
if(rt[a[u]] && !v) v = rt[a[u]] + m;
if(dn[a[u]] && !v) v = dn[a[u]] + m;
if(lf[a[u]] && !v) v = lf[a[u]];
if(up[a[u]] && !v) v = up[a[u]];
}else{
if(lf[b[u]] && !v) v = lf[b[u]];
if(up[b[u]] && !v) v = up[b[u]];
if(rt[b[u]] && !v) v = rt[b[u]] + m;
if(dn[b[u]] && !v) v = dn[b[u]] + m;
}
}else{
if(u > m){ // up
u -= m;
if(up[b[u]] && !v) v = up[b[u]];
if(rt[b[u]] && !v) v = rt[b[u]] + m;
if(dn[b[u]] && !v) v = dn[b[u]] + m;
if(lf[b[u]] && !v) v = lf[b[u]];
}else{
if(dn[a[u]] && !v) v = dn[a[u]] + m;
if(lf[a[u]] && !v) v = lf[a[u]];
if(up[a[u]] && !v) v = up[a[u]];
if(rt[a[u]] && !v) v = rt[a[u]] + m;
}
}
if(v){
g[w].push_back(v);
g[v].push_back(w);
}
u = v;
}
}
deque<int> q;
int lowest = 0, leftmost = 0;
for(int i=1; i<=m; ++i){
if(vertical[i]){
if(!leftmost || x[a[i]] < x[a[leftmost]]) leftmost = i;
}else{
if(!lowest || y[a[i]] < y[a[lowest]]) lowest = i;
}
}
lowest = lowest ? : leftmost;
q.push_back(lowest);
fill(d, d+m+m+1, 2e9);
d[lowest] = 0;
function<int(int)> f = [&](int z){ return z > m ? z - m : z; };
while(!q.empty()){
int u = q.front(); q.pop_front();
for(int v : g[u]) if(d[v] > d[u]){
d[v] = d[u];
q.push_front(v);
}
int v = u > m ? u-m : u+m;
if(d[v] > d[u] + 1){
d[v] = d[u] + 1;
q.push_back(v);
}
if(d[u] == d[v]) ans.push_back(min(u, v));
}
sort(begin(ans), end(ans));
ans.resize(unique(begin(ans), end(ans)) - begin(ans));
cout << size(ans) << '\n';
for(int i : ans) cout << i << '\n';
}
# | 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... |
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |