#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
const int maxn = 260;
vector < int > adj[maxn];
int used[maxn], edge[maxn][maxn];
int n;
void dfs(int v, int c)
{
used[v] = c;
for (int u = 0; u < n; u ++)
{
if (edge[v][u] != 1)
continue;
if (!used[u])
dfs(u, c);
}
}
vector < int > construct(int v, bool done)
{
for (int i = 0; i < n; i ++)
used[i] = 0;
int mx = -1;
for (int u = 0; u < n; u ++)
{
if (edge[v][u] == 1)
{
edge[v][u] = 2, mx = u;
edge[u][v] = 1;
}
}
if (mx == -1)
{
vector < int > vec;
vec.push_back(v);
return vec;
}bool split = false;
if (!done)
{dfs(mx, 1);
for (int u = 0; u < n; u ++)
{
if (edge[v][u] == 2 && used[u] == 0)
{
split = true;
}
if (edge[v][u] == 2)
edge[v][u] = 1;
}
}
if (!split)
{
for (int u = 0; u < n; u ++)
{
if (edge[v][u])
{
edge[v][u] = edge[u][v] = 0;
}
}
vector < int > res = construct(mx, true);
res.push_back(v);
return res;
}
int dx = -1;
for (int u = 0; u < n; u ++)
{
if (edge[v][u])
{
edge[v][u] = edge[u][v] = 0;
if (u != mx)
dx = u;
}
}
vector < int > res = construct(mx, true);
res.push_back(v);
vector < int > bk = construct(dx, true);
reverse(bk.begin(), bk.end());
for (int u : bk)
res.push_back(u);
return res;
}
vector<int> longest_trip(int N, int D)
{
n = N;
if (n == 0)
return {};
if (D == 3)
{
vector < int > r;
for (int i = 0; i < N; i ++)
r.push_back(i);
return r;
}
for (int i = 0; i < N; i ++)
for (int j = 0; j < N; j ++)
{
edge[i][j] = edge[j][i] = 0;
}
for (int i = 0; i < N; i ++)
used[i] = 0;
for (int i = 0; i < N; i ++)
for (int j = i + 1; j < N; j ++)
{
vector < int > a, b;
a.push_back(i);
b.push_back(j);
if (are_connected(a, b))
{
adj[i].push_back(j);
adj[j].push_back(i);
edge[i][j] = 1;
edge[j][i] = 1;
}
}
int cp = 0;
for (int i = 0; i < N; i ++)
{
if (!used[i])
dfs(i, ++ cp);
}
if (cp == 2)
{
vector < int > a, b;
for (int i = 0; i < N; i ++)
if (used[i] == 1)
a.push_back(i);
else
b.push_back(i);
if (a.size() > b.size())
return a;
return b;
}
vector < int > res = construct(0, false);
return res;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:102:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
102 | for (int i = 0; i < N; i ++)
| ^~~
longesttrip.cpp:107:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
107 | for (int i = 0; i < N; i ++)
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
208 KB |
Incorrect |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
208 KB |
Output is correct |
2 |
Correct |
2 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
328 KB |
Output is correct |
2 |
Correct |
37 ms |
340 KB |
Output is correct |
3 |
Correct |
152 ms |
580 KB |
Output is correct |
4 |
Correct |
478 ms |
1012 KB |
Output is correct |
5 |
Correct |
850 ms |
1672 KB |
Output is correct |
6 |
Correct |
12 ms |
208 KB |
Output is correct |
7 |
Incorrect |
6 ms |
208 KB |
Incorrect |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
332 KB |
Output is correct |
2 |
Correct |
36 ms |
360 KB |
Output is correct |
3 |
Correct |
160 ms |
540 KB |
Output is correct |
4 |
Correct |
397 ms |
988 KB |
Output is correct |
5 |
Correct |
875 ms |
1676 KB |
Output is correct |
6 |
Correct |
6 ms |
208 KB |
Output is correct |
7 |
Correct |
25 ms |
336 KB |
Output is correct |
8 |
Correct |
158 ms |
588 KB |
Output is correct |
9 |
Correct |
361 ms |
876 KB |
Output is correct |
10 |
Correct |
886 ms |
1712 KB |
Output is correct |
11 |
Correct |
833 ms |
1692 KB |
Output is correct |
12 |
Correct |
806 ms |
1688 KB |
Output is correct |
13 |
Correct |
686 ms |
1680 KB |
Output is correct |
14 |
Correct |
8 ms |
208 KB |
Output is correct |
15 |
Correct |
16 ms |
336 KB |
Output is correct |
16 |
Correct |
45 ms |
372 KB |
Output is correct |
17 |
Correct |
101 ms |
716 KB |
Output is correct |
18 |
Correct |
172 ms |
504 KB |
Output is correct |
19 |
Correct |
268 ms |
912 KB |
Output is correct |
20 |
Correct |
340 ms |
852 KB |
Output is correct |
21 |
Correct |
895 ms |
1740 KB |
Output is correct |
22 |
Correct |
846 ms |
1892 KB |
Output is correct |
23 |
Correct |
814 ms |
1836 KB |
Output is correct |
24 |
Correct |
850 ms |
1800 KB |
Output is correct |
25 |
Correct |
15 ms |
208 KB |
Output is correct |
26 |
Correct |
12 ms |
208 KB |
Output is correct |
27 |
Correct |
31 ms |
368 KB |
Output is correct |
28 |
Correct |
38 ms |
336 KB |
Output is correct |
29 |
Correct |
31 ms |
336 KB |
Output is correct |
30 |
Correct |
183 ms |
648 KB |
Output is correct |
31 |
Correct |
224 ms |
748 KB |
Output is correct |
32 |
Correct |
230 ms |
528 KB |
Output is correct |
33 |
Correct |
312 ms |
840 KB |
Output is correct |
34 |
Correct |
296 ms |
852 KB |
Output is correct |
35 |
Correct |
315 ms |
968 KB |
Output is correct |
36 |
Correct |
874 ms |
1760 KB |
Output is correct |
37 |
Correct |
823 ms |
1648 KB |
Output is correct |
38 |
Correct |
896 ms |
1616 KB |
Output is correct |
39 |
Correct |
760 ms |
1660 KB |
Output is correct |
40 |
Correct |
835 ms |
1392 KB |
Output is correct |
41 |
Correct |
845 ms |
1188 KB |
Output is correct |
42 |
Correct |
842 ms |
1148 KB |
Output is correct |
43 |
Correct |
803 ms |
1372 KB |
Output is correct |
44 |
Correct |
834 ms |
1348 KB |
Output is correct |
45 |
Incorrect |
1 ms |
208 KB |
Incorrect |
46 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
464 KB |
Output is correct |
2 |
Correct |
31 ms |
364 KB |
Output is correct |
3 |
Partially correct |
151 ms |
476 KB |
Output is partially correct |
4 |
Partially correct |
399 ms |
1108 KB |
Output is partially correct |
5 |
Partially correct |
832 ms |
1760 KB |
Output is partially correct |
6 |
Correct |
12 ms |
208 KB |
Output is correct |
7 |
Incorrect |
7 ms |
384 KB |
Incorrect |
8 |
Halted |
0 ms |
0 KB |
- |