-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsinistoses.java
More file actions
77 lines (58 loc) · 1.71 KB
/
sinistoses.java
File metadata and controls
77 lines (58 loc) · 1.71 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package xoria;
import java.util.*;
/**
*
* @author evi
*/
public class sinistoses {
public static int countComponents(int n, int m, int k, int[][] edges){
int teams;
int[] root = new int[n+1];
//root of each village is itself
for(int i=0; i<n+1; i++){
root[i] = i;
}
//create the graph as given
for(int i=0; i<m; i++) {
root = union(n, edges[i][0], edges[i][1], root);
}
//add new edges-creating min{#teams}
for(int i=0; i<k; i++){
int j=2;
while(root[1] == root[j]){
j += 1;
}
union(n, root[1], root[j], root);
}
//count and return min{#teams}
teams = counter(n, root);
return teams;
}
//counting teams
public static int counter(int nodes, int [] arr){
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i=1; i< nodes+1; i++) {
if(list.contains(arr[i]) == false) {
list.add(arr[i]);
}
}
int c = list.size();
return c;
}
//creating edge between a-b
public static int[] union(int n, int a, int b, int[] root) {
int ra = root[a];
int rb = root[b];
for(int j=1; j<n+1; j++){
if(root[j] == ra){
root[j] = rb;
}
}
return root;
}
}