-
Notifications
You must be signed in to change notification settings - Fork 0
/
wormholes.cpp
64 lines (53 loc) · 1.28 KB
/
wormholes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// USACO Trainer's "Wormholes"
/*
ID: warithr1
LANG: C++11
TASK: wormhole
*/
#include <bits/stdc++.h>
using namespace std;
int N, X[13], Y[13];
int partner[13], nxt[13]; // bruh "next" is an STL word smh
bool cycle() {
for (int i = 1; i <= N; i++) {
int pos = i;
for (int j = 0; j < N; ++j) {
pos = nxt[partner[pos]];
}
if (pos != 0) return true;
}
return false;
}
int solve() {
int i, tot = 0;
for (i = 1; i <= N; i++) {
if (partner[i] == 0) break;
}
if (i > N) return cycle();
for (int j = i+1; j <= N; ++j) {
if (partner[j] == 0) {
partner[i] = j; partner[j] = i;
tot += solve(); // yay recursion
partner[i] = partner[j] = 0;
}
}
return tot;
}
int main() {
freopen("wormhole.in", "r", stdin);
freopen("wormhole.out", "w", stdout);
cin >> N;
for (int i = 1; i <= N; i++) cin >> X[i] >> Y[i];
// pcomp some stuff
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; ++j) {
if (X[j] > X[i] && Y[i] == Y[j]) {
if (nxt[i] == 0 ||
X[j] - X[i] < X[nxt[i]] - X[i]) {
nxt[i] = j;
}
}
}
}
cout << solve() << '\n';
}