forked from Diusrex/UVA-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10160 Servicing Stations.cpp
120 lines (99 loc) · 3.34 KB
/
10160 Servicing Stations.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
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <vector>
#include <bitset>
using namespace std;
typedef bitset<35> bset;
// Is quite fast (top 3) with the least obvious speedup being only search in connected components
// (which shaved the last 0.02 seconds off)
// Also ensures that, if trying to skip node, that it can be completed
// Nothing really fancy other than those two
// A further improvement could be, for nodes that are only connected to a single other node who is not connected to a service station, know will just include the other guy
// This could be advanced quite far along the graph
int N;
bset allIncluded;
// Who is connected to
bset connections[35];
// Who may be connected after this node. Based on following checks
bset connectedAfter[35];
// To find connected components
void dfs(int node, bset &included)
{
included[node] = true;
for (int i = 0; i < N; ++i)
if (connections[node][i] && !included[i])
dfs(i, included);
}
void FindLowestCost(bset connected, int currentNode, int currentCount, int & bestCount)
{
// All are connected
if (connected == allIncluded)
{
bestCount = min(bestCount, currentCount);
return;
}
// No point in continuing
// If is equal to bestCount - 1, will need to add at least one more node
if (currentCount >= bestCount - 1)
return;
// Cannot continue. Don't think this should be reached
if (currentNode == N)
{
return;
}
// Try including this guy, but only if he will add any usefulness
bset whenAdded = connected | connections[currentNode];
if (connected != whenAdded)
{
FindLowestCost(whenAdded, currentNode + 1, currentCount + 1, bestCount);
}
// Try not including this guy. But only if there is still a valid solution after
if ((connected | connectedAfter[currentNode]) == allIncluded)
{
FindLowestCost(connected, currentNode + 1, currentCount, bestCount);
}
}
int main()
{
int M;
while (cin >> N >> M, N)
{
for (int i = 0; i < N; ++i)
{
connections[i].reset();
connections[i].set(i);
}
allIncluded.reset();
for (int i = 0; i < N; ++i)
allIncluded.set(i);
while (M--)
{
int i, j;
cin >> i >> j;
--i; --j;
connections[i].set(j);
connections[j].set(i);
}
// Since nothing can be reached after it
connectedAfter[N - 1].reset();
for (int i = N - 2; i >= 0; --i)
connectedAfter[i] = connectedAfter[i + 1] | connections[i + 1];
// Will match for the overallIncluded
bset overallIncluded;
int bestOverall = 0;
for (int i = 0; i < N; ++i)
{
if (!overallIncluded[i])
{
// Will need to include i, or part of its component
bset component;
dfs(i, component);
bset outsideComponent = allIncluded ^ component;
int componentBest = N;
FindLowestCost(outsideComponent, 0, 0, componentBest);
overallIncluded |= component;
bestOverall += componentBest;
}
}
cout << bestOverall << '\n';
}
}