返回介绍

solution / 0600-0699 / 0685.Redundant Connection II / README_EN

发布于 2024-06-17 01:03:35 字数 6773 浏览 0 评论 0 收藏 0

685. Redundant Connection II

中文文档

Description

In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) for which all other nodes are descendants of this node, plus every node has exactly one parent, except for the root node which has no parents.

The given input is a directed graph that started as a rooted tree with n nodes (with distinct values from 1 to n), with one additional directed edge added. The added edge has two different vertices chosen from 1 to n, and was not an edge that already existed.

The resulting graph is given as a 2D-array of edges. Each element of edges is a pair [ui, vi] that represents a directed edge connecting nodes ui and vi, where ui is a parent of child vi.

Return _an edge that can be removed so that the resulting graph is a rooted tree of_ n _nodes_. If there are multiple answers, return the answer that occurs last in the given 2D-array.

 

Example 1:

Input: edges = [[1,2],[1,3],[2,3]]
Output: [2,3]

Example 2:

Input: edges = [[1,2],[2,3],[3,4],[4,1],[1,5]]
Output: [4,1]

 

Constraints:

  • n == edges.length
  • 3 <= n <= 1000
  • edges[i].length == 2
  • 1 <= ui, vi <= n
  • ui != vi

Solutions

Solution 1

class UnionFind:
  def __init__(self, n):
    self.p = list(range(n))
    self.n = n

  def union(self, a, b):
    if self.find(a) == self.find(b):
      return False
    self.p[self.find(a)] = self.find(b)
    self.n -= 1
    return True

  def find(self, x):
    if self.p[x] != x:
      self.p[x] = self.find(self.p[x])
    return self.p[x]


class Solution:
  def findRedundantDirectedConnection(self, edges: List[List[int]]) -> List[int]:
    n = len(edges)
    p = list(range(n + 1))
    uf = UnionFind(n + 1)
    conflict = cycle = None
    for i, (u, v) in enumerate(edges):
      if p[v] != v:
        conflict = i
      else:
        p[v] = u
        if not uf.union(u, v):
          cycle = i
    if conflict is None:
      return edges[cycle]
    v = edges[conflict][1]
    if cycle is not None:
      return [p[v], v]
    return edges[conflict]
class Solution {
  public int[] findRedundantDirectedConnection(int[][] edges) {
    int n = edges.length;
    int[] p = new int[n + 1];
    for (int i = 0; i <= n; ++i) {
      p[i] = i;
    }
    UnionFind uf = new UnionFind(n + 1);
    int conflict = -1, cycle = -1;
    for (int i = 0; i < n; ++i) {
      int u = edges[i][0], v = edges[i][1];
      if (p[v] != v) {
        conflict = i;
      } else {
        p[v] = u;
        if (!uf.union(u, v)) {
          cycle = i;
        }
      }
    }
    if (conflict == -1) {
      return edges[cycle];
    }
    int v = edges[conflict][1];
    if (cycle != -1) {
      return new int[] {p[v], v};
    }
    return edges[conflict];
  }
}

class UnionFind {
  public int[] p;
  public int n;

  public UnionFind(int n) {
    p = new int[n];
    for (int i = 0; i < n; ++i) {
      p[i] = i;
    }
    this.n = n;
  }

  public boolean union(int a, int b) {
    int pa = find(a);
    int pb = find(b);
    if (pa == pb) {
      return false;
    }
    p[pa] = pb;
    --n;
    return true;
  }

  public int find(int x) {
    if (p[x] != x) {
      p[x] = find(p[x]);
    }
    return p[x];
  }
}
class UnionFind {
public:
  vector<int> p;
  int n;

  UnionFind(int _n)
    : n(_n)
    , p(_n) {
    iota(p.begin(), p.end(), 0);
  }

  bool unite(int a, int b) {
    int pa = find(a), pb = find(b);
    if (pa == pb) return false;
    p[pa] = pb;
    --n;
    return true;
  }

  int find(int x) {
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
  }
};

class Solution {
public:
  vector<int> findRedundantDirectedConnection(vector<vector<int>>& edges) {
    int n = edges.size();
    vector<int> p(n + 1);
    for (int i = 0; i <= n; ++i) p[i] = i;
    UnionFind uf(n + 1);
    int conflict = -1, cycle = -1;
    for (int i = 0; i < n; ++i) {
      int u = edges[i][0], v = edges[i][1];
      if (p[v] != v)
        conflict = i;
      else {
        p[v] = u;
        if (!uf.unite(u, v)) cycle = i;
      }
    }
    if (conflict == -1) return edges[cycle];
    int v = edges[conflict][1];
    if (cycle != -1) return {p[v], v};
    return edges[conflict];
  }
};
type unionFind struct {
  p []int
  n int
}

func newUnionFind(n int) *unionFind {
  p := make([]int, n)
  for i := range p {
    p[i] = i
  }
  return &unionFind{p, n}
}

func (uf *unionFind) find(x int) int {
  if uf.p[x] != x {
    uf.p[x] = uf.find(uf.p[x])
  }
  return uf.p[x]
}

func (uf *unionFind) union(a, b int) bool {
  if uf.find(a) == uf.find(b) {
    return false
  }
  uf.p[uf.find(a)] = uf.find(b)
  uf.n--
  return true
}

func findRedundantDirectedConnection(edges [][]int) []int {
  n := len(edges)
  p := make([]int, n+1)
  for i := range p {
    p[i] = i
  }
  uf := newUnionFind(n + 1)
  conflict, cycle := -1, -1
  for i, e := range edges {
    u, v := e[0], e[1]
    if p[v] != v {
      conflict = i
    } else {
      p[v] = u
      if !uf.union(u, v) {
        cycle = i
      }
    }
  }
  if conflict == -1 {
    return edges[cycle]
  }
  v := edges[conflict][1]
  if cycle != -1 {
    return []int{p[v], v}
  }
  return edges[conflict]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文