#include <bits/stdc++.h>
#include "longesttrip.h"
using namespace std;
typedef long long ll;
vector<vector<ll>> adj;
vector<ll> dist;
vector<bool> vst;
ll numVst;
void dfs1(ll cur, ll depth) {
dist[cur] = depth;
vst[cur] = true;
numVst++;
for (auto &e : adj[cur]) {
if (!vst[e]) dfs1(e, depth+1);
}
}
void dfs2(ll cur, ll depth) {
dist[cur] = depth;
vst[cur] = true;
for (auto &e : adj[cur]) {
if (!vst[e]) dfs2(e, depth+1);
}
}
vector<int> longest_trip(int n, int d) {
adj = vector<vector<ll>>(n);
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
if (are_connected({i}, {j})) {
adj[i].push_back(j);
adj[j].push_back(i);
}
}
}
dist = vector<ll>(n);
vst = vector<bool>(n);
numVst = 0;
dfs1(0, 0);
ll start = 0;
if (numVst <= n/2) {
for (int i = 0; i < n; i++) {
if (!vst[i]) {
start = i;
break;
}
}
}
else {
ll mx = 0;
for (int i = 0; i < n; i++) {
if (dist[i] > mx) {
start = i;
mx = dist[i];
}
}
}
dist = vector<ll>(n);
vst = vector<bool>(n);
dfs2(start, 0);
ll cur = 0;
ll mx = 0;
for (int i = 0; i < n; i++) {
if (dist[i] > mx) {
cur = i;
mx = dist[i];
}
}
vector<int> res = {(int)cur};
while (dist[cur]) {
for (auto &e : adj[cur]) {
if (dist[e] == dist[cur]-1) {
cur = e;
res.push_back(e);
break;
}
}
}
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
160 ms |
1144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
18 ms |
344 KB |
Output is correct |
3 |
Correct |
127 ms |
344 KB |
Output is correct |
4 |
Correct |
387 ms |
780 KB |
Output is correct |
5 |
Correct |
737 ms |
1244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
20 ms |
344 KB |
Output is correct |
3 |
Correct |
128 ms |
344 KB |
Output is correct |
4 |
Correct |
348 ms |
832 KB |
Output is correct |
5 |
Correct |
706 ms |
1256 KB |
Output is correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Correct |
21 ms |
344 KB |
Output is correct |
8 |
Correct |
145 ms |
620 KB |
Output is correct |
9 |
Correct |
274 ms |
1024 KB |
Output is correct |
10 |
Correct |
766 ms |
1148 KB |
Output is correct |
11 |
Correct |
806 ms |
1504 KB |
Output is correct |
12 |
Correct |
778 ms |
1012 KB |
Output is correct |
13 |
Correct |
768 ms |
1332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
23 ms |
344 KB |
Output is correct |
3 |
Correct |
128 ms |
600 KB |
Output is correct |
4 |
Correct |
377 ms |
688 KB |
Output is correct |
5 |
Correct |
783 ms |
1256 KB |
Output is correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Correct |
22 ms |
344 KB |
Output is correct |
8 |
Correct |
122 ms |
472 KB |
Output is correct |
9 |
Correct |
300 ms |
776 KB |
Output is correct |
10 |
Correct |
830 ms |
1176 KB |
Output is correct |
11 |
Correct |
871 ms |
1508 KB |
Output is correct |
12 |
Correct |
822 ms |
1292 KB |
Output is correct |
13 |
Correct |
787 ms |
1040 KB |
Output is correct |
14 |
Correct |
5 ms |
344 KB |
Output is correct |
15 |
Correct |
10 ms |
344 KB |
Output is correct |
16 |
Correct |
44 ms |
344 KB |
Output is correct |
17 |
Correct |
84 ms |
344 KB |
Output is correct |
18 |
Correct |
125 ms |
596 KB |
Output is correct |
19 |
Correct |
327 ms |
600 KB |
Output is correct |
20 |
Correct |
290 ms |
592 KB |
Output is correct |
21 |
Correct |
843 ms |
1224 KB |
Output is correct |
22 |
Correct |
854 ms |
1252 KB |
Output is correct |
23 |
Correct |
902 ms |
1052 KB |
Output is correct |
24 |
Correct |
792 ms |
1448 KB |
Output is correct |
25 |
Correct |
7 ms |
344 KB |
Output is correct |
26 |
Correct |
12 ms |
344 KB |
Output is correct |
27 |
Correct |
26 ms |
344 KB |
Output is correct |
28 |
Correct |
24 ms |
344 KB |
Output is correct |
29 |
Correct |
18 ms |
344 KB |
Output is correct |
30 |
Correct |
164 ms |
468 KB |
Output is correct |
31 |
Correct |
179 ms |
708 KB |
Output is correct |
32 |
Correct |
171 ms |
600 KB |
Output is correct |
33 |
Correct |
318 ms |
604 KB |
Output is correct |
34 |
Correct |
340 ms |
784 KB |
Output is correct |
35 |
Correct |
299 ms |
1004 KB |
Output is correct |
36 |
Correct |
812 ms |
1572 KB |
Output is correct |
37 |
Correct |
800 ms |
1496 KB |
Output is correct |
38 |
Correct |
835 ms |
1392 KB |
Output is correct |
39 |
Correct |
851 ms |
1172 KB |
Output is correct |
40 |
Correct |
820 ms |
1396 KB |
Output is correct |
41 |
Correct |
906 ms |
976 KB |
Output is correct |
42 |
Correct |
881 ms |
1376 KB |
Output is correct |
43 |
Correct |
791 ms |
1184 KB |
Output is correct |
44 |
Correct |
790 ms |
1312 KB |
Output is correct |
45 |
Correct |
12 ms |
344 KB |
Output is correct |
46 |
Correct |
12 ms |
344 KB |
Output is correct |
47 |
Correct |
31 ms |
344 KB |
Output is correct |
48 |
Correct |
26 ms |
344 KB |
Output is correct |
49 |
Correct |
29 ms |
344 KB |
Output is correct |
50 |
Correct |
194 ms |
720 KB |
Output is correct |
51 |
Correct |
198 ms |
600 KB |
Output is correct |
52 |
Correct |
213 ms |
452 KB |
Output is correct |
53 |
Correct |
282 ms |
980 KB |
Output is correct |
54 |
Correct |
262 ms |
856 KB |
Output is correct |
55 |
Correct |
270 ms |
764 KB |
Output is correct |
56 |
Correct |
737 ms |
1240 KB |
Output is correct |
57 |
Correct |
715 ms |
1312 KB |
Output is correct |
58 |
Correct |
697 ms |
1172 KB |
Output is correct |
59 |
Correct |
699 ms |
1244 KB |
Output is correct |
60 |
Correct |
749 ms |
1244 KB |
Output is correct |
61 |
Correct |
839 ms |
1004 KB |
Output is correct |
62 |
Correct |
767 ms |
1292 KB |
Output is correct |
63 |
Correct |
808 ms |
1244 KB |
Output is correct |
64 |
Correct |
772 ms |
1088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
21 ms |
344 KB |
Output is correct |
3 |
Partially correct |
155 ms |
464 KB |
Output is partially correct |
4 |
Partially correct |
341 ms |
992 KB |
Output is partially correct |
5 |
Partially correct |
752 ms |
1036 KB |
Output is partially correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
17 ms |
344 KB |
Output is correct |
8 |
Partially correct |
124 ms |
460 KB |
Output is partially correct |
9 |
Partially correct |
264 ms |
808 KB |
Output is partially correct |
10 |
Partially correct |
810 ms |
1260 KB |
Output is partially correct |
11 |
Partially correct |
760 ms |
1256 KB |
Output is partially correct |
12 |
Partially correct |
794 ms |
1268 KB |
Output is partially correct |
13 |
Partially correct |
851 ms |
1184 KB |
Output is partially correct |
14 |
Correct |
4 ms |
344 KB |
Output is correct |
15 |
Correct |
10 ms |
344 KB |
Output is correct |
16 |
Incorrect |
1 ms |
344 KB |
Incorrect |
17 |
Halted |
0 ms |
0 KB |
- |