forked from kamyu104/AOAPCII-BeginingAlgorithmContests2nd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVa1587.cpp
91 lines (80 loc) · 2.32 KB
/
UVa1587.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) 2015 kamyu. All rights reserved.
/*
* UVa1587 - Box
* http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=830&page=show_problem&problem=4462
*
* Time : O(1)
* Space : O(1)
*
*/
#include <iostream>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <utility>
using std::cin;
using std::cout;
using std::endl;
using std::unordered_set;
using std::unordered_map;
using std::hash;
using std::pair;
using std::make_pair;
using std::swap;
struct HashPair {
size_t operator()(const pair<int, int>& p) const {
return hash<int>()(p.first) ^ hash<int>()(p.second);
}
};
// Make sure all of (w, h) has pairs.
bool CheckArea(const unordered_map<pair<int, int>, int, HashPair>& area) {
return all_of(area.cbegin(), area.cend(),
[](const pair<pair<int, int>, int>& p) {
return p.second % 2 == 0;
});
}
// Make sure at most 3 different lengths of w, h, l,
// and all pairs of l, w, h exist.
bool CheckEdge(const unordered_map<pair<int, int>, int, HashPair>& area,
const unordered_set<int>& edge) {
if (edge.size() <= 3) {
for (auto it = edge.cbegin(); it != edge.cend(); ++it) {
auto jt = it;
++jt;
for (; jt != edge.cend(); ++jt) {
// Some of (w, h) does not exist.
if (area.find(make_pair(*it, *jt)) == area.cend() &&
area.find(make_pair(*jt, *it)) == area.cend()) {
return false;
}
}
}
return true;
}
return false;
}
void CheckBox(const unordered_map<pair<int, int>, int, HashPair>& area,
const unordered_set<int>& edge) {
if (CheckArea(area) && CheckEdge(area, edge)) {
cout << "POSSIBLE" << endl;
} else {
cout << "IMPOSSIBLE" << endl;
}
}
int main() {
int w, h;
while (cin >> w >> h) {
unordered_map<pair<int, int>, int, HashPair> area;
unordered_set<int> edge;
int count = 6;
do {
if (w > h) {
swap(w, h);
}
++area[make_pair(w, h)];
edge.insert(w), edge.insert(h);
} while (--count && cin >> w >> h);
CheckBox(area, edge);
}
return 0;
}