#include <bits/stdc++.h>
#include "longesttrip.h"
using namespace std;
typedef long long ll;
vector<vector<ll>> adj, noAdj;
vector<bool> vst;
vector<vector<ll>> k;
void dfs(ll cur, ll comp) {
vst[cur] = true;
k[comp].push_back(cur);
for (auto &e : noAdj[cur]) {
if (vst[e]) continue;
dfs(e, !comp);
}
}
vector<int> longest_trip(int n, int d) {
adj = vector<vector<ll>>(n);
noAdj = vector<vector<ll>>(n);
k = vector<vector<ll>>(2);
vector<vector<bool>> mat(n, vector<bool>(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);
mat[i][j] = true;
mat[j][i] = true;
}
else {
noAdj[i].push_back(j);
noAdj[j].push_back(i);
}
}
}
vst = vector<bool>(n);
ll comp = 0;
for (int i = 0; i < n; i++) {
if (vst[i]) continue;
dfs(i, comp);
comp = !comp;
}
if (k[0].size() < k[1].size()) swap(k[0], k[1]);
ll mxDeg = 0, bridge = 0;
for (int i = 0; i < k[0].size(); i++) {
if (adj[k[0][i]].size() > mxDeg) {
mxDeg = adj[k[0][i]].size();
bridge = i;
}
}
vst = vector<bool>(n);
vector<int> res;
for (int i = bridge+1; i < k[0].size(); i++) {
vst[k[0][i]] = true;
res.push_back(k[0][i]);
}
for (int i = 0; i <= bridge; i++) {
vst[k[0][i]] = true;
res.push_back(k[0][i]);
}
ll used = -1;
for (auto &e : adj[k[0][bridge]]) {
if (vst[e]) continue;
used = e;
break;
}
if (used == -1) return res;
for (int i = 0; i < k[1].size(); i++) {
if (k[1][i] == used) {
used = i;
break;
}
}
for (int i = used; i < k[1].size(); i++) {
res.push_back(k[1][i]);
}
for (int i = 0; i < used; i++) {
res.push_back(k[1][i]);
}
volatile int idk = 0;
for (int i = 1; i < res.size(); i++) {
while (!mat[res[i-1]][res[i]] && (res[i-1] == k[0][bridge] && res[i] == k[1][used]))
idk++;
}
return res;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:50:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for (int i = 0; i < k[0].size(); i++) {
| ~~^~~~~~~~~~~~~
longesttrip.cpp:51:33: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
51 | if (adj[k[0][i]].size() > mxDeg) {
| ~~~~~~~~~~~~~~~~~~~~^~~~~~~
longesttrip.cpp:59:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for (int i = bridge+1; i < k[0].size(); i++) {
| ~~^~~~~~~~~~~~~
longesttrip.cpp:74:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for (int i = 0; i < k[1].size(); i++) {
| ~~^~~~~~~~~~~~~
longesttrip.cpp:80:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for (int i = used; i < k[1].size(); i++) {
| ~~^~~~~~~~~~~~~
longesttrip.cpp:88:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for (int i = 1; i < res.size(); i++) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
168 ms |
1540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
23 ms |
344 KB |
Output is correct |
3 |
Correct |
123 ms |
344 KB |
Output is correct |
4 |
Correct |
398 ms |
648 KB |
Output is correct |
5 |
Correct |
720 ms |
1340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
18 ms |
344 KB |
Output is correct |
3 |
Correct |
109 ms |
344 KB |
Output is correct |
4 |
Correct |
347 ms |
796 KB |
Output is correct |
5 |
Correct |
695 ms |
1340 KB |
Output is correct |
6 |
Correct |
5 ms |
344 KB |
Output is correct |
7 |
Correct |
17 ms |
344 KB |
Output is correct |
8 |
Correct |
124 ms |
344 KB |
Output is correct |
9 |
Correct |
287 ms |
556 KB |
Output is correct |
10 |
Correct |
566 ms |
1272 KB |
Output is correct |
11 |
Correct |
598 ms |
1120 KB |
Output is correct |
12 |
Correct |
670 ms |
1468 KB |
Output is correct |
13 |
Correct |
607 ms |
1116 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 |
119 ms |
484 KB |
Output is correct |
4 |
Correct |
297 ms |
804 KB |
Output is correct |
5 |
Correct |
568 ms |
1160 KB |
Output is correct |
6 |
Correct |
9 ms |
344 KB |
Output is correct |
7 |
Correct |
19 ms |
344 KB |
Output is correct |
8 |
Correct |
121 ms |
696 KB |
Output is correct |
9 |
Correct |
225 ms |
556 KB |
Output is correct |
10 |
Correct |
644 ms |
1228 KB |
Output is correct |
11 |
Correct |
649 ms |
1172 KB |
Output is correct |
12 |
Correct |
709 ms |
1464 KB |
Output is correct |
13 |
Correct |
720 ms |
1412 KB |
Output is correct |
14 |
Correct |
6 ms |
344 KB |
Output is correct |
15 |
Incorrect |
1 ms |
344 KB |
Incorrect |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
18 ms |
344 KB |
Output is correct |
3 |
Partially correct |
126 ms |
344 KB |
Output is partially correct |
4 |
Partially correct |
322 ms |
580 KB |
Output is partially correct |
5 |
Partially correct |
682 ms |
1632 KB |
Output is partially correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Correct |
15 ms |
344 KB |
Output is correct |
8 |
Partially correct |
127 ms |
456 KB |
Output is partially correct |
9 |
Partially correct |
267 ms |
592 KB |
Output is partially correct |
10 |
Partially correct |
696 ms |
1176 KB |
Output is partially correct |
11 |
Partially correct |
674 ms |
1172 KB |
Output is partially correct |
12 |
Partially correct |
704 ms |
1208 KB |
Output is partially correct |
13 |
Partially correct |
589 ms |
1568 KB |
Output is partially correct |
14 |
Correct |
6 ms |
344 KB |
Output is correct |
15 |
Incorrect |
1 ms |
344 KB |
Incorrect |
16 |
Halted |
0 ms |
0 KB |
- |