Skip to content

USACO 2016 December Contest Silver Division - Cities and States#

Problem link: here

Solution Author: Stefan Dascalescu

Problem Solution#

This is a simple exercise of obtaining the codes from the pairs given (city, state) and counting how many pairs are of each type. Then, for each new pair, we count how many "complementary" pairs exist.

Source code#

The source code in C++ can be seen below.

#include <fstream>
using namespace std;

int adj[26 * 26][26 * 26];

int main() {
    ifstream cin("citystate.in");
    ofstream cout("citystate.out");

    int n;
    cin >> n;
    long long ans = 0;
    for (int i = 1; i <= n; ++i) {
        string a, b;
        cin >> a >> b;
        int nra = (a[0] - 'A') * 26 + (a[1] - 'A');
        int nrb = (b[0] - 'A') * 26 + (b[1] - 'A');
        if (nra != nrb) {
            ans += adj[nrb][nra];
        }
        adj[nra][nrb]++;
    }
    cout << ans;
    return 0;
}