Connection Status:
Competition Arena > DQuads
TCCC '03 Finals · 2003-04-05 · by lars2520 · Brute Force
Class Name: DQuads
Return Type: int
Method Name: count
Arg Types: (vector<string>)
Problem Statement

Problem Statement

We have a list of airline flights (directed edges between two nodes in a graph), and want to determine how many distinct directed quadrilaterals (directed cycles of length 4, which contain 4 distinct nodes) there are in the flight graph such that the nodes on opposite corners of the quadrilateral are not connected in either direction. Thus, we would count the graph on the left, but not the one on the right (the diagonal edge can be directed either way, or both ways - it doesn't matter):
      o--->o      o--->o
      ^    ^      ^\   ^
      |    |      | \  |
      |    v      |  \ v
      o<-->o      o<-->o
You will be given a String[] flights representing the flights. Element i of flights is a single space delimited list of integers (without extra leading 0's or leading/trailing spaces), where each integer j in the list indicates that there is an edge from i to j. An integer, j may appear more than once in element i meaning that there are multiple flights from i to j, each of which should be considered distinct. However, regardless of how many flights there are from one node to another, a quadrilateral must contain exactly 4 distinct nodes. Two quadrilaterals are considered distinct if and only if the sets of flights that they use are distinct. Thus, there may be many quadrilaterals that use the same four nodes, but different edges. Your task is to write a class DQuads, with a method count, which returns the total number of distinct quadrilaterals.

Constraints

  • flights will contain between 4 and 50 elements, inclusive.
  • Each element of flights will be a single space delimited list of integers, with no extra leading 0's and no leading or trailing spaces.
  • Each integer in each element of flights will be between 0 and the length of flights - 1, inclusive.
  • Each element of flights will contain between 0 and 50 characters, inclusive.
  • Element i of flights will not contain the integer i as one of its terms.
Examples
0)
{"1 1 1 1 1 1 1 1 1 1","2","3","0"}
Returns: 10

This is just a single directed square. Since there are 10 edges from 0 to 1, there are 10 distinct, directed quadrilaterals.

1)
{"1 1 1 1 1 1 1 1 1 1","2","3","0 1"}
Returns: 0

This is the same as the last example, except with a cross edge from 3 to 1 included. The cross edge is in all the quadrilaterals from the previous example, and so none of them count any more.

2)
{"1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
"2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2",
"3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3",
"0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"}
Returns: 390625
3)
{"","6 0 2","","6 6 4","6 5 5 0","",""}
Returns: 0
4)
{"1", "0 2", "3 1", "0"}
Returns: 1

Submissions are judged against all 25 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class DQuads with a public method int count(vector<string> flights) · 25 test cases · 2 s / 256 MB per case

Submitting as anonymous