답안 #1063743

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1063743 2024-08-18T00:44:21 Z danielzhu Tracks in the Snow (BOI13_tracks) C++17
97.8125 / 100
2000 ms 696148 KB
#include <bits/stdc++.h>
using namespace std;
#define MX 4001
typedef long long LL;
typedef pair<int, int> PR;
typedef tuple<int, int, int> TP;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9 + 5;
const LL MAXV = (LL)1e18; 
using vi = vector<int>;
using vii = vector<vi>;

int H, W;
string md[MX]; //snow meadow
/*
 use Disjoint Sets to solve the problem.
 (1) Use BFS to find all CCs (each with the same footprint letter R or F) and assign each CC with
     an integer ID starting from 0.
 (2) Perform a scan on the meadow to discover connectitvity between adjcent CCs with different letters
 (3) A bipartite graph or tree, then BFS to find the height of the tree which is the answer.
 
 */

vector<set<int>> adj;


int solve() {
	cin >> H >> W;
	//read the snow meadow info
	for (int i = 0; i < H; i++)
		cin >> md[i]; 
	if (md[0][0] == '.')
		return 0;
  vector<vector<int>> id(H, vector<int>(W, -1));
	int t = 0;
	for (int i = 0; i < H; i++) 
		for (int j = 0; j < W; j++) {
			if (md[i][j] == '.' || id[i][j] != -1)
				continue;
			queue<PR> Q;
			Q.push({i, j});
			id[i][j] = t;
			//BFS to find the CC
      while (!Q.empty()) {
        int r = Q.front().first, c = Q.front().second;
        Q.pop();
				int dr = 0, dc = 1;
        for (int k = 0; k < 4; k++) {
					int nr = r + dr, nc = c + dc;
					if (nr >= 0 && nr < H && nc >= 0 && nc < W && 
					    md[nr][nc] == md[i][j] && id[nr][nc] == -1) {
					  id[nr][nc] = t;
            Q.push({nr, nc});						
					}
					swap(dr, dc);
					dc = -dc;
				}				
			}
      t++;//next CC			
		}

	adj.resize(t);
	
	//scan adjacency between CCs, to build the bipartite graph
	for (int i = 0; i < H; i++)
		for (int j = 0; j < W; j++) {
			if (id[i][j] == -1)
				continue;
			int dr = 0, dc = 1;
			for (int k = 0; k < 4; k++) {
				int r = i + dr, c = j + dc;
				if (r >= 0 && r < H && c >= 0 && c < W && id[r][c] != -1 && id[r][c] != id[i][j] && md[r][c] != md[i][j]) {
					adj[id[i][j]].insert(id[r][c]);
					adj[id[r][c]].insert(id[i][j]);
				}
				swap(dr, dc);
				dc = -dc;
			}
		}
	vector<int> d(t, -1);
	queue<int> Q2;
	Q2.push(0); //the root
	d[0] = 1;
	while (!Q2.empty()) {
		int u = Q2.front();
		Q2.pop();
		for (auto v : adj[u])
			if (d[v] == -1) {
				d[v] = d[u]+1;
				Q2.push(v);
			}
	}
	return *max_element(d.begin(), d.end());
}   


int main() {
	ios::sync_with_stdio(false);
  cin.tie(nullptr);
	cin.exceptions(cin.failbit);
  cout << solve() << endl;
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 8528 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 11 ms 1628 KB Output is correct
5 Correct 4 ms 2140 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 1 ms 856 KB Output is correct
10 Correct 5 ms 2140 KB Output is correct
11 Correct 5 ms 864 KB Output is correct
12 Correct 16 ms 3416 KB Output is correct
13 Correct 4 ms 2140 KB Output is correct
14 Correct 4 ms 2140 KB Output is correct
15 Correct 30 ms 8220 KB Output is correct
16 Correct 42 ms 8444 KB Output is correct
17 Correct 22 ms 7768 KB Output is correct
18 Correct 11 ms 1772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1628 KB Output is correct
2 Correct 109 ms 40052 KB Output is correct
3 Correct 585 ms 303184 KB Output is correct
4 Correct 140 ms 58192 KB Output is correct
5 Correct 898 ms 696132 KB Output is correct
6 Correct 1511 ms 162380 KB Output is correct
7 Correct 2 ms 1368 KB Output is correct
8 Correct 2 ms 1624 KB Output is correct
9 Correct 5 ms 1884 KB Output is correct
10 Correct 1 ms 860 KB Output is correct
11 Correct 1 ms 1116 KB Output is correct
12 Correct 3 ms 2140 KB Output is correct
13 Correct 107 ms 40116 KB Output is correct
14 Correct 66 ms 23636 KB Output is correct
15 Correct 52 ms 26032 KB Output is correct
16 Correct 60 ms 18772 KB Output is correct
17 Correct 298 ms 103512 KB Output is correct
18 Correct 268 ms 100148 KB Output is correct
19 Correct 143 ms 58192 KB Output is correct
20 Correct 139 ms 68428 KB Output is correct
21 Correct 423 ms 180052 KB Output is correct
22 Correct 894 ms 696148 KB Output is correct
23 Correct 579 ms 199252 KB Output is correct
24 Correct 397 ms 262484 KB Output is correct
25 Correct 1323 ms 410852 KB Output is correct
26 Correct 493 ms 62032 KB Output is correct
27 Correct 626 ms 83320 KB Output is correct
28 Correct 1508 ms 162392 KB Output is correct
29 Correct 1310 ms 128892 KB Output is correct
30 Correct 1083 ms 113232 KB Output is correct
31 Execution timed out 2066 ms 337344 KB Time limit exceeded
32 Correct 569 ms 100948 KB Output is correct