#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);
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);
}
}
}
mt19937 mt(12684);
vector<int> G(n);
for (int i = 0; i < n; i++) G[i] = i;
help:
shuffle(G.begin(), G.end(), mt);
k = vector<vector<ll>>(2);
vst = vector<bool>(n);
ll comp = 0;
for (int i1 = 0; i1 < n; i1++) {
int i = G[i1];
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]);
}
for (int i = 1; i < res.size(); i++) {
if (!mat[i-1][i]) goto help;
}
return res;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:57:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for (int i = 0; i < k[0].size(); i++) {
| ~~^~~~~~~~~~~~~
longesttrip.cpp:58: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]
58 | if (adj[k[0][i]].size() > mxDeg) {
| ~~~~~~~~~~~~~~~~~~~~^~~~~~~
longesttrip.cpp:66:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for (int i = bridge+1; i < k[0].size(); i++) {
| ~~^~~~~~~~~~~~~
longesttrip.cpp:81:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for (int i = 0; i < k[1].size(); i++) {
| ~~^~~~~~~~~~~~~
longesttrip.cpp:87:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for (int i = used; i < k[1].size(); i++) {
| ~~^~~~~~~~~~~~~
longesttrip.cpp:94:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for (int i = 1; i < res.size(); i++) {
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
3026 ms |
224884 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
21 ms |
344 KB |
Output is correct |
3 |
Correct |
109 ms |
344 KB |
Output is correct |
4 |
Correct |
323 ms |
708 KB |
Output is correct |
5 |
Correct |
708 ms |
1120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
19 ms |
344 KB |
Output is correct |
3 |
Correct |
116 ms |
356 KB |
Output is correct |
4 |
Correct |
347 ms |
344 KB |
Output is correct |
5 |
Correct |
721 ms |
1196 KB |
Output is correct |
6 |
Execution timed out |
3041 ms |
520784 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
21 ms |
344 KB |
Output is correct |
3 |
Correct |
124 ms |
600 KB |
Output is correct |
4 |
Correct |
336 ms |
592 KB |
Output is correct |
5 |
Correct |
676 ms |
1808 KB |
Output is correct |
6 |
Execution timed out |
3070 ms |
534644 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
19 ms |
344 KB |
Output is correct |
3 |
Partially correct |
130 ms |
488 KB |
Output is partially correct |
4 |
Partially correct |
329 ms |
580 KB |
Output is partially correct |
5 |
Partially correct |
695 ms |
1356 KB |
Output is partially correct |
6 |
Execution timed out |
3040 ms |
539692 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |