#include <bits/stdc++.h>
#include "longesttrip.h"
#define N 1005
using namespace std;
vector <int> v[N], v1;
int c[N], k, y;
bool vis[N][N], vi[N];
void dfs(int x){
vi[x] = 1;
if(k < c[x]) y = x;
k = max(c[x], k);
for(auto i : v[x]){
if(vi[i] == 0){
c[i] = c[x] + 1;
dfs(i);
}
}
}
void df(int x, int z){
v1.push_back(x);
if(x == z) return;
for(auto i : v[x]){
if(c[i] == c[x] - 1){
df(i,z);
break;
}
}
}
vector<int> longest_trip(int n, int d) {
vector <int> a(1), b(1);
for(int i = 0; i < n; i++) v[i].clear();
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
vis[i][j] = 0;
}
}
for(int i = 0; i < n; i++){
for(int j = i+1; j < n; j++){
a[0] = i;
b[0] = j;
if(are_connected(a, b) and vis[i][j] == 0) {
vis[i][j] = 1;
vis[j][i] = 1;
v[i].push_back(j);
v[j].push_back(i);
}
}
}
v1.clear();
int mx = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++) vi[j] = c[j] = 0;
k = y = 0;
c[i] = 1;
dfs(i);
if(k > mx){
mx = k;
v1.clear();
df(y,i);
}
}
reverse(v1.begin(), v1.end());
return v1;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:60:49: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
60 | for(int j = 0; j < n; j++) vi[j] = c[j] = 0;
| ~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
217 ms |
1076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
31 ms |
344 KB |
Output is correct |
3 |
Correct |
155 ms |
344 KB |
Output is correct |
4 |
Correct |
452 ms |
1104 KB |
Output is correct |
5 |
Correct |
965 ms |
1056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
22 ms |
344 KB |
Output is correct |
3 |
Correct |
147 ms |
600 KB |
Output is correct |
4 |
Correct |
421 ms |
1080 KB |
Output is correct |
5 |
Correct |
892 ms |
1444 KB |
Output is correct |
6 |
Correct |
10 ms |
344 KB |
Output is correct |
7 |
Correct |
17 ms |
344 KB |
Output is correct |
8 |
Correct |
158 ms |
344 KB |
Output is correct |
9 |
Correct |
312 ms |
612 KB |
Output is correct |
10 |
Correct |
958 ms |
1104 KB |
Output is correct |
11 |
Correct |
954 ms |
1448 KB |
Output is correct |
12 |
Correct |
922 ms |
1276 KB |
Output is correct |
13 |
Correct |
934 ms |
880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
23 ms |
344 KB |
Output is correct |
3 |
Correct |
147 ms |
344 KB |
Output is correct |
4 |
Correct |
441 ms |
880 KB |
Output is correct |
5 |
Correct |
945 ms |
1360 KB |
Output is correct |
6 |
Correct |
6 ms |
344 KB |
Output is correct |
7 |
Correct |
22 ms |
344 KB |
Output is correct |
8 |
Correct |
147 ms |
344 KB |
Output is correct |
9 |
Correct |
356 ms |
600 KB |
Output is correct |
10 |
Correct |
876 ms |
1112 KB |
Output is correct |
11 |
Correct |
969 ms |
1156 KB |
Output is correct |
12 |
Correct |
957 ms |
1168 KB |
Output is correct |
13 |
Execution timed out |
1002 ms |
1264 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
344 KB |
Output is correct |
2 |
Correct |
32 ms |
344 KB |
Output is correct |
3 |
Partially correct |
187 ms |
788 KB |
Output is partially correct |
4 |
Partially correct |
469 ms |
848 KB |
Output is partially correct |
5 |
Partially correct |
897 ms |
1604 KB |
Output is partially correct |
6 |
Correct |
9 ms |
344 KB |
Output is correct |
7 |
Correct |
20 ms |
344 KB |
Output is correct |
8 |
Partially correct |
171 ms |
344 KB |
Output is partially correct |
9 |
Partially correct |
336 ms |
840 KB |
Output is partially correct |
10 |
Partially correct |
905 ms |
996 KB |
Output is partially correct |
11 |
Partially correct |
897 ms |
888 KB |
Output is partially correct |
12 |
Partially correct |
993 ms |
1064 KB |
Output is partially correct |
13 |
Partially correct |
951 ms |
1152 KB |
Output is partially correct |
14 |
Correct |
7 ms |
344 KB |
Output is correct |
15 |
Correct |
8 ms |
344 KB |
Output is correct |
16 |
Incorrect |
6 ms |
344 KB |
Incorrect |
17 |
Halted |
0 ms |
0 KB |
- |