#P1444. [USACO1.3] 虫洞 wormhole

[USACO1.3] 虫洞 wormhole

Description

Farmer John's high-energy physics experiment over the weekend backfired, causing nn wormholes to appear on the farm, which is a two-dimensional plane. No two wormholes are at the same position.

According to his calculations, FJ knows the wormholes are paired up, forming n2\dfrac{n}{2} pairs. For example, if wormholes AA and BB form a pair, any object entering wormhole AA will exit from wormhole BB with its direction unchanged, and vice versa.

However, this can lead to a rather unpleasant consequence. For instance, suppose there are two paired wormholes A(1,1)A(1,1) and B(3,1)B(3,1), and Bessie starts at (2,1)(2,1) moving in the positive xx direction. Bessie will enter wormhole B(3,1)B(3,1), exit from A(1,1)A(1,1), then enter BB again, getting trapped in an infinite loop!

FJ knows the exact position of every wormhole on his farm. He knows Bessie always moves in the positive xx direction, although he does not remember Bessie's current position.

Please help FJ count how many pairing schemes of wormholes have the property that there exists a position such that if Bessie starts there, she will be trapped in an infinite loop.

Input Format

The first line contains a positive integer nn, the number of wormholes.

Each of the next nn lines contains two integers x,yx, y, the coordinates of a wormhole.

Output Format

Output a single integer, the answer.

4
0 0
1 0
1 1
0 1
2

Hint

Constraints

For 100% of the testdata, 2n122 \le n \le 12, 0x,y1090 \le x,y \le 10^9.
It is guaranteed that nn is even.

Sample Explanation

Number the wormholes 11 to 44. If we pair 1,21,2 and 3,43,4, then if Bessie starts anywhere between (0,0)(0,0) and (1,0)(1,0), she will fall into an infinite loop.

Similarly, with the same starting point, if the pairings are 1,31,3 and 2,42,4, Bessie will also be trapped in a loop. (If Bessie enters through 33 and exits from 11, she will move toward 22, then be teleported to 44, and finally return to 33.)

Only the pairing 1,41,4 and 2,32,3 allows Bessie to walk in the positive xx direction from any point in the plane without entering an infinite loop.

Problem statement translation from NOCOW.

Translated by ChatGPT 5