-
Notifications
You must be signed in to change notification settings - Fork 20
/
monk-and-palindromes.java
42 lines (38 loc) · 1.12 KB
/
monk-and-palindromes.java
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
// https://www.hackerearth.com/practice/data-structures/disjoint-data-strutures/basics-of-disjoint-data-structures/practice-problems/algorithm/monk-and-palindromes/description/
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
static int parent[];
public int find(int a) {
while (a != parent[a]) a = parent[a];
return a;
}
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int q = sc.nextInt();
parent = new int[n+1];
for (int i = 1; i <= n; i++) parent[i] = i;
while (q-- > 0) {
int u = sc.nextInt();
int v = sc.nextInt();
u = Math.min(u, v); v = Math.max(u, v);
for (int i = u, j = v; i < j; i++, j--) {
int a = parent[i];
int b = parent[j];
if (a != b) parent[a] = b;
}
}
HashSet<Integer> hs = new HashSet<Integer>();
for (int i = 1; i <= n; i++) {
if (parent[i] == i) hs.add(i);
}
int ans = (int) Math.pow(10, hs.size());
System.out.println(ans);
}
}