# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
282112 |
2020-08-24T03:10:03 Z |
jjwdi0 |
물병 (JOI14_bottle) |
C++11 |
|
425 ms |
88624 KB |
#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) {
if(f(x) != f(y)) return -1;
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];
assert(st1[x][0] == st1[y][0]);
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, 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));
}
for(int i=1; i<=N; i++) if(!lev[i]) dfs(i, 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));
}
}
Compilation message
bottle.cpp: In function 'int main()':
bottle.cpp:37:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
37 | scanf("%d %d %d %d", &N, &M, &K, &Q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bottle.cpp:38:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
38 | for(int i=1; i<=N; i++) scanf("%s", A[i] + 1);
| ~~~~~^~~~~~~~~~~~~~~~
bottle.cpp:39:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
39 | 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:65:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
65 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
16 ms |
11264 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
19348 KB |
Output is correct |
2 |
Runtime error |
56 ms |
21672 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
19360 KB |
Output is correct |
2 |
Runtime error |
46 ms |
19144 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
425 ms |
88624 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |