-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl_path.c
144 lines (115 loc) · 2.52 KB
/
l_path.c
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
#include <stdio.h>
#include <memory.h>
int const maxN = 22;
unsigned n;
unsigned m;
// Contains for [vertex] its edges in some order
unsigned edges[22][22];
unsigned vertexDegree[22];
unsigned nearVertexSet[22];
unsigned func[1 << 22];
int bestCount;
unsigned bestWaySize;
unsigned curVertexCnt;
unsigned parents[22];
unsigned bestParents[22];
unsigned bestWayEnd;
unsigned curReachableVertexNumber;
unsigned tempSet;
unsigned set;
void dfs(unsigned v) {
unsigned i;
unsigned nextVertex;
unsigned shift;
for (i = 0; i < vertexDegree[v]; i++)
{
nextVertex = edges[v][i];
shift = 1 << nextVertex;
if ((tempSet & shift) == 0) {
tempSet |= shift;
curReachableVertexNumber++;
dfs(nextVertex);
}
}
}
unsigned reachableVertexNumber(unsigned v, unsigned set) {
tempSet = set;
curReachableVertexNumber = 0;
dfs(v);
return curReachableVertexNumber;
}
void go(unsigned v, unsigned parent) {
unsigned i;
unsigned next;
int hasContinuation = 0;
if ((func[set] & (1 << v)) != 0) {
return;
}
func[set] |= (1 << v);
if (curVertexCnt + reachableVertexNumber(v, set) <= bestWaySize) {
return;
}
parents[v] = parent;
for (i = 0; i < vertexDegree[v]; i++) {
next = edges[v][i];
if ((set & (1 << next)) == 0) {
hasContinuation = 1;
set |= (1 << next);
curVertexCnt++;
go(next, v);
set ^= (1 << next);
curVertexCnt--;
}
}
if (!hasContinuation && (curVertexCnt > bestWaySize)) {
bestWaySize = curVertexCnt;
bestWayEnd = v;
memcpy(bestParents, parents, n * sizeof(unsigned));
}
}
int main() {
int i;
FILE* in;
FILE* out;
unsigned curStartVertex;
unsigned curEndVertex;
unsigned bestWay[maxN];
for (i = 0; i < maxN; i++) {
nearVertexSet[i] = 1 << i;
}
in = fopen("path.in", "r");
fscanf(in, "%u %u", &n, &m);
for (i = 0; i < m; i++) {
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 (i = 0; i < n; i++) {
curVertexCnt = 1;
set = (1 << i);
go(i, -1);
}
bestWay[0] = bestWayEnd;
for (i = 1; i < bestWaySize; i++) {
bestWay[i] = bestParents[bestWay[i - 1]];
}
out = fopen("path.out", "w");
fprintf(out, "%u\n", bestWaySize - 1);
for (i = bestWaySize - 1; i >= 0; i--)
{
fprintf(out, "%u ", bestWay[i] + 1);
}
fclose(out);
return 0;
}