-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphComponents.java
More file actions
81 lines (69 loc) · 1.86 KB
/
GraphComponents.java
File metadata and controls
81 lines (69 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
https://www.hackerrank.com/challenges/components-in-graph
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
DisJoint graph = new DisJoint(2*n);
for(int k = 0 ; k<n ; k++){
int node1 = sc.nextInt();
int node2 = sc.nextInt();
graph.Union(node1,node2);
}
for(int i=1;i<=n;i++){
graph.Rank[i]=graph.Rank[graph.Find(i)];
}
int min=Integer.MAX_VALUE,max=Integer.MIN_VALUE;
for(int i=1;i<=n;i++){
if(graph.Rank[i]!=1){
min=Math.min(graph.Rank[i],min);
max=Math.max(graph.Rank[i],max);
}
}
System.out.print(min+" "+max);
}
}
class DisJoint{
public int Count;
public int[] Parent;
public int[] Rank;
public DisJoint(int count){
this.Count = count;
this.Parent = new int[this.Count+1];
this.Rank = new int[this.Count+1];
for (int i = 1; i < this.Count+1; i++) {
this.Parent[i] = i;
this.Rank[i] = 1;
}
}
public int Find(int i){
if(i == Parent[i]){
return Parent[i];
}
else{
int result = Find(Parent[i]);
Parent[i] = result;
return result;
}
}
public void Union(int a, int b){
int aroot = this.Find(a);
int broot = this.Find(b);
if (aroot == broot){
return;
}
if(aroot>broot){
int tmp = aroot;
aroot = broot;
broot = tmp;
}
this.Rank[aroot] += this.Rank[broot];
this.Parent[broot] = aroot;
}
}