답안 #1063741

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1063741 2024-08-18T00:43:14 Z danielzhu Tracks in the Snow (BOI13_tracks) C++17
93.4375 / 100
2000 ms 1048576 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<unordered_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 34 ms 10840 KB Output is correct
2 Correct 0 ms 600 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 9 ms 2136 KB Output is correct
5 Correct 5 ms 2652 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 6 ms 2652 KB Output is correct
11 Correct 2 ms 860 KB Output is correct
12 Correct 14 ms 4356 KB Output is correct
13 Correct 5 ms 2652 KB Output is correct
14 Correct 6 ms 2884 KB Output is correct
15 Correct 25 ms 10980 KB Output is correct
16 Correct 35 ms 10832 KB Output is correct
17 Correct 25 ms 10296 KB Output is correct
18 Correct 8 ms 2140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1880 KB Output is correct
2 Correct 149 ms 55764 KB Output is correct
3 Correct 874 ms 422596 KB Output is correct
4 Correct 164 ms 78928 KB Output is correct
5 Runtime error 934 ms 1048576 KB Execution killed with signal 9
6 Correct 1085 ms 213628 KB Output is correct
7 Correct 3 ms 1884 KB Output is correct
8 Correct 3 ms 1884 KB Output is correct
9 Correct 4 ms 2396 KB Output is correct
10 Correct 2 ms 1116 KB Output is correct
11 Correct 2 ms 1372 KB Output is correct
12 Correct 4 ms 3164 KB Output is correct
13 Correct 114 ms 55728 KB Output is correct
14 Correct 63 ms 32596 KB Output is correct
15 Correct 83 ms 38228 KB Output is correct
16 Correct 55 ms 25680 KB Output is correct
17 Correct 320 ms 144460 KB Output is correct
18 Correct 366 ms 147588 KB Output is correct
19 Correct 167 ms 78876 KB Output is correct
20 Correct 155 ms 95328 KB Output is correct
21 Correct 397 ms 251240 KB Output is correct
22 Runtime error 1524 ms 1048576 KB Execution killed with signal 9
23 Correct 850 ms 277848 KB Output is correct
24 Correct 513 ms 391504 KB Output is correct
25 Execution timed out 2021 ms 606844 KB Time limit exceeded
26 Correct 492 ms 62048 KB Output is correct
27 Correct 631 ms 84860 KB Output is correct
28 Correct 1153 ms 213672 KB Output is correct
29 Correct 982 ms 160332 KB Output is correct
30 Correct 843 ms 135504 KB Output is correct
31 Correct 1881 ms 432060 KB Output is correct
32 Correct 591 ms 112720 KB Output is correct