#P8403. Counting Group Rule Violations

    ID: 21580 Type: Default 1000ms 256MiB

Counting Group Rule Violations

Counting Group Rule Violations

A class is divided into \( g \) groups, and each group contains exactly three students. In forming these groups, two kinds of rules might be violated:

  1. Certain students must be in the same group.
  2. Certain students must not be in the same group.

The students are numbered from 1 to \(3g\). The groups are determined by the students' numbers: the \(i\)-th student belongs to group \(\lceil i/3 \rceil\). For each "must be together" pair, if the two students are not in the same group, it counts as one violation. For each "must be separate" pair, if the two students are in the same group, it counts as one violation. Given the grouping and these constraints, your task is to count the total number of violated rules.

inputFormat

The input is given in the following format:

 g
 m
 a1 b1
 a2 b2
 ...
 am bm
 n
 c1 d1
 c2 d2
 ...
 cn dn

Here,

  • \( g \) is the number of groups. The total number of students is \( 3g \).
  • \( m \) is the number of pairs of students that must be in the same group. Each of the following \( m \) lines contains two integers \( a_i \) and \( b_i \) (1 ≤ \( a_i, b_i \) ≤ \(3g\)).
  • \( n \) is the number of pairs of students that must not be in the same group. Each of the following \( n \) lines contains two integers \( c_i \) and \( d_i \) (1 ≤ \( c_i, d_i \) ≤ \(3g\)).

Note that the groups are fixed: the \(i\)-th student belongs to group \( \lceil i/3 \rceil \).

outputFormat

Output a single integer representing the total number of violated rules.

sample

3
2
1 2
4 5
2
2 4
7 8
1