684. Redundant Connection
https://leetcode.com/problems/redundant-connection/description/
Use map to do Union Find.
class Solution {
public:
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
unordered_map<int,int> parent;
for (const auto& e : edges) {
int p = findParent(parent, e[]);
int c = findParent(parent, e[]);
if (p == c)
return e;
parent[c] = p;
}
return vector<int>();
}
int findParent(unordered_map<int,int>& parents, int child) {
while (parents.count(child))
child = parents[child];
return child;
}
};
684. Redundant Connection的更多相关文章
- LN : leetcode 684 Redundant Connection
lc 684 Redundant Connection 684 Redundant Connection In this problem, a tree is an undirected graph ...
- [LeetCode] 684. Redundant Connection 冗余的连接
In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...
- leetcode 684. Redundant Connection
We are given a "tree" in the form of a 2D-array, with distinct values for each node. In th ...
- LeetCode 684. Redundant Connection 冗余连接(C++/Java)
题目: In this problem, a tree is an undirected graph that is connected and has no cycles. The given in ...
- 【LeetCode】684. Redundant Connection 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetco ...
- [LeetCode] 685. Redundant Connection II 冗余的连接之 II
In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...
- Leetcode之并查集专题-684. 冗余连接(Redundant Connection)
Leetcode之并查集专题-684. 冗余连接(Redundant Connection) 在本问题中, 树指的是一个连通且无环的无向图. 输入一个图,该图由一个有着N个节点 (节点值不重复1, 2 ...
- [LeetCode] Redundant Connection 冗余的连接
In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...
- [LeetCode] Redundant Connection II 冗余的连接之二
In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...
随机推荐
- css相关知识
display: block; "块级元素". display: inline; "行内元素". display: none; "在不删除元素的情况下 ...
- Javascript常见问题
倒计时 <Script Language="JavaScript"> var timedate= new Date("October 1,2002" ...
- ios Lable 添加删除线
遇到坑了: NSString *goodsPrice = @"230.39"; NSString *marketPrice = @"299.99"; NSStr ...
- GitHub上易于高效开发的Android开源项目TOP20--适合新手
1. android-async-http android-async-http是Android上的一个异步.基于回调的HTTP客户端开发包,建立在Apache的HttpClient库上. 2. an ...
- thinkphp搜索实现
视图: <html lang="zh-cn"><head> <meta charset="UTF-8"><title& ...
- 【extjs6学习笔记】1.13 初始: 模型
Ext JS包括数据包Ext.data包括处理从服务器保存和检索数据的类. 以下是Ext JS 6数据包中的重要类: Model (Ext.data.Model) Store (Ext.data.St ...
- python 多线程 生产者消费者
import threading import time import logging import random import Queue logging.basicConfig(level=log ...
- python3基础05(有关日期的使用1)
#!/usr/bin/env python# -*- coding:utf-8 -*- import timefrom datetime import datetime,timedelta,timez ...
- JavaScript_对象
1. 直接创建实例: //简单对象 var person1 = new Object(); person1.name = "Mike"; person1.age = 29; pe ...
- UOJ#122【NOI2013】树的计数
[NOI2013]树的计数 链接:http://uoj.ac/problem/122 按BFS序来,如果$B_i$与$B_{i-1}$必须在同一层,那么贡献为0,必须在不同层那么贡献为1,都可以贡献为 ...