0% found this document useful (0 votes)
11 views3 pages

Ready Go Part 2 Phares Hamad Sirph Source Code

The document discusses an algorithm to solve a problem involving connecting nodes on a grid based on character matches. It includes declarations of variables and functions to get the parent node, connect nodes, and preprocess and solve the given problem.

Uploaded by

braagamer82
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Ready Go Part 2 Phares Hamad Sirph Source Code

The document discusses an algorithm to solve a problem involving connecting nodes on a grid based on character matches. It includes declarations of variables and functions to get the parent node, connect nodes, and preprocess and solve the given problem.

Uploaded by

braagamer82
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<bits/stdc++.

h>
#pragma GCC optimize ("Ofast")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
typedef long long int lli;
typedef unsigned long long ull;
using namespace std;
using namespace __gnu_pbds;
template<class x>
using ordered_set = tree<x, null_type,less<x>,
rb_tree_tag,tree_order_statistics_node_update>;
ll mod=(ll)1e9+7;
ll mod1=998244353;
///the defines :)
#define endl '\n'
#define vi vector<int>
#define vll vector<ll>
#define ent(arr) for(int i=0;i<arr.size();i++)cin>>arr[i];
#define all(arr) arr.begin(),arr.end()
#define allr(arr) arr.rbegin(),arr.rend()
#define sz size()
#define int long long
int parent[9000003];
int siz[9000003];
int xd[3002][3002];
int get(int a)
{
if(parent[a]==a)return a;
return parent[a]=get(parent[a]);
}
void connect(int a,int b)
{
a=get(a);
b=get(b);
if(a==b)
return;
if(siz[a]>siz[b])swap(a,b);
siz[b]+=siz[a];
parent[a]=b;

}
int hello=0;
void preprocess() {}
void solve()
{
hello++;
cout<<"Case #"<<hello<<": ";
int n,m;
cin>>n>>m;
vector<string>arr(n);
ent(arr);
int x=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
xd[i][j]=x;
x++;
}
}
for(int i=0;i<=x;i++)
parent[i]=i,siz[i]=1;
int lol[n][m];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(arr[i][j]!='.'){
if(i+1<n&&arr[i+1][j]==arr[i][j])
connect(xd[i][j],xd[i+1][j]);
if(j+1<m&&arr[i][j+1]==arr[i][j])
connect(xd[i][j],xd[i][j+1]);
if(i-1>=0&&arr[i-1][j]==arr[i][j])
connect(xd[i][j],xd[i-1][j]);
if(j-1>=0&&arr[i][j-1]==arr[i][j])
connect(xd[i][j],xd[i][j-1]);
}
}
}
vector<set<int>>ans(x+2);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(arr[i][j]=='W')
{
if(i+1<n&&arr[i+1][j]=='.')
ans[get(xd[i][j])].insert(xd[i+1][j]);
if(j+1<m&&arr[i][j+1]=='.')
ans[get(xd[i][j])].insert(xd[i][j+1]);
if(i-1>=0&&arr[i-1][j]=='.')
ans[get(xd[i][j])].insert(xd[i-1][j]);
if(j-1>=0&&arr[i][j-1]=='.')
ans[get(xd[i][j])].insert(xd[i][j-1]);
}
}
}
int pog=0;
map<int,int>mp;
for(int i=0;i<=x;i++)
{
if(ans[i].sz==1)
{
mp[*ans[i].begin()]+=siz[i];
}
}
for(auto i:mp)pog=max(pog,i.second);
cout<<pog<<endl;
}
signed main()
{
freopen("ready_go_part_2_input.txt","r",stdin);
freopen("otput.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
preprocess();
//bla();
int t=1;
cin>>t;
while(t--)
solve();
}

You might also like