-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP10054.cpp
108 lines (98 loc) · 2.35 KB
/
P10054.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
struct Node {
Node *prev, *next;
PI ele;
};
std::ostream& operator<<(std::ostream& os, const Node& n) {
os << n.ele.first << " " << n.ele.second;
return os;
}
int main() {
int m, a, b;
Node* lastToColor[51];
set<Node*> adjacent[51];
Node list[1010];
FORCAS {
if(cas != 0)
cout << endl;
cout << "Case #" << (cas+1) << endl;
// Reset data:
FORI(51) {
adjacent[i].clear();
lastToColor[i] = NULL;
}
// Read data:
cin >> m;
FORI(m) {
cin >> a >> b;
list[i].ele = PI(a,b);
if(i == 0)
continue;
adjacent[a].insert(&list[i]);
if(a != b)
adjacent[b].insert(&list[i]);
}
Node *first = &list[0];
lastToColor[first->ele.second] = first;
// Always maintain a closed necklace:
first->next = first;
first->prev = first;
// Try to reconstruct necklace:
bool ok = true;
Node *last = first;
int retSize = 1;
while(retSize < m && ok) {
int lastColor = last->ele.second;
if(adjacent[lastColor].empty()) {
// Find one that can be stitched in:
bool foundAny = false;
FORI(51) {
if(adjacent[i].empty() || lastToColor[i] == NULL)
continue;
first = lastToColor[i]->next;
last = lastToColor[i];
//cerr << "New first: " << *first << ", last: " << *last << endl;
lastColor = i;
foundAny = true;
break;
}
if(!foundAny) {
ok = false;
break;
}
}
Node* node = retSize%2 == 1 ?
*adjacent[lastColor].begin() :
*adjacent[lastColor].rbegin();
adjacent[lastColor].erase(node);
//cerr << "-> " << *node << endl;
int nextColor = node->ele.first == last->ele.second ? node->ele.second : node->ele.first;
if(lastColor != nextColor)
adjacent[nextColor].erase(node);
if(node->ele.second != nextColor)
swap(node->ele.first, node->ele.second); // Redirect node.
// Stitch in node:
++retSize;
last->next = node;
node->prev = last;
node->next = first;
first->prev = node;
last = node;
// Update aux. structures:
lastToColor[nextColor] = node;
}
if(last->ele.second != first->ele.first) {
ok = false;
}
if(ok) {
Node *cur = first;
do {
cout << *cur << endl;
cur = cur->next;
} while(cur != first);
}
else {
cout << "some beads may be lost" << endl;
}
} // FORCAS
return 0;
}