-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gfg_hard_Making_A_Large_Island.cpp
90 lines (89 loc) · 2.43 KB
/
Gfg_hard_Making_A_Large_Island.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class Solution
{
public:
class DisjointSet
{
public:
vector<int> parent,size;
DisjointSet(int n)
{
parent.resize(n+1);
for(int i=0;i<=n;i++) parent[i]=i;
size.resize(n+1,1);
}
int findParent(int u)
{
if(parent[u]==u) return u;
return parent[u]=findParent(parent[u]);
}
void Union(int u,int v)
{
u=findParent(u);
v=findParent(v);
if(size[u]<size[v])
{
parent[u]=v;
size[v]+=size[u];
}
else
{
parent[v]=u;
size[u]+=size[v];
}
}
};
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
int largestIsland(vector<vector<int>>& grid) {
int n=grid.size();
int gsize=n*n;
DisjointSet ds(gsize);
int maxsize=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(grid[i][j]==0) continue;
int curr=i*n+j;
for(int k=0;k<4;k++)
{
int ci=i+dx[k],cj=j+dy[k];
if(ci<0 || cj<0 || ci>=n || cj>=n || grid[ci][cj]==0) continue;
int adj=ci*n+cj;
if(ds.findParent(curr)!=ds.findParent(adj)) ds.Union(curr,adj);
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(grid[i][j]==0)
{
int curr=i*n+j;
set<int> st;
for(int k=0;k<4;k++)
{
int ci=i+dx[k],cj=j+dy[k];
int adj=ci*n+cj;
if(ci<0 || cj<0 || ci>=n || cj>=n || grid[ci][cj]==0) continue;
st.insert(ds.findParent(adj));
}
int currsize=1;
for(auto it:st)
{
currsize+=ds.size[it];
}
maxsize=max(maxsize,currsize);
}
else
{
int curr=i*n+j;
int uparent=ds.findParent(curr);
maxsize=max(maxsize,ds.size[uparent]);
}
}
}
return maxsize;
}
};