답안 #282108

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
282108 2020-08-24T03:06:31 Z jjwdi0 물병 (JOI14_bottle) C++11
0 / 100
492 ms 45220 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];
	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));
	}
	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));
	}
}

Compilation message

bottle.cpp: In function 'int main()':
bottle.cpp:36:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   36 |  scanf("%d %d %d %d", &N, &M, &K, &Q);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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<=N; i++) scanf("%s", A[i] + 1);
      |                          ~~~~~^~~~~~~~~~~~~~~~
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<=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:64:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   64 |   scanf("%d %d", &x, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 5632 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 19384 KB Output is correct
2 Correct 43 ms 10796 KB Output is correct
3 Correct 352 ms 42580 KB Output is correct
4 Incorrect 492 ms 45220 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 19320 KB Output is correct
2 Incorrect 31 ms 9896 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 475 ms 44416 KB Output isn't correct
2 Halted 0 ms 0 KB -