#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
// #define int long long // remove when necessary
#define pii pair<int, int>
#define ff first
#define ss second
#define pb push_back
#define ll long long
const int mxN = 256;
vector<int> adj[mxN];
vector<int> grp[mxN];
bool con[mxN][mxN];
int n;
int dsupp[mxN];
vector<int> nxt[mxN];
bool vis[mxN];
void resetvis() {
for (int i = 0; i < n; i++) vis[i] = false;
}
void reset() {
for (int i = 0; i < n; i++) {
adj[i].clear();
grp[i].clear();
for (int j = 0; j < n; j++) {
con[i][j] = 0;
}
dsupp[i] = i;
nxt[i].clear();
vis[i] = false;
}
}
int DSUFind(int u) {
if (dsupp[u] == u) return u;
return (dsupp[u] = DSUFind(dsupp[u]));
}
void DSUMerge(int u, int v) {
u = DSUFind(u); v = DSUFind(v);
if (u != v) dsupp[v] = u;
}
vector<int> GetPath(int st) {
resetvis();
vector<int> res;
while (true) {
res.pb(st);
vis[st] = true;
bool found = false;
for (int &v : nxt[st]) {
if (!vis[v]) {
st = v;
found = true;
break;
}
}
if (!found) break;
}
return res;
}
vector<int> MakePath(vector<int> &v) {
if (v.size() == 1) return {v[0]};
if (v.size() == 2) {
if (con[v[0]][v[1]]) return {v[0], v[1]};
else return {v[0]};
}
if (v.size() == 3) {
if (con[v[0]][v[1]] && con[v[0]][v[2]]) return {v[1], v[0], v[2]};
if (con[v[0]][v[1]] && con[v[1]][v[2]]) return {v[0], v[1], v[2]};
if (con[v[0]][v[2]] && con[v[1]][v[2]]) return {v[0], v[2], v[1]};
if (con[v[0]][v[1]]) return {v[0], v[1]};
if (con[v[0]][v[2]]) return {v[0], v[2]};
if (con[v[1]][v[2]]) return {v[1], v[2]};
return {v[0]};
}
for (int i = 0; i < n; i++) {
nxt[i].clear();
}
pair<pii, pii> cur = {{v[0], v[0]}, {v[1], v[1]}};
for (int i = 2; i < (int)(v.size()); i++) {
if (con[cur.ff.ff][cur.ss.ff]) {
nxt[cur.ff.ff].pb(cur.ss.ff);
nxt[cur.ss.ff].pb(cur.ff.ff);
cur.ff.ff = cur.ss.ss;
cur.ss = {v[i], v[i]};
} else if (con[cur.ff.ff][v[i]]) {
nxt[cur.ff.ff].pb(v[i]);
nxt[v[i]].pb(cur.ff.ff);
cur.ff.ff = v[i];
} else if (con[cur.ss.ff][v[i]]) {
nxt[cur.ss.ff].pb(v[i]);
nxt[v[i]].pb(cur.ss.ff);
cur.ss.ff = v[i];
} else {
while (true) continue;
}
}
int st;
if (con[cur.ff.ff][cur.ss.ff]) {
nxt[cur.ff.ff].pb(cur.ss.ff);
nxt[cur.ss.ff].pb(cur.ff.ff);
st = cur.ff.ss;
} else if (con[cur.ff.ff][cur.ss.ss]) {
nxt[cur.ff.ff].pb(cur.ss.ss);
nxt[cur.ss.ss].pb(cur.ff.ff);
st = cur.ff.ss;
} else if (con[cur.ff.ss][cur.ss.ff]) {
nxt[cur.ff.ss].pb(cur.ss.ff);
nxt[cur.ss.ff].pb(cur.ff.ss);
st = cur.ff.ff;
} else if (con[cur.ff.ss][cur.ss.ss]) {
nxt[cur.ff.ss].pb(cur.ss.ss);
nxt[cur.ss.ss].pb(cur.ff.ss);
st = cur.ff.ff;
} else {
vector<int> res[2];
res[0] = GetPath(cur.ff.ff);
res[1] = GetPath(cur.ss.ff);
// cerr << "res0: ";
// for (int &x : res[0]) cerr << x << ' ';
// cerr << endl;
// cerr << "res1: ";
// for (int &x : res[1]) cerr << x << ' ';
// cerr << endl;
bool found = false;
for (int i = 0; i < (int)(res[0].size()); i++) {
for (int j = 0; j < (int)(res[1].size()); j++) {
if (con[res[0][i]][res[1][j]]) {
// cerr << "FOUND " << res[0][i] << ' ' << res[1][j] << endl;
nxt[res[0][i]].pb(res[1][j]);
nxt[res[1][j]].pb(res[0][i]);
if (res[0].size() >= 3) {
nxt[cur.ff.ff].pb(cur.ff.ss);
nxt[cur.ff.ss].pb(cur.ff.ff);
// cerr << "RES0 ";
// for (int &x : res[0]) cerr << x << ' ';
// cerr << endl;
st = (i == 0 ? res[0].back() : res[0][i - 1]);
// cerr << "ST " << st << endl;
vector<int>::iterator it;
for (it = nxt[st].begin(); it != nxt[st].end(); ++it) {
if (*it == res[0][i]) {
nxt[st].erase(it);
break;
}
}
for (it = nxt[res[0][i]].begin(); it != nxt[res[0][i]].end(); ++it) {
if (*it == st) {
nxt[res[0][i]].erase(it);
break;
}
}
} else {
st = (res[0].size() == 2 ? res[0][i ^ 1] : res[0][i]);
}
if (res[1].size() >= 3) {
nxt[cur.ss.ff].pb(cur.ss.ss);
nxt[cur.ss.ss].pb(cur.ss.ff);
int tar = (j == 0 ? res[1].back() : res[1][j + 1]);
vector<int>::iterator it;
for (it = nxt[tar].begin(); it != nxt[tar].end(); ++it) {
if (*it == res[1][j]) {
nxt[tar].erase(it);
break;
}
}
for (it = nxt[res[1][j]].begin(); it != nxt[res[1][j]].end(); ++it) {
if (*it == tar) {
nxt[res[1][j]].erase(it);
break;
}
}
}
found = true;
break;
}
}
if (found) break;
}
if (!found) {
while (true) continue;
}
}
// cerr << "find path for: ";
// for (int &x : v) cerr << x << ' ';
// cerr << endl;
vector<int> res = GetPath(st);
// cerr << "st: " << st << endl;
// cerr << "result: ";
// for (int &x : res) cerr << x << ' ';
// cerr << endl;
return res;
}
vector<int> longest_trip(int N, int D)
{
n = N;
reset();
if (D == 3) {
vector<int> res(n);
for (int i = 0; i < n; i++) res[i] = i;
return res;
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (are_connected({i}, {j})) {
adj[i].pb(j);
adj[j].pb(i);
con[i][j] = con[j][i] = true;
DSUMerge(i, j);
}
}
}
for (int i = 0; i < n; i++) {
grp[DSUFind(i)].pb(i);
}
vector<int> best = {};
for (int i = 0; i < n; i++) {
if (grp[i].empty()) continue;
vector<int> res = MakePath(grp[i]);
if (res.size() > best.size()) {
best = res;
}
}
return best;
}
// after contest 60 submission
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
229 ms |
584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
208 KB |
Output is correct |
2 |
Correct |
36 ms |
208 KB |
Output is correct |
3 |
Correct |
170 ms |
336 KB |
Output is correct |
4 |
Correct |
426 ms |
404 KB |
Output is correct |
5 |
Correct |
851 ms |
800 KB |
Output is correct |
6 |
Correct |
10 ms |
208 KB |
Output is correct |
7 |
Correct |
30 ms |
208 KB |
Output is correct |
8 |
Correct |
174 ms |
336 KB |
Output is correct |
9 |
Correct |
256 ms |
336 KB |
Output is correct |
10 |
Correct |
806 ms |
612 KB |
Output is correct |
11 |
Correct |
813 ms |
824 KB |
Output is correct |
12 |
Correct |
883 ms |
788 KB |
Output is correct |
13 |
Correct |
783 ms |
704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
208 KB |
Output is correct |
2 |
Correct |
32 ms |
208 KB |
Output is correct |
3 |
Correct |
150 ms |
336 KB |
Output is correct |
4 |
Correct |
426 ms |
392 KB |
Output is correct |
5 |
Correct |
779 ms |
648 KB |
Output is correct |
6 |
Correct |
10 ms |
208 KB |
Output is correct |
7 |
Correct |
33 ms |
208 KB |
Output is correct |
8 |
Correct |
150 ms |
336 KB |
Output is correct |
9 |
Correct |
276 ms |
376 KB |
Output is correct |
10 |
Correct |
804 ms |
572 KB |
Output is correct |
11 |
Correct |
825 ms |
528 KB |
Output is correct |
12 |
Correct |
760 ms |
700 KB |
Output is correct |
13 |
Correct |
760 ms |
592 KB |
Output is correct |
14 |
Correct |
6 ms |
332 KB |
Output is correct |
15 |
Correct |
11 ms |
208 KB |
Output is correct |
16 |
Correct |
58 ms |
208 KB |
Output is correct |
17 |
Correct |
84 ms |
208 KB |
Output is correct |
18 |
Correct |
105 ms |
336 KB |
Output is correct |
19 |
Correct |
320 ms |
388 KB |
Output is correct |
20 |
Correct |
309 ms |
396 KB |
Output is correct |
21 |
Correct |
697 ms |
616 KB |
Output is correct |
22 |
Correct |
867 ms |
672 KB |
Output is correct |
23 |
Correct |
883 ms |
656 KB |
Output is correct |
24 |
Correct |
780 ms |
880 KB |
Output is correct |
25 |
Correct |
16 ms |
208 KB |
Output is correct |
26 |
Correct |
13 ms |
208 KB |
Output is correct |
27 |
Correct |
28 ms |
208 KB |
Output is correct |
28 |
Correct |
36 ms |
208 KB |
Output is correct |
29 |
Correct |
31 ms |
332 KB |
Output is correct |
30 |
Correct |
212 ms |
364 KB |
Output is correct |
31 |
Correct |
218 ms |
348 KB |
Output is correct |
32 |
Correct |
226 ms |
336 KB |
Output is correct |
33 |
Correct |
290 ms |
388 KB |
Output is correct |
34 |
Correct |
321 ms |
392 KB |
Output is correct |
35 |
Correct |
334 ms |
392 KB |
Output is correct |
36 |
Correct |
767 ms |
792 KB |
Output is correct |
37 |
Correct |
853 ms |
648 KB |
Output is correct |
38 |
Execution timed out |
1148 ms |
600 KB |
Time limit exceeded |
39 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
208 KB |
Output is correct |
2 |
Correct |
46 ms |
208 KB |
Output is correct |
3 |
Partially correct |
169 ms |
336 KB |
Output is partially correct |
4 |
Partially correct |
521 ms |
516 KB |
Output is partially correct |
5 |
Execution timed out |
1050 ms |
696 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |