#include "longesttrip.h"
#include <bits/stdc++.h>
#include <vector>
#define MP make_pair
#define f first
#define s second
#define mid ((l+r)>>1)
typedef long long ll;
using namespace std;
mt19937_64 rnd(286);
const int INF = (int)1e6;
bool conectados(int A, int B) {
vector<int> a,b;
a.push_back(A);
b.push_back(B);
return are_connected(a,b);
}
struct linea{
deque <int> lin;
linea(){}
linea(int A) {
lin.push_front(A);
}
void unir(linea &B) {
while (!B.lin.empty()) {
lin.push_front(B.lin.front());
B.lin.pop_front();
}
return;
}
void unirrevez(linea &B) {
while (!B.lin.empty()) {
lin.push_front(B.lin.back());
B.lin.pop_back();
}
return;
}
void unirrevez2(linea &B) {
while (!B.lin.empty()) {
lin.push_back(B.lin.back());
B.lin.pop_back();
}
return;
}
};
vector <int> base(int N) {
vector <int> ans;
ans.push_back(0);
if (conectados(0, 1)) ans.push_back(1);
return ans;
}
void dfs(int nodo, int p, vector <vector<int>> &g, vector <int> &ans) {
ans.push_back(nodo);
for (auto &it : g[nodo]) {
if (it == p) continue;
dfs(it, nodo, g, ans);
}
return;
}
int bin(int l, int r, vector <int> &ar, vector <int> &bus) {
if (l == r) return ar[l];
vector <int> aux;
for (int i = l; i <= mid; i++) aux.push_back(ar[i]);
if (are_connected(aux, bus)) return bin(l, mid, ar, bus);
else return bin(mid + 1, r, ar, bus);
}
std::vector<int> longest_trip(int N, int D) {
if (N <= 2) return base(N);
vector <linea> V;
vector <int> ans;
for (int i = 0; i < N; i++)
V.push_back(linea(i));
shuffle(V.begin(),V.end(),rnd);
while (V.size() >= 3) {
linea A = V.back();
V.pop_back();
linea B = V.back();
V.pop_back();
linea C = V.back();
V.pop_back();
if (conectados(A.lin.front(), B.lin.front())) {
if (A.lin.size() < B.lin.size()) swap(A,B);
A.unir(B);
V.push_back(A);
V.push_back(C);
}
else if (conectados(A.lin.front(), C.lin.front())) {
if (A.lin.size() < C.lin.size()) swap(A,C);
A.unir(C);
V.push_back(A);
V.push_back(B);
}
else {
if (B.lin.size() < C.lin.size()) swap(B,C);
B.unir(C);
V.push_back(B);
V.push_back(A);
}
}
bool c1,c2,c3,c4;
c1 = conectados(V[0].lin.front(), V[1].lin.front());
c2 = conectados(V[0].lin.front(), V[1].lin.back());
c3 = conectados(V[0].lin.back(), V[1].lin.back());
c4 = conectados(V[0].lin.back(), V[1].lin.front());
if (c1 || c2 || c3 || c4) {
if (c1) V[1].unir(V[0]);
//else if (c2) {
// V[0].unirrevez(V[1]);
// swap(V[1],V[0]);
//}
else if (c3) V[1].unirrevez2(V[0]);
else if (c4) V[1].unirrevez(V[0]);
while (!V[1].lin.empty()) {
ans.push_back(V[1].lin.front());
V[1].lin.pop_front();
}
return ans;
}
vector <int> VA, VB;
while (!V[1].lin.empty()) {
VB.push_back(V[1].lin.front());
V[1].lin.pop_front();
}
while (!V[0].lin.empty()) {
VA.push_back(V[0].lin.front());
V[0].lin.pop_front();
}
if (!are_connected(VA,VB)) {
if (VA.size() > VB.size()) return VA;
return VB;
}
int ancla = bin(0, VA.size() - 1, VA, VB);
vector <int> vancla;
vancla.push_back(ancla);
int ancla2 = bin(0, VB.size() - 1, VB, vancla);
vector <vector<int>> g(N + 2);
for (int i = 0; i < VA.size() - 1; i++) {
g[VA[i]].push_back(VA[i + 1]);
g[VA[i + 1]].push_back(VA[i]);
}
for (int i = 0; i < VB.size() - 1; i++) {
g[VB[i]].push_back(VB[i + 1]);
g[VB[i + 1]].push_back(VB[i]);
}
g[VA[VA.size() - 1]].push_back(VA[0]);
g[VA[0]].push_back(VA[VA.size() - 1]);
g[VB[VB.size() - 1]].push_back(VB[0]);
g[VA[0]].push_back(VA[VB.size() - 1]);
g[ancla].push_back(ancla2);
g[ancla2].push_back(ancla);
if (VA.size() != 1) {
for (auto &it : g[ancla]) {
if (it != ancla2) {
dfs(it, -1, g, ans);
break;
}
}
}
else {
for (auto &it : g[ancla2]) {
if (it != ancla) {
dfs(it, -1, g, ans);
break;
}
}
}
return ans;
return ans;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:150:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
150 | for (int i = 0; i < VA.size() - 1; i++) {
| ~~^~~~~~~~~~~~~~~
longesttrip.cpp:154:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
154 | for (int i = 0; i < VB.size() - 1; i++) {
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
600 KB |
Output is correct |
4 |
Correct |
7 ms |
596 KB |
Output is correct |
5 |
Correct |
5 ms |
936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
344 KB |
Output is correct |
2 |
Correct |
10 ms |
344 KB |
Output is correct |
3 |
Correct |
6 ms |
600 KB |
Output is correct |
4 |
Correct |
6 ms |
600 KB |
Output is correct |
5 |
Correct |
5 ms |
684 KB |
Output is correct |
6 |
Correct |
11 ms |
344 KB |
Output is correct |
7 |
Correct |
8 ms |
344 KB |
Output is correct |
8 |
Correct |
6 ms |
600 KB |
Output is correct |
9 |
Correct |
5 ms |
600 KB |
Output is correct |
10 |
Correct |
5 ms |
856 KB |
Output is correct |
11 |
Correct |
5 ms |
856 KB |
Output is correct |
12 |
Correct |
6 ms |
936 KB |
Output is correct |
13 |
Correct |
7 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
344 KB |
Output is correct |
2 |
Correct |
8 ms |
344 KB |
Output is correct |
3 |
Correct |
6 ms |
600 KB |
Output is correct |
4 |
Correct |
6 ms |
564 KB |
Output is correct |
5 |
Correct |
7 ms |
860 KB |
Output is correct |
6 |
Correct |
13 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
6 ms |
600 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
8 ms |
936 KB |
Output is correct |
11 |
Correct |
6 ms |
684 KB |
Output is correct |
12 |
Correct |
6 ms |
940 KB |
Output is correct |
13 |
Correct |
6 ms |
932 KB |
Output is correct |
14 |
Correct |
15 ms |
344 KB |
Output is correct |
15 |
Correct |
17 ms |
344 KB |
Output is correct |
16 |
Execution timed out |
1456 ms |
2097152 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
344 KB |
Output is correct |
2 |
Correct |
7 ms |
344 KB |
Output is correct |
3 |
Correct |
6 ms |
752 KB |
Output is correct |
4 |
Correct |
5 ms |
600 KB |
Output is correct |
5 |
Correct |
6 ms |
1112 KB |
Output is correct |
6 |
Correct |
10 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
8 ms |
856 KB |
Output is correct |
9 |
Correct |
6 ms |
600 KB |
Output is correct |
10 |
Correct |
6 ms |
1112 KB |
Output is correct |
11 |
Correct |
6 ms |
944 KB |
Output is correct |
12 |
Correct |
5 ms |
940 KB |
Output is correct |
13 |
Correct |
5 ms |
856 KB |
Output is correct |
14 |
Correct |
15 ms |
344 KB |
Output is correct |
15 |
Correct |
11 ms |
344 KB |
Output is correct |
16 |
Execution timed out |
1177 ms |
2097152 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |