Under construction

Still being built. Things may change

Why Did the Cow Cross the Road 6

Interview

Time limit2sMemory limit512 MB

Summary
Given an N x N grid of pastures, some adjacent pairs blocked by roads, and K cows on distinct cells, count the pairs of cows that cannot reach each other without crossing a road.
Level

Medium5 of 10

Topics
Graph, BFS, Union-find, Implementation
Solved
No attempts yet

Problem

The reason the cow crossed the road is simply that there are so many roads. John's farm has so many roads that a cow can hardly move around without crossing one.

John's farm was recently reorganized. It is now an N×NN \times N grid of small square pastures (2≤N≤1002 \le N \le 100). A cow can usually walk freely between two adjacent pastures, but some adjacent pairs are separated by a road that the cow must cross to go between them. A tall fence surrounds the farm, so no cow ever leaves it.

There are KK cows on John's farm (1≤K≤1001 \le K \le 100, K≤N2K \le N^2), each in a different pasture. Some pairs of cows cannot meet unless one of them crosses a road. Count how many such pairs there are.

Input

The first line contains NN, KK, and RR.

Each of the next RR lines describes one road. A road joins two pastures that are adjacent vertically or horizontally, and it is given as rr cc r′r' c′c' (row, column, row, column). Every number is between 11 and NN, inclusive.

Each of the next KK lines gives the position of one cow as a row and a column.

Output

Print the number of pairs of cows that cannot meet without crossing a road.

Examples1

  1. Example 1

    Input
    3 3 3
    2 2 2 3
    3 3 3 2
    3 3 2 3
    3 3
    2 2
    2 3
    
    Expected output
    2