国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專(zhuān)欄INFORMATION COLUMN

261. Graph Valid Tree

Jinkey / 981人閱讀

摘要:題目鏈接檢查圖的連通性及是否有環(huán),可以,,從一個(gè)點(diǎn)出發(fā)看能不能遍歷所有的點(diǎn),同時(shí)來(lái)檢查是否有環(huán)。還可以用檢查是否有環(huán),最后看的數(shù)量是否等于來(lái)判斷是不是。

261. Graph Valid Tree

題目鏈接:https://leetcode.com/problems...

檢查圖的連通性及是否有環(huán),可以dfs,bfs,從一個(gè)點(diǎn)出發(fā)看能不能遍歷所有的點(diǎn),同時(shí)visited來(lái)檢查是否有環(huán)。還可以用union find檢查是否有環(huán),最后看edge的數(shù)量是否等于n-1來(lái)判斷是不是spinning tree。

public class Solution {
    public boolean validTree(int n, int[][] edges) {
        if(edges.length != n - 1) return false;
        
        map = new int[n];
        Arrays.fill(map, -1);
        // union find
        for(int[] edge : edges) {
            int root1 = find(edge[0]);
            int root2 = find(edge[1]);
            // if connected before, there is a cycle
            if(root1 == root2) return false;
            // union
            map[root1] = root2;
        }
        return true;
    }
    int[] map;
    
    private int find(int child) {
        while(map[child] != -1) child = map[child];
        return child;
    }
}

dfs注意要保留parent指針,這樣防止出現(xiàn)u->v之后又返回查一遍

public class Solution {
    public boolean validTree(int n, int[][] edges) {
        // build graph
        build(edges, n);
        // store visited node
        Set visited = new HashSet();
        // dfs from 0, to check if visited all nodes and if cycle
        if(dfs(visited, 0, -1)) return false;
        return visited.size() == n;
    }
    
    // adjacent list to represent graph
    List> graph;
    private void build(int[][] edges, int n) {
        graph = new ArrayList();
        for(int i = 0; i < n; i++) graph.add(new HashSet());
        for(int[] edge : edges) {
            graph.get(edge[0]).add(edge[1]);
            graph.get(edge[1]).add(edge[0]);
        }
    }
    
    private boolean dfs(Set visited, int u, int parent) {
        // has cycle
        if(visited.contains(u)) return true;
        visited.add(u);
        for(int v : graph.get(u)) {
            if(v == parent) continue;
            if(dfs(visited, v, u)) return true;
        }
        return false;
    }
}

bfs同樣要要注意避免走重復(fù)路經(jīng)的問(wèn)題,遍歷過(guò)的點(diǎn)就刪掉。

public class Solution {
    public boolean validTree(int n, int[][] edges) {
        // build graph
        build(edges, n);
        // store visited node
        Set visited = new HashSet();
        // bfs from 0, to check if visited all nodes and if cycle
        return bfs(visited, n);
    }
    
    // adjacent list to represent graph
    List> graph;
    private void build(int[][] edges, int n) {
        graph = new ArrayList();
        for(int i = 0; i < n; i++) graph.add(new HashSet());
        for(int[] edge : edges) {
            graph.get(edge[0]).add(edge[1]);
            graph.get(edge[1]).add(edge[0]);
        }
    }
    
    private boolean bfs(Set visited, int n) {
        Queue q = new LinkedList();
        q.offer(0);
        while(!q.isEmpty()) {
            int u = q.poll();
            if(visited.contains(u)) return false;
            visited.add(u);
            for(int v : graph.get(u)) {
                q.offer(v);
                // remove parent, avoid duplicate
                graph.get(v).remove(u);
            }
        }
        
        return visited.size() == n;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/69875.html

相關(guān)文章

  • [Leetcode] Graph Valid Tree 圖與樹(shù)

    摘要:這樣就將一個(gè)集合的節(jié)點(diǎn)歸屬到同一個(gè)集合號(hào)下了。最后如果并查集中只有一個(gè)集合,則說(shuō)明可以構(gòu)建樹(shù)。 Graph Valid Tree Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to check w...

    luqiuwen 評(píng)論0 收藏0
  • [Leetcode] Graph Valid Tree 判斷一個(gè)圖是否為樹(shù)

    摘要:只有一個(gè)連通分量還沒(méi)有環(huán),就是樹(shù),無(wú)根樹(shù)。無(wú)向圖邊的兩端是對(duì)稱(chēng)的,無(wú)向圖講究連通這個(gè)概念,沒(méi)有方向,沒(méi)有拓?fù)洌芟窦希苑浅_m合用并查集來(lái)解決。并查集寫(xiě)法參考注意方法用找的老大哥,合并的是的老大哥。 Graph Valid Tree Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each...

    xbynet 評(píng)論0 收藏0
  • [LeetCode] Graph Valid Tree [Union Find]

    Problem Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree. Example Given n = 5 and...

    104828720 評(píng)論0 收藏0
  • 【LC總結(jié)】Union Find系列(Num of Islands I&II/Graph V

    摘要:不得不說(shuō),對(duì)于這道題而言,是一種很木訥的模板。主函數(shù)按矩陣大小建立一個(gè)類(lèi)進(jìn)行后續(xù)操作一個(gè)結(jié)點(diǎn)用來(lái)連接邊角不可能被圍繞的一個(gè)函數(shù)對(duì)矩陣元素進(jìn)行結(jié)點(diǎn)線(xiàn)性化。同理,,,在主函數(shù)中初始化后調(diào)用。 Surrounded Regions Problem Given a 2D board containing X and O (the letter O), capture all regions s...

    bergwhite 評(píng)論0 收藏0
  • vue+iview之select和時(shí)間日期的使用

    摘要:實(shí)現(xiàn)從后臺(tái)獲取數(shù)據(jù),并賦值默認(rèn)值,同時(shí)也可以對(duì)選框進(jìn)行更改,即實(shí)現(xiàn)雙向綁定使用和方式實(shí)現(xiàn)雙向綁定,如下請(qǐng)選擇開(kāi)始時(shí)間和結(jié)束時(shí)間獲取默認(rèn)時(shí)間將后臺(tái)傳回的默認(rèn)時(shí)間數(shù)據(jù)放在時(shí)間選擇框內(nèi)按照后臺(tái)傳回的數(shù)據(jù),將斜杠前的時(shí)間作為初始時(shí)間按照 實(shí)現(xiàn)從后臺(tái)獲取數(shù)據(jù),并賦值默認(rèn)值,同時(shí)也可以對(duì)選框進(jìn)行更改,即實(shí)現(xiàn)雙向綁定 1、使用value和on-change方式實(shí)現(xiàn)雙向綁定,html如下: js...

    tainzhi 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<