• `Algorithm-Solution` `LeetCode` 6256. 将节点分成尽可能多的组


    link

    Solution

    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)

    Code

    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;
        }
    };
    
    • 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
  • 相关阅读:
    IO流 -- 调研
    DMRS的产生
    Ubuntu系统安装
    SpringMVC01
    网络第一颗
    unordered_set、unordered_map的介绍+使用+比较
    基于SpringBoot的在线聊天室系统,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
    JSP简介
    分享一个用python写的本地WIFI密码查看器
    零基础学Linux内核之设备驱动篇(8)_设备模型_概念篇
  • 原文地址:https://blog.csdn.net/qq_66485519/article/details/128186658