1579. Remove Max Number of Edges to Keep Graph Fully Traversable(Leetcode每日一题-2021.01.27)--抄答案

Problem

Alice and Bob have an undirected graph of n nodes and 3 types of edges:

  • Type 1: Can be traversed by Alice only.
  • Type 2: Can be traversed by Bob only.
  • Type 3: Can by traversed by both Alice and Bob.

Given an array edges where edges[i] = [typei, ui, vi] represents a bidirectional edge of type typei between nodes ui and vi, find the maximum number of edges you can remove so that after removing the edges, the graph can still be fully traversed by both Alice and Bob. The graph is fully traversed by Alice and Bob if starting from any node, they can reach all other nodes.

Return the maximum number of edges you can remove, or return -1 if it’s impossible for the graph to be fully traversed by Alice and Bob.

Constraints:

  • 1 <= n <= 10^5
  • 1 <= edges.length <= min(10^5, 3 * n * (n-1) / 2)
  • edges[i].length == 3
  • 1 <= edges[i][0] <= 3
  • 1 <= edges[i][1] < edges[i][2] <= n
  • All tuples (typei, ui, vi) are distinct.

Example1

在这里插入图片描述

Input: n = 4, edges = [[3,1,2],[3,2,3],[1,1,3],[1,2,4],[1,1,2],[2,3,4]]
Output: 2
Explanation: If we remove the 2 edges [1,1,2] and [1,1,3]. The graph will still be fully traversable by Alice and Bob. Removing any additional edge will not make it so. So the maximum number of edges we can remove is 2.

Example2

在这里插入图片描述

Input: n = 4, edges = [[3,1,2],[3,2,3],[1,1,4],[2,1,4]]
Output: 0
Explanation: Notice that removing any edge will not make the graph fully traversable by Alice and Bob.

Example3

在这里插入图片描述

Input: n = 4, edges = [[3,2,3],[1,1,2],[2,3,4]]
Output: -1
Explanation: In the current graph, Alice cannot reach node 4 from the other nodes. Likewise, Bob cannot reach 1. Therefore it’s impossible to make the graph fully traversable.

Solution

class Solution {
public:
    vector<int> par;
    int cnt;
    int getRoot(int x){
        int root = x;
        while(par[root]!=root){
            root = par[root];
        }
        while(par[x]!=root){
            int tmp = par[x];
            par[x] = root;
            x = tmp;
        }
        return root;
    }
    void merge(int x,int y){
        int _x = getRoot(x);
        int _y = getRoot(y);
        if(_x!=_y){
            par[_x]=_y;
            cnt--;
        }
    }
    //初始化
    void init(int n){
        //cnt为集合个数,初始化每个结点视为一个集合
        cnt = n;
        for(int i =1;i<=n;i++){
            par[i] = i;
        }
    }
    int maxNumEdgesToRemove(int n, vector<vector<int>>& edges) {
        par = vector<int>(n+1,0);
        int ans = 0;
        //分别存储第一种到第三种类型的边
        int cnt1 = 0,cnt2 = 0,cnt3 = 0;
        init(n);
        //判断对于Alice是否连通
        for(int i = 0;i<edges.size();i++){
            if(edges[i][0]==1||edges[i][0]==3){
                merge(edges[i][1],edges[i][2]);
                cnt1++;
            }
        }
        if(cnt!=1)
            return -1;
        init(n);
        //判断对于Bob是否连通
        for(int i = 0;i<edges.size();i++){
            if(edges[i][0]==2||edges[i][0]==3){
                merge(edges[i][1],edges[i][2]);
                cnt2++;
            }
        }
        if(cnt!=1)
            return -1;
        init(n);
        //添加第三种类型的边
        for(int i = 0;i<edges.size();i++){
            if(edges[i][0]==3){
                merge(edges[i][1],edges[i][2]);
                cnt3++;
            }
        }
        //去除第三种类型的边
        cnt1-=cnt3;
        cnt2-=cnt3;
        //多余的第三种类型的边
        ans+=(cnt3-(n-cnt));
        //多余的其余两种类型的边
        ans += cnt1-(cnt-1)+cnt2-(cnt-1);
        
        return ans;
    }
};

//作者:hust_dhc
//链接:https://leetcode-cn.com/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable/solution/bing-cha-ji-zheng-ming-zui-zhong-di-san-chong-lei-/
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 程序猿惹谁了 设计师:白松林 返回首页