#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector <ll>;
using dll = deque <ll>;
using vi = vector <int>;
const ll MAXN = 256+16;
vll adj[MAXN];
bool mat[MAXN][MAXN];
ll comp[MAXN];
void dfs (ll u, ll col, ll &sz) {
if (comp[u]) return;
comp[u] = col;
sz++;
for (ll v : adj[u]) {
dfs(v, col, sz);
}
}
vi longest_trip (int n, int d) {
fill(adj, adj+n+5, vll({}));
fill(comp, comp+n+5, 0);
for (ll u = 0; u < n; u++) {
mat[u][u] = false;
for (ll v = u+1; v < n; v++) {
if (are_connected(vi({ int(u) }), vi({ int(v) }))) {
adj[u].push_back(v);
adj[v].push_back(u);
mat[u][v] = true;
mat[v][u] = true;
} else {
mat[u][v] = false;
mat[v][u] = false;
}
}
}
ll c = 1;
vll sz;
for (ll u = 0; u < n; u++) {
if (comp[u]) continue;
sz.push_back(0);
dfs(u, c++, sz.back());
}
assert(c == 2 || c == 3);
if (c == 2) {
dll dq;
dq.push_back(0);
vector <char> vis(n, false);
vis[0] = true;
for (ll v = 0; v < n; v++) {
if (mat[0][v]) {
vis[v] = true;
dq.push_back(v);
break;
}
}
assert(dq.size() == 2);
for (ll u = 0; u < n; u++) {
if (vis[u]) continue;
vis[u] = true;
if (mat[u][dq.front()]) {
dq.push_front(u);
} else if (mat[u][dq.back()]) {
dq.push_back(u);
}
}
vi ans;
for (ll i : dq) ans.push_back(i);
return ans;
} else if (c == 3) {
assert(sz.size() == 2);
if (sz[0] > sz[1]) {
vi ans;
for (ll u = 0; u < n; u++) {
if (comp[u] == 1) {
ans.push_back(u);
}
}
return ans;
} else {
vi ans;
for (ll u = 0; u < n; u++) {
if (comp[u] == 2) {
ans.push_back(u);
}
}
return ans;
}
}
}
Compilation message
longesttrip.cpp: In function 'vi longest_trip(int, int)':
longesttrip.cpp:41:9: warning: control reaches end of non-void function [-Wreturn-type]
41 | vll sz;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
175 ms |
1860 KB |
Incorrect |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
29 ms |
344 KB |
Output is correct |
3 |
Correct |
128 ms |
856 KB |
Output is correct |
4 |
Correct |
351 ms |
1300 KB |
Output is correct |
5 |
Correct |
727 ms |
1460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
23 ms |
600 KB |
Output is correct |
3 |
Correct |
138 ms |
1120 KB |
Output is correct |
4 |
Correct |
334 ms |
1112 KB |
Output is correct |
5 |
Correct |
684 ms |
1728 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
21 ms |
344 KB |
Output is correct |
8 |
Correct |
117 ms |
736 KB |
Output is correct |
9 |
Correct |
279 ms |
864 KB |
Output is correct |
10 |
Correct |
737 ms |
1844 KB |
Output is correct |
11 |
Correct |
757 ms |
1812 KB |
Output is correct |
12 |
Correct |
790 ms |
1432 KB |
Output is correct |
13 |
Correct |
848 ms |
1916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
600 KB |
Output is correct |
2 |
Correct |
24 ms |
344 KB |
Output is correct |
3 |
Correct |
171 ms |
600 KB |
Output is correct |
4 |
Correct |
368 ms |
1056 KB |
Output is correct |
5 |
Correct |
873 ms |
2080 KB |
Output is correct |
6 |
Correct |
7 ms |
596 KB |
Output is correct |
7 |
Correct |
27 ms |
344 KB |
Output is correct |
8 |
Correct |
177 ms |
852 KB |
Output is correct |
9 |
Correct |
294 ms |
1284 KB |
Output is correct |
10 |
Correct |
856 ms |
2020 KB |
Output is correct |
11 |
Correct |
840 ms |
2264 KB |
Output is correct |
12 |
Correct |
873 ms |
1772 KB |
Output is correct |
13 |
Correct |
826 ms |
2240 KB |
Output is correct |
14 |
Correct |
10 ms |
344 KB |
Output is correct |
15 |
Correct |
13 ms |
344 KB |
Output is correct |
16 |
Correct |
47 ms |
600 KB |
Output is correct |
17 |
Correct |
88 ms |
856 KB |
Output is correct |
18 |
Correct |
124 ms |
1132 KB |
Output is correct |
19 |
Correct |
277 ms |
1284 KB |
Output is correct |
20 |
Correct |
316 ms |
1644 KB |
Output is correct |
21 |
Correct |
716 ms |
1764 KB |
Output is correct |
22 |
Correct |
792 ms |
2036 KB |
Output is correct |
23 |
Correct |
858 ms |
2316 KB |
Output is correct |
24 |
Correct |
857 ms |
2244 KB |
Output is correct |
25 |
Correct |
16 ms |
504 KB |
Output is correct |
26 |
Correct |
12 ms |
344 KB |
Output is correct |
27 |
Correct |
29 ms |
596 KB |
Output is correct |
28 |
Correct |
29 ms |
600 KB |
Output is correct |
29 |
Correct |
30 ms |
344 KB |
Output is correct |
30 |
Correct |
207 ms |
972 KB |
Output is correct |
31 |
Correct |
231 ms |
1216 KB |
Output is correct |
32 |
Correct |
207 ms |
1364 KB |
Output is correct |
33 |
Correct |
287 ms |
1280 KB |
Output is correct |
34 |
Correct |
282 ms |
1008 KB |
Output is correct |
35 |
Correct |
348 ms |
856 KB |
Output is correct |
36 |
Correct |
887 ms |
2004 KB |
Output is correct |
37 |
Correct |
805 ms |
2168 KB |
Output is correct |
38 |
Correct |
866 ms |
1564 KB |
Output is correct |
39 |
Correct |
906 ms |
2120 KB |
Output is correct |
40 |
Correct |
900 ms |
1568 KB |
Output is correct |
41 |
Correct |
925 ms |
1464 KB |
Output is correct |
42 |
Correct |
740 ms |
1184 KB |
Output is correct |
43 |
Correct |
688 ms |
2404 KB |
Output is correct |
44 |
Correct |
740 ms |
2036 KB |
Output is correct |
45 |
Correct |
9 ms |
756 KB |
Output is correct |
46 |
Correct |
11 ms |
344 KB |
Output is correct |
47 |
Correct |
27 ms |
344 KB |
Output is correct |
48 |
Correct |
24 ms |
344 KB |
Output is correct |
49 |
Correct |
22 ms |
600 KB |
Output is correct |
50 |
Correct |
176 ms |
1012 KB |
Output is correct |
51 |
Incorrect |
8 ms |
1464 KB |
Incorrect |
52 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
344 KB |
Output is correct |
2 |
Correct |
24 ms |
344 KB |
Output is correct |
3 |
Partially correct |
126 ms |
1112 KB |
Output is partially correct |
4 |
Partially correct |
371 ms |
1236 KB |
Output is partially correct |
5 |
Partially correct |
757 ms |
1860 KB |
Output is partially correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
22 ms |
340 KB |
Output is correct |
8 |
Partially correct |
125 ms |
856 KB |
Output is partially correct |
9 |
Partially correct |
252 ms |
1388 KB |
Output is partially correct |
10 |
Partially correct |
758 ms |
2300 KB |
Output is partially correct |
11 |
Partially correct |
706 ms |
1900 KB |
Output is partially correct |
12 |
Partially correct |
780 ms |
1896 KB |
Output is partially correct |
13 |
Partially correct |
721 ms |
1808 KB |
Output is partially correct |
14 |
Correct |
6 ms |
344 KB |
Output is correct |
15 |
Incorrect |
2 ms |
344 KB |
Incorrect |
16 |
Halted |
0 ms |
0 KB |
- |