-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP208.cpp
executable file
·71 lines (64 loc) · 1.35 KB
/
P208.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
bool visited[20], reachVisit[20];
vector<int> G[20];
int FIRE, S[20], sizeS;
bool reachable(int from) {
if(from == FIRE)
return true;
reachVisit[from] = true;
FORIT(vector<int>, G[from]) {
int n = *it;
if(visited[n] || reachVisit[n])
continue;
if(reachable(n))
return true;
}
return false;
}
int go(int from) {
if(from == FIRE) {
printf("%d", S[0]+1);
for(int i = 1; i < sizeS; ++i)
printf(" %d", S[i]+1);
printf(" %d\n", FIRE+1);
return 1;
}
// Check reachable:
memset(reachVisit, false, sizeof(reachVisit));
if(!reachable(from))
return 0;//*/
visited[from] = true;
S[sizeS++] = from;
int ret = 0;
FORIT(vector<int>, G[from]) {
int n = *it;
if(!visited[n])
ret += go(n);
}
visited[from] = false;
--sizeS;
return ret;
}
int main() {
for(int cas = 1; cin >> FIRE; ++cas) {
// Reset:
--FIRE;
FORI(20) {
G[i].clear();
visited[i] = false;
}
sizeS = 0;
//Read data:
while(true) {
GI(a); GI(b); if(a == 0 && b == 0) break;
G[a-1].push_back(b-1);
G[b-1].push_back(a-1);
}
FORI(20)
sort(G[i].begin(), G[i].end()); // Ensure DFS goes
// GO!
printf("CASE %d:\n", cas);
int ret = go(0);
printf("There are %d routes from the firestation to streetcorner %d.\n", ret, FIRE+1);
}
return 0;
}