#include "longesttrip.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
const string asafaciunstringsus = R""""(
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
)"""";
typedef long long ll;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int random(int st, int dr)
{
uniform_int_distribution<int> dist(st, dr);
return dist(rng);
}
template <typename t>
using ordered_set = tree<t, null_type, less<t>, rb_tree_tag, tree_order_statistics_node_update>;
const int mod = 1e9 + 7;
struct Mint
{
int val;
Mint(int x = 0)
{
val = x % mod;
}
Mint(long long x)
{
val = x % mod;
}
Mint operator+(Mint oth)
{
return val + oth.val;
}
Mint operator-(Mint oth)
{
return val - oth.val + mod;
}
Mint operator*(Mint oth)
{
return 1ll * val * oth.val;
}
void operator+=(Mint oth)
{
val = (*this + oth).val;
}
void operator-=(Mint oth)
{
val = (*this - oth).val;
}
void operator*=(Mint oth)
{
val = (*this * oth).val;
}
};
Mint powmod(int a, int b)
{
if (b == 0)
{
return 1;
}
if (b % 2 == 1)
{
return powmod(a, b - 1) * a;
}
Mint p = powmod(a, b / 2);
return p * p;
}
/*
.___ __ __ .__
____ ____ __| _/____ _______/ |______ ________/ |_ ______ | |__ ___________ ____
_/ ___\/ _ \ / __ |/ __ \ / ___/\ __\__ \\_ __ \ __\/ ___/ | | \_/ __ \_ __ \_/ __ \
\ \__( <_> ) /_/ \ ___/ \___ \ | | / __ \| | \/| | \___ \ | Y \ ___/| | \/\ ___/
\___ >____/\____ |\___ > /____ > |__| (____ /__| |__| /____ > |___| /\___ >__| \___ >
\/ \/ \/ \/ \/ \/ \/ \/ \/
*/
vector<vector<int>> mat;
bool get(int i, int j)
{
if (mat[i][j] == -1)
{
mat[i][j] = mat[j][i] = are_connected({i}, {j});
}
return mat[i][j];
}
std::vector<int> longest_trip(int N, int D)
{
int n = N;
mat = vector<vector<int>>(n, vector<int>(n, -1));
vector<bool> vis(n);
vector<int> ord;
function<void(int)> dfs = [&](int node)
{
ord.push_back(node);
vis[node] = true;
for (int i = 0; i < n; ++i)
{
if (!vis[i] && get(node, i))
{
dfs(i);
}
}
};
function<vector<int>(vector<int>)> build = [&](vector<int> ord)
{
if (ord.size() <= 2)
{
return ord;
}
vector<int> ans;
ans.push_back(ord[0]);
ans.push_back(ord[1]);
for (int lol = 2; lol < ord.size(); ++lol)
{
int it = ord[lol];
if (get(ans.back(), it))
{
ans.push_back(it);
continue;
}
if (get(ans.front(), it))
{
ans.insert(ans.begin(), it);
continue;
}
for (int i = 0; i < ans.size(); ++i)
{
int node = ans[i];
if (get(node, it))
{
vector<int> new_ans(ans.size() + 1);
int p = 0;
new_ans[p++] = it;
for (int j = i; j >= 0; --j)
{
new_ans[p++] = ans[j];
}
for (int j = ans.size() - 1; j > i; --j)
{
new_ans[p++] = ans[j];
}
ans = new_ans;
break;
}
}
}
return ans;
};
vector<int> ans;
for (int i = 0; i < n; ++i)
{
if (!vis[i])
{
dfs(i);
if (ord.size() > ans.size())
{
ans = build(ord);
}
ord.clear();
}
}
return ans;
}
/*
I cannot take this anymore
Saying everything I've said before
All these words, they make no sense
I find bliss in ignorance
Less I hear, the less you say
You'll find that out anyway
Just like before
Everything you say to me
(Takes me one step closer to the edge)
(And I'm about to break)
I need a little room to breathe
(Cause I'm one step closer to the edge)
(I'm about to break)
*/
Compilation message
longesttrip.cpp: In lambda function:
longesttrip.cpp:127:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
127 | for (int lol = 2; lol < ord.size(); ++lol)
| ~~~~^~~~~~~~~~~~
longesttrip.cpp:140:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
140 | for (int i = 0; i < ans.size(); ++i)
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
4 ms |
592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
336 KB |
Output is correct |
2 |
Correct |
16 ms |
444 KB |
Output is correct |
3 |
Correct |
17 ms |
336 KB |
Output is correct |
4 |
Correct |
19 ms |
336 KB |
Output is correct |
5 |
Correct |
16 ms |
976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
336 KB |
Output is correct |
2 |
Correct |
16 ms |
336 KB |
Output is correct |
3 |
Correct |
17 ms |
336 KB |
Output is correct |
4 |
Correct |
16 ms |
336 KB |
Output is correct |
5 |
Correct |
14 ms |
968 KB |
Output is correct |
6 |
Correct |
15 ms |
336 KB |
Output is correct |
7 |
Correct |
19 ms |
336 KB |
Output is correct |
8 |
Correct |
21 ms |
336 KB |
Output is correct |
9 |
Correct |
17 ms |
336 KB |
Output is correct |
10 |
Correct |
18 ms |
968 KB |
Output is correct |
11 |
Correct |
12 ms |
976 KB |
Output is correct |
12 |
Correct |
19 ms |
992 KB |
Output is correct |
13 |
Correct |
17 ms |
976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
336 KB |
Output is correct |
2 |
Correct |
16 ms |
504 KB |
Output is correct |
3 |
Correct |
16 ms |
336 KB |
Output is correct |
4 |
Correct |
16 ms |
336 KB |
Output is correct |
5 |
Correct |
19 ms |
1108 KB |
Output is correct |
6 |
Correct |
19 ms |
336 KB |
Output is correct |
7 |
Correct |
20 ms |
584 KB |
Output is correct |
8 |
Correct |
17 ms |
336 KB |
Output is correct |
9 |
Correct |
16 ms |
336 KB |
Output is correct |
10 |
Correct |
17 ms |
1116 KB |
Output is correct |
11 |
Correct |
17 ms |
972 KB |
Output is correct |
12 |
Correct |
13 ms |
1224 KB |
Output is correct |
13 |
Correct |
16 ms |
976 KB |
Output is correct |
14 |
Correct |
19 ms |
336 KB |
Output is correct |
15 |
Correct |
31 ms |
336 KB |
Output is correct |
16 |
Correct |
22 ms |
336 KB |
Output is correct |
17 |
Correct |
29 ms |
336 KB |
Output is correct |
18 |
Correct |
21 ms |
336 KB |
Output is correct |
19 |
Correct |
15 ms |
336 KB |
Output is correct |
20 |
Correct |
21 ms |
336 KB |
Output is correct |
21 |
Correct |
19 ms |
976 KB |
Output is correct |
22 |
Correct |
21 ms |
1232 KB |
Output is correct |
23 |
Correct |
23 ms |
968 KB |
Output is correct |
24 |
Correct |
19 ms |
984 KB |
Output is correct |
25 |
Correct |
26 ms |
336 KB |
Output is correct |
26 |
Correct |
43 ms |
336 KB |
Output is correct |
27 |
Correct |
29 ms |
436 KB |
Output is correct |
28 |
Correct |
34 ms |
436 KB |
Output is correct |
29 |
Correct |
33 ms |
432 KB |
Output is correct |
30 |
Correct |
35 ms |
336 KB |
Output is correct |
31 |
Correct |
243 ms |
452 KB |
Output is correct |
32 |
Correct |
360 ms |
460 KB |
Output is correct |
33 |
Correct |
30 ms |
336 KB |
Output is correct |
34 |
Correct |
74 ms |
524 KB |
Output is correct |
35 |
Correct |
124 ms |
736 KB |
Output is correct |
36 |
Correct |
30 ms |
968 KB |
Output is correct |
37 |
Correct |
209 ms |
1132 KB |
Output is correct |
38 |
Correct |
715 ms |
1236 KB |
Output is correct |
39 |
Execution timed out |
1119 ms |
1076 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
336 KB |
Output is correct |
2 |
Correct |
17 ms |
584 KB |
Output is correct |
3 |
Correct |
16 ms |
336 KB |
Output is correct |
4 |
Correct |
12 ms |
336 KB |
Output is correct |
5 |
Correct |
15 ms |
1104 KB |
Output is correct |
6 |
Correct |
16 ms |
336 KB |
Output is correct |
7 |
Correct |
18 ms |
436 KB |
Output is correct |
8 |
Correct |
18 ms |
336 KB |
Output is correct |
9 |
Correct |
17 ms |
336 KB |
Output is correct |
10 |
Correct |
16 ms |
976 KB |
Output is correct |
11 |
Correct |
16 ms |
968 KB |
Output is correct |
12 |
Correct |
17 ms |
1220 KB |
Output is correct |
13 |
Correct |
16 ms |
1236 KB |
Output is correct |
14 |
Correct |
22 ms |
336 KB |
Output is correct |
15 |
Correct |
31 ms |
336 KB |
Output is correct |
16 |
Correct |
25 ms |
336 KB |
Output is correct |
17 |
Correct |
22 ms |
336 KB |
Output is correct |
18 |
Correct |
18 ms |
336 KB |
Output is correct |
19 |
Correct |
18 ms |
336 KB |
Output is correct |
20 |
Correct |
20 ms |
336 KB |
Output is correct |
21 |
Correct |
26 ms |
336 KB |
Output is correct |
22 |
Correct |
24 ms |
336 KB |
Output is correct |
23 |
Correct |
28 ms |
584 KB |
Output is correct |
24 |
Correct |
29 ms |
504 KB |
Output is correct |
25 |
Correct |
32 ms |
432 KB |
Output is correct |
26 |
Correct |
31 ms |
336 KB |
Output is correct |
27 |
Partially correct |
242 ms |
336 KB |
Output is partially correct |
28 |
Partially correct |
356 ms |
336 KB |
Output is partially correct |
29 |
Correct |
33 ms |
488 KB |
Output is correct |
30 |
Correct |
74 ms |
336 KB |
Output is correct |
31 |
Partially correct |
132 ms |
520 KB |
Output is partially correct |
32 |
Correct |
22 ms |
336 KB |
Output is correct |
33 |
Correct |
21 ms |
336 KB |
Output is correct |
34 |
Correct |
29 ms |
336 KB |
Output is correct |
35 |
Correct |
25 ms |
336 KB |
Output is correct |
36 |
Correct |
27 ms |
436 KB |
Output is correct |
37 |
Correct |
27 ms |
336 KB |
Output is correct |
38 |
Partially correct |
166 ms |
468 KB |
Output is partially correct |
39 |
Partially correct |
223 ms |
472 KB |
Output is partially correct |
40 |
Correct |
28 ms |
336 KB |
Output is correct |
41 |
Correct |
52 ms |
336 KB |
Output is correct |
42 |
Partially correct |
81 ms |
336 KB |
Output is partially correct |
43 |
Correct |
14 ms |
968 KB |
Output is correct |
44 |
Correct |
15 ms |
980 KB |
Output is correct |
45 |
Correct |
19 ms |
1136 KB |
Output is correct |
46 |
Correct |
18 ms |
968 KB |
Output is correct |
47 |
Partially correct |
35 ms |
1360 KB |
Output is partially correct |
48 |
Partially correct |
202 ms |
956 KB |
Output is partially correct |
49 |
Partially correct |
674 ms |
984 KB |
Output is partially correct |
50 |
Execution timed out |
1142 ms |
972 KB |
Time limit exceeded |
51 |
Halted |
0 ms |
0 KB |
- |