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;
using pr = pair<int, int>;
using qr = pair<pr, pr>;
using tr = pair<int, pr>;
int N, M, K, Q, X[200005], Y[200005], d[4] = { 0,0,1,-1 }, par[200005], st1[200005][20], st2[200005][20], lev[200005];
pr D[2005][2005];
char A[2005][2005];
vector<tr> edge;
vector<pr> v[200005];
queue<qr> q;
int f(int x) { return par[x] = (par[x] == x ? x : f(par[x])); }
void uni(int x, int y) { par[f(y)] = par[f(x)]; }
void dfs(int x, int p) {
lev[x] = lev[p] + 1, st1[x][0] = p;
for(auto it : v[x]) {
if(it.first == p) st2[x][0] = it.second;
else dfs(it.first, x);
}
}
int query(int x, int y) {
int res = 0;
if(lev[x] > lev[y]) swap(x, y);
for(int i=0; i<20; i++) if((lev[y] - lev[x]) & (1 << i)) res = max(res, st2[y][i]), y = st1[y][i];
if(x == y) return res;
for(int i=19; ~i; i--) if(st1[x][i] != st1[y][i]) res = max({ res, st2[x][i], st2[y][i] }), x = st1[x][i], y = st1[y][i];
return max({ res, st2[x][0], st2[y][0] });
}
int main() {
scanf("%d %d %d %d", &N, &M, &K, &Q);
for(int i=1; i<=N; i++) scanf("%s", A[i] + 1);
for(int i=1; i<=K; i++) scanf("%d %d", X+i, Y+i), q.push(qr(pr(0, i), pr(X[i], Y[i]))), D[X[i]][Y[i]] = pr(0, i);
while(!q.empty()) {
int u = q.front().first.second, dist = q.front().first.first, x = q.front().second.first, y = q.front().second.second; q.pop();
for(int i=0; i<4; i++) {
int xx = x + d[i], yy = y + d[3-i];
if(xx < 1 || yy < 1 || xx > N || yy > M || D[xx][yy].second == u || A[xx][yy] == '#') continue;
if(D[xx][yy].second) edge.push_back(tr(D[xx][yy].first + dist + 1, pr(u, D[xx][yy].second)));
else D[xx][yy] = pr(dist + 1, u), q.push(qr(D[xx][yy], pr(xx, yy)));
}
}
iota(par, par + K + 1, 0);
sort(edge.begin(), edge.end());
for(auto it : edge) {
int cost = it.first, x = it.second.first, y = it.second.second;
if(f(x) == f(y)) continue;
uni(x, y);
v[x].push_back(pr(y, cost));
v[y].push_back(pr(x, cost));
}
dfs(1, 0);
for(int i=1; i<20; i++) for(int j=1; j<=N; j++) {
st1[j][i] = st1[st1[j][i-1]][i-1];
st2[j][i] = max(st2[j][i-1], st2[st1[j][i-1]][i-1]);
}
while(Q--) {
int x, y;
scanf("%d %d", &x, &y);
printf("%d\n", query(x, y) - 1);
}
}
Compilation message (stderr)
bottle.cpp: In function 'int main()':
bottle.cpp:35:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
35 | scanf("%d %d %d %d", &N, &M, &K, &Q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bottle.cpp:36:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
36 | for(int i=1; i<=N; i++) scanf("%s", A[i] + 1);
| ~~~~~^~~~~~~~~~~~~~~~
bottle.cpp:37:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
37 | for(int i=1; i<=K; i++) scanf("%d %d", X+i, Y+i), q.push(qr(pr(0, i), pr(X[i], Y[i]))), D[X[i]][Y[i]] = pr(0, i);
| ~~~~~^~~~~~~~~~~~~~~~~~~
bottle.cpp:63:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
63 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
# | 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... |