[LintCode] Connecting Graph III

Given n nodes in a graph labeled from 1 to n. There is no edges in the graph at beginning.

You need to support the following method:
1. connect(a, b), an edge to connect node a and node b
2. query(), Returns the number of connected component in the graph

Example

5 // n = 5
query() return 5
connect(1, 2)
query() return 4
connect(2, 4)
query() return 3
connect(1, 4)
query() return 3

 1 class UnionFind {
 2     private int[] father = null;
 3     private int count = 0;
 4     public UnionFind(int n){
 5         father = new int[n];
 6         count = n;
 7         for(int i = 0; i < n; i++){
 8             father[i] = i;
 9         }
10     }
11     public int find(int x){
12         if(father[x] == x){
13             return x;
14         }
15         return father[x] = find(father[x]);
16     }
17     public void connect(int a, int b){
18         int root_a = find(a);
19         int root_b = find(b);
20         if(root_a != root_b){
21             father[root_a] = root_b;
22             count--;
23         }
24     }
25     public int queryUnionCount(){
26         return count;
27     }
28 }
29 public class ConnectingGraph3 {
30     private UnionFind uf = null;
31     public ConnectingGraph3(int n) {
32         uf = new UnionFind(n);
33     }
34
35     public void connect(int a, int b) {
36         uf.connect(a - 1, b - 1);
37     }
38
39     public int query() {
40         return uf.queryUnionCount();
41     }
42 }
 
时间: 2024-10-21 03:20:03

[LintCode] Connecting Graph III的相关文章

[LintCode] Connecting Graph

Given n nodes in a graph labeled from 1 to n. There is no edges in the graph at beginning. You need to support the following method: 1. connect(a, b), add an edge to connect node a and node b. 2.query(a, b)`, check if two nodes are connected Example

Lintcode - Maximum Subarray III

Given an array of integers and a number k, find k non-overlappingsubarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. http://www.lintcode.com/en/problem/maximum-subarray-iii/ Thought: 一开始以为这和

[LintCode] Clone Graph

Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label an

Lintcode: Single Number III

Given 2*n + 2 numbers, every numbers occurs twice except two, find them. Example Given [1,2,2,3,4,4,5,3] return 1 and 5 Challenge O(n) time, O(1) extra space. 利用bitwise XOR的特点,n个数(0或1),如果1的个数为奇数,则n个数bitwise XOR结果为1,否则为0 先将所有的数异或,得到的将是x和y以后之后的值n. 找到这个

GYM - 100814 C.Connecting Graph

题意: 初始有n个点,m次操作.每次操作加一条边或者询问两个点第一次连通的时刻(若不连通输出-1). 题解: 用并查集维护每个点所在连通块的根.对于每次加边,暴力的更新新的根. 每次将2个块合并时,将小的块并向大的块.这么合并使得每个点的根最多更新log2n次,并储存每次更新信息(更新时刻以及新的根). 对于每一次询问,二分两个点第一次连通的时刻.对于每一个二分的时刻,求的是两点的根是否相同. 由于存储过了每个点根的更新信息,所以再用二分求出他这个时刻的根. #include <bits/std

[LintCode] Surrounded Regions

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O''s into 'X''s in that surrounded region. Have you met this question in a real interview? Yes Example X X X X X O O X X X O X X O X

通过案例对SparkStreaming透彻理解三板之二

1.解密Spark Streaming运行机制 2.解密Spark Streaming架构 I.Spark Core是基于RDD形成的,RDD之间都会有依赖关系,Spark Streaming在RDD上的时间维度,DStream就是在RDD的基础之上加上了时间维度.DStream就是RDD的模板,随着时间的流逝不断地实例化DStream,以数据进行填充DStream Graph,静态的RDD DAG模板,这个模板就是DStream Graph, II.基于DStream 的依赖构造成DStrea

我所知道的面试手册

达内 Java 企业面试题精选 达内 20 本教材里面的最后一本,偏 Java 语言和 Web 框架. Github 下载 Gitee 下载 传智播客面试宝典 传智播客总结的面试宝典,里面有 Android.C++.Java.PHP.大数据五种. CSDN 下载 数据结构与算法/leetcode/lintcode题解 本文档为数据结构和算法学习笔记,全文大致分为以下三大部分: Part I为数据结构和算法基础,介绍一些基础的排序/链表/基础算法 Part II为 OJ 上的编程题目实战,按题目的

【Lintcode】137.Clone Graph

题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. How we serialize an undirected graph: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each