-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathl_path.cpp
More file actions
145 lines (112 loc) · 2.63 KB
/
Copy pathl_path.cpp
File metadata and controls
145 lines (112 loc) · 2.63 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <cstdio>
#include <memory.h>
using namespace std;
int const maxN = 22;
unsigned n;
unsigned m;
// Contains for [vertex] its edges in some order
unsigned edges[maxN][maxN];
unsigned vertexDegree[maxN];
unsigned nearVertexSet[maxN];
unsigned func[1 << maxN];
int bestCount;
unsigned dfs(unsigned v, unsigned set) {
unsigned result = 1;
for (unsigned i = 0; i < vertexDegree[v]; i++)
{
int nextVertex = edges[v][i];
if ((set & (1 << nextVertex)) == 0) {
set |= 1 << nextVertex;
result += dfs(nextVertex, set);
}
}
return result;
}
/*
unsigned setElementNumber(unsigned set) {
unsigned result = 0;
for (int i = 0; i < 25 && set != 0; i++) {
result += set & 1;
set >>= 1;
}
return result;
}
*/
unsigned reachableVertexNumber(unsigned v, unsigned set) {
return dfs(v, set) - 1;
}
unsigned bestWaySize;
unsigned curVertexCnt;
unsigned parents[maxN];
unsigned bestParents[maxN];
unsigned bestWayEnd;
void go(unsigned v, unsigned parent, unsigned set) {
if ((func[set] & (1 << v)) != 0) {
return;
}
func[set] |= (1 << v);
if (curVertexCnt + reachableVertexNumber(v, set) <= bestWaySize) {
return;
}
parents[v] = parent;
bool hasContinuation = false;
for (unsigned i = 0; i < vertexDegree[v]; i++) {
unsigned next = edges[v][i];
if ((set & (1 << next)) == 0) {
hasContinuation = true;
set |= (1 << next);
curVertexCnt++;
go(next, v, set);
set ^= (1 << next);
curVertexCnt--;
}
}
if (!hasContinuation && curVertexCnt > bestWaySize) {
bestWaySize = curVertexCnt;
bestWayEnd = v;
memcpy(bestParents, parents, n * sizeof(unsigned));
}
}
int main() {
for (int i = 0; i < maxN; i++) {
nearVertexSet[i] = 1 << i;
}
FILE* in = fopen("path.in", "r");
fscanf(in, "%u %u", &n, &m);
for (unsigned i = 0; i < m; i++) {
unsigned curStartVertex;
unsigned curEndVertex;
fscanf(in, "%u %u", &curStartVertex, &curEndVertex);
curStartVertex--;
curEndVertex--;
if (
(nearVertexSet[curStartVertex] & (1 << curEndVertex))
==
0
) {
nearVertexSet[curStartVertex] |= 1 << curEndVertex;
edges[curStartVertex][ vertexDegree[curStartVertex] ] = curEndVertex;
vertexDegree[curStartVertex]++;
}
}
fclose(in);
bestWaySize = 0;
for (unsigned i = 0; i < n; i++) {
curVertexCnt = 1;
go(i, -1, (1 << i));
}
unsigned bestWay[maxN];
bestWay[0] = bestWayEnd;
for (unsigned i = 1; i < bestWaySize; i++) {
bestWay[i] = bestParents[bestWay[i - 1]];
}
FILE* out = fopen("path.out", "w");
fprintf(out, "%u\n", bestWaySize - 1);
for (int i = bestWaySize - 1; i >= 0; i--)
{
fprintf(out, "%u ", bestWay[i] + 1);
}
fclose(out);
return 0;
}