#include <bits/stdc++.h>
#include "longesttrip.h"
using namespace std;
using ll = long long;
int n, d;
vector<int> adj2[266];
int col[266];
vector<int> r1, r2;
vector<int> t1, t2;
void dfs(int v) {
if (col[v] == 1) t1.push_back(v);
else t2.push_back(v);
for (auto& x : adj2[v]) {
if (!col[x]) col[x] = 3 - col[v], dfs(x);
}
}
vector<int> longest_trip(int N, int D) {
n = N;
d = D;
vector<vector<int>> adj(n, vector<int>(n, 1));
r1.clear(), r2.clear();
for (int i = 0;i < n;i++) {
for (int j = i + 1;j < n;j++) {
adj[i][j] = adj[j][i] = are_connected({ i }, { j });
if (!adj[i][j]) adj2[i].push_back(j), adj2[j].push_back(i);
}
}
{
vector<int> g1, g2;
g1.push_back(0);
for (int i = 1;i < n;i++) {
if (adj[0][i]) g1.push_back(i);
else g2.push_back(i);
}
bool gg = 0;
for (auto& x : g1) for (auto& y : g2) if (adj[x][y]) gg = 1;
if (!gg) {
if (g1.size() > g2.size()) return g1;
return g2;
}
}
int tot = 0;
for (int i = 0;i < n;i++) {
if (!col[i]) {
col[i] = 1;
dfs(i);
if (tot == 0) {
tot = 1;
}
else {
bool ch = 0;
for (auto& x : r1) for (auto& y : t2) if (adj[x][y]) ch = 1;
for (auto& x : r2) for (auto& y : t1) if (adj[x][y]) ch = 1;
if (ch) {
swap(t1, t2);
for (auto& x : t1) col[x] = 3 - col[x];
for (auto& x : t2) col[x] = 3 - col[x];
}
}
for (auto& x : t1) r1.push_back(x);
for (auto& x : t2) r2.push_back(x);
t1.clear(), t2.clear();
}
}
for (int i = 0;i < n;i++) adj2[i].clear(), col[i] = 0;
int s1 = r1.size(), s2 = r2.size();
for (int i = 0;i < s1;i++) {
for (int j = 0;j < s2;j++) {
if (adj[r1[i]][r2[j]]) {
swap(r1[i], r1[s1 - 1]);
swap(r2[0], r2[j]);
vector<int> ans;
for (auto& x : r1) ans.push_back(x);
for (auto& x : r2) ans.push_back(x);
return ans;
}
}
}
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:20:49: warning: control reaches end of non-void function [-Wreturn-type]
20 | vector<vector<int>> adj(n, vector<int>(n, 1));
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
164 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
26 ms |
344 KB |
Output is correct |
3 |
Correct |
120 ms |
344 KB |
Output is correct |
4 |
Correct |
359 ms |
484 KB |
Output is correct |
5 |
Correct |
745 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
24 ms |
344 KB |
Output is correct |
3 |
Correct |
129 ms |
344 KB |
Output is correct |
4 |
Correct |
350 ms |
592 KB |
Output is correct |
5 |
Correct |
709 ms |
600 KB |
Output is correct |
6 |
Incorrect |
0 ms |
344 KB |
Incorrect |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
357 ms |
500 KB |
Output is correct |
5 |
Correct |
737 ms |
600 KB |
Output is correct |
6 |
Incorrect |
0 ms |
344 KB |
Incorrect |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
23 ms |
344 KB |
Output is correct |
3 |
Partially correct |
135 ms |
344 KB |
Output is partially correct |
4 |
Partially correct |
308 ms |
344 KB |
Output is partially correct |
5 |
Partially correct |
692 ms |
600 KB |
Output is partially correct |
6 |
Incorrect |
0 ms |
344 KB |
Incorrect |
7 |
Halted |
0 ms |
0 KB |
- |