#P3405. [USACO16DEC] Cities and States S

[USACO16DEC] Cities and States S

Description

Farmer John has several cows. To train their intelligence, Farmer John put a map of the United States on the barn wall. The map shows each city and the code of the state it belongs to (2 uppercase letters).

Because the cows spend a lot of time looking at the map, they start to notice some strange relationships. For example, the first two letters of FLINT are the state of MIAMI, FL, and the first two letters of MIAMI are the state of FLINT, MI. Precisely, for two cities, the first two letters of each city's name are equal to the other city's state code.

We call two cities a "special" pair if they have the property above and they come from different states. Given NN total cities, the cows want to know how many "special" pairs exist. Please help them solve this interesting geography puzzle.

Input Format

The input consists of N+1N + 1 lines.

The first line contains a positive integer NN, the number of cities on the map.
Each of the next NN lines contains two strings: a city name (22 to 1010 uppercase letters) and the code of its state (22 uppercase letters). Within the same state, there are no two cities with the same name.

Output Format

Output a single line with one integer, the number of special city pairs.

6
MIAMI FL
DALLAS TX
FLINT MI
CLEMSON SC
BOSTON MA
ORLANDO FL
1

Hint

Constraints: For 100%100\% of the testdata, 1N2×1051 \le N \le 2 \times 10^5, and the city name length is at most 1010.

Translated by ChatGPT 5