-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathfriends-and-foes.java
46 lines (44 loc) · 1.02 KB
/
friends-and-foes.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
43
44
45
46
/* 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
{
int size[], parent[];
public static void union(int a, int b) {
int u = find(a);
int v = find(b);
if (u == v) return;
if (size[u] > size[v]) {
parent[v] = u;
size[u] += size[v];
} else {
parent[u] = v;
size[v] += size[u];
}
}
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
size = new int[n];
parent = new int[n];
for (int i = 0; i < n; i++) size[i] = 1;
int m1 = sc.nextInt();
while (m1--) {
int a = sc.nextInt(), b = sc.nextInt();
union(a,b);
}
int m2 = sc.nextInt();
while (m2--) {
int a = sc.nextInt(), b = sc.nextInt();
if (find(a) == find(b)) {
size[find(a)] = size[find(b)] = 0;
}
}
int ans = 0;
for (int i = 0; i < n; i++) ans = Math.max(ans, size[i]);
System.out.println(ans);
}
}