0% found this document useful (0 votes)
9 views

Ready Go Part 1 Omar Ahmed Omar Ahmed Source Code

The document contains C++ code for a problem involving finding connected components in a 2D grid and determining if there exists a path between land and water tiles. It defines structs for disjoint set union and includes headers, declares variables and functions, accepts input, performs DSU operations, and outputs the answer.

Uploaded by

braagamer82
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Ready Go Part 1 Omar Ahmed Omar Ahmed Source Code

The document contains C++ code for a problem involving finding connected components in a 2D grid and determining if there exists a path between land and water tiles. It defines structs for disjoint set union and includes headers, declares variables and functions, accepts input, performs DSU operations, and outputs the answer.

Uploaded by

braagamer82
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <bits/stdc++.

h>
#pragma GCC optimize("Ofast")
using namespace std ;
#define endl '\n'
#define all(a) a.begin() , a.end()
#define alr(a) a.rbegin() , a.rend()
struct DSU {
vector < int > par, sz;
void init(int n) {
par = sz = vector < int > (n + 1);
for(int i = 0 ; i <= n ; i++) {
par[i] = i, sz[i] = 1;
}
}

int find(int node) {


if(par[node] == node) return node;
return par[node] = find(par[node]);
}

void join(int u, int v) {


u = find(u), v = find(v);
if(u == v) return;
if(sz[v] > sz[u]) {
swap(u, v);
}
sz[u] += sz[v];
par[v] = u;
}
};
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);

int t; cin >> t;


for(int test = 1 ; test <= t ; test++) {
cout << "Case #" << test << ": ";
int n, m;
cin >> n >> m;
vector < vector < short >> a(n, vector < short > (m));
vector < vector < int >> b(n + 1, vector < int > (m + 1));
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
char ch; cin >> ch;
if(ch == 'W') {
a[i][j] = 1;
} else if(ch == 'B') {
a[i][j] = 2;
}
}
}
DSU s; s.init(n * m + 2);
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
if(a[i][j] == 1) {
if(i && a[i - 1][j] == 1) {
s.join(i * m + j, (i - 1) * m + j);
}
if(j && a[i][j - 1] == 1) {
s.join(i * m + j, i * m + j - 1);
}
}
}
}

map < int , pair < int , int >> mp;


for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < n ; j++) {
if(a[i][j] == 1) {
for(int k = 0 ; k < 4 ; k++) {
int nx = dx[k] + i;
int ny = dy[k] + j;
if(min(nx, ny) >= 0 && nx < n && ny < m && a[nx][ny] == 0)
{
pair < int , int > val = {nx, ny};
int root = s.find(i * m + j);
if(mp.find(root) != mp.end()) {
if(mp[root] != val) {
mp[root] = {-1, -1};
}
} else {
mp[root] = val;
}
}
}
}
}
}

for(auto i : mp) {
if(i.second != make_pair(-1, -1)) {
b[i.second.first][i.second.second] += s.sz[i.first];
}
}
int ans = 0;
for(auto i : b) for(auto j : i) ans = max(ans, j);
if(ans >= 1) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
return 0 ;
}

You might also like