Given a undirected unweighted graph
G
G
G, if we divide it into
S
1
,
S
2
,
.
.
.
S1, S2, ...
S1,S2,... groups, each group has a unique depth, that is,
D
e
p
t
h
(
S
i
)
=
i
Depth( Si) = i
Depth(Si)=i, every point
a
a
a belongs to a unique group
S
(
a
)
S( a)
S(a).
Then any edge between
a
a
a and
b
b
b satisfying
∣
D
e
p
t
h
(
S
(
a
)
)
−
D
e
p
t
h
(
S
(
b
)
)
∣
=
1
| Depth( S( a)) - Depth( S(b)) | = 1
∣Depth(S(a))−Depth(S(b))∣=1, or you can view groups as a layered-graph, where
S
1
S1
S1 at the top, all edges are connecting two adjacent groups.
Find the maximal number of group-count.
Firstly, because
G
G
G may contains multiple connected graph
G
i
Gi
Gi, we can handle
G
i
Gi
Gi alone, then together them.
. For instance, if
G
1
G1
G1 has
3
3
3 groups and
G
2
G2
G2 has
2
2
2 groups, then
G
G
G has
5
5
5 groups where the front
3
3
3 groups denote
G
1
G1
G1, and so on.
Then, the question is, how many groups can a
G
i
Gi
Gi divides into at maximum (or it can’t be grouped (you can use Bipartite-Check, because if we let
L
=
S
1
,
S
3
,
S
5
,
.
.
.
L = S1, S3, S5, ...
L=S1,S3,S5,... and
R
=
S
2
,
S
4
,
.
.
.
R = S2, S4, ...
R=S2,S4,..., it must be a Bipartite. In fact, this case is not necessary handled by this way, it can be embedded into the below approach))
If
G
i
Gi
Gi can not be grouped,
G
G
G is also could not be grouped (unsolvable)
G
i
Gi
Gi may be grouped in different ways (i.e., with different count of groups)
. One prominent property for
G
i
Gi
Gi is, if
G
i
Gi
Gi can be grouped,
S
1
S1
S1 (the first/top group) must can contains just one point. (if
S
1
=
(
a
,
.
.
.
)
S1 = (a, ...)
S1=(a,...),
.
.
.
...
... can be putted into
S
3
S3
S3)
So, we choose a point
s
s
s as the start-point (the only point is
S
1
S1
S1), perform BFS_1 from
s
s
s with
d
e
p
t
h
[
s
]
=
1
depth[ s] = 1
depth[s]=1, then if
d
e
p
t
h
[
n
e
x
]
≠
−
1
depth[ nex] \neq -1
depth[nex]=−1, it must satisfies
∣
d
e
p
t
h
[
n
e
x
]
−
d
e
p
t
h
[
c
u
r
]
∣
=
1
| depth[ nex] - depth[cur] | = 1
∣depth[nex]−depth[cur]∣=1 (otherwise, it is unsolvable), the deepest depth is the group-count. (e.g., 1-2-3, BFS(2) will get
2
2
2, BFS(1/3) will get
3
3
3, so
m
a
x
(
2
,
3
,
3
)
max( 2, 3, 3)
max(2,3,3) is the optimal scheme for
G
i
Gi
Gi.)
Formally, let
f
a
fa
fa be the Disjoint-Set root for
G
i
Gi
Gi, each
B
F
S
(
i
)
BFS(i)
BFS(i) with the deepest depth
d
d
d is used to update
M
d
[
f
a
]
=
m
a
x
(
s
e
l
f
,
d
)
Md[ fa] = max( self, d)
Md[fa]=max(self,d) (
M
d
[
f
a
]
Md[fa]
Md[fa] means the optimal group-division for
G
i
Gi
Gi which is represented by
f
a
fa
fa) (once
B
F
S
(
i
)
BFS(i)
BFS(i) is failed, then
G
G
G is unsolvable)
class Solution {
public:
int D[ 502];
int Md[ 502];
int magnificentSets(int N, vector<vector<int>>& A) {
Graph_UnWeighted g( N, A.size() * 2);
g.Initialize( N);
DisjointSet ss( N);
ss.Initialize( N);
for( auto & i : A){
int a = i[0], b = i[1];
-- a, -- b;
g.Add_edge( a, b);
g.Add_edge( b, a);
ss.Merge( a, b);
}
memset( Md, 0, sizeof( Md));
for( int i = 0; i < N; ++i){
queue< int> que;
que.push( i);
memset( D, -1, sizeof( D));
int aa = 1;
D[ i] = 1;
while( false == que.empty()){
int cur = que.front();
que.pop();
for( int nex, e = g.Head[ cur]; ~e; e = g.Next[ e]){
nex = g.Vertex[ e];
if( D[ nex] == -1){
D[ nex] = D[ cur] + 1;
aa = max( aa, D[ nex]);
que.push( nex);
}
else{
if( ( D[ nex] != D[ cur] - 1) && ( D[ nex] != D[ cur] + 1)){
return -1;
}
}
}
}
int fa = ss.Get_root( i);
Md[ fa] = max( Md[ fa], aa);
}
int ans = 0;
for( int i = 0; i < N; ++i){
if( i == ss.Get_root( i)){
ans += Md[ i];
}
}
return ans;
}
};