#include "islands.h"
#include <cassert>
#include <cstdio>
#include <variant>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
bool loop = false;
vector<int> path, lpath;
void dfs(int node, vector<vector<pair<int, int>>> &tree, vector<bool> &vis){
path.push_back(node);
if(vis[node] && !loop){
loop = true;
lpath = path;
return;
}
vis[node] = true;
for(auto&[l, r] : tree[node]){
if(!r){
r = true;
dfs(l, tree, vis);
}
}
path.pop_back();
vis[node] = false;
}
variant<bool, vector<int>> find_journey(int N, int M, vector<int> U, vector<int> V) {
if (N == 2){
if(count(U.begin(), U.end(), 0) >= 2 && count(U.begin(), U.end(), 1) >= 1){
vector<pair<int, int>> ans(M);
for(int i = 0; i < M; i ++){
ans[i] = {U[i], i};
}
sort(ans.begin(), ans.end());
vector<int> ret = {ans[0].second, ans.back().second, ans[1].second, ans[0].second, ans.back().second, ans[1].second};
return ret;
}
return false;
}
bool N2 = true;
for(int i = 0; i < N; i ++){
N2 &= ((count(U.begin(), U.end(), i) + count(V.begin(), V.end(), i)) == 2 * N - 2);
}
if(N2){
vector<vector<pair<int, int>>> tree(N);
for(int i = 0; i < M; i ++){
tree[U[i]].push_back({V[i], i});
}
for(int i = 0; i < N; i ++){
sort(tree[i].begin(), tree[i].end());
}
vector<int> ret = {tree[0][0].second, tree[1][0].second, tree[0][1].second, tree[2][0].second, tree[1][0].second, tree[0][0].second, tree[2][0].second, tree[0][1].second};
return ret;
}
vector<vector<pair<int,int>>> tree(N);
bool good = true;
for(int i = 0; i < M-1; i ++){
good = V[i] != V[i + 1];
}
if(good){
for(int i = 0; i < M; i ++){
tree[U[i]].push_back({V[i], i});
}
vector<int> path;
int id = 0, par = -1;
bool back = false;
int NNN = 0;
while(tree[id].size() < 3 && tree[id].size() > 0 && !back){
NNN = max(NNN, (int)tree[id].size());
if(tree[id].size() == 1){
par = id;
path.push_back(tree[id][0].second);
id = tree[id][0].first;
}
else{
if(tree[id][0].first == par){
path.push_back(tree[id][1].second);
par = id;
id = tree[id][1].first;
}
else{
path.push_back(tree[id][0].second);
par = id;
id = tree[id][0].first;
}
}
if(id == 0){
back = true;
}
}
if(!back && tree[0].size() != 0){
vector<int> mov, ids;
for(auto[l, r] : tree[id]){
if (l != par){
mov.push_back(l);
ids.push_back(r);
}
if(mov.size() > 1){
break;
}
}
for(int i = 0; i < M; i ++){
if(mov[0] == U[i] && id == V[i]){
ids.insert(ids.begin() + 1, i);
break;
}
}
for(int i = 0; i < M; i ++){
if(mov[1] == U[i] && id == V[i]){
ids.push_back(i);
break;
}
}
vector<int> gap;
if(mov[0] == mov[1]){
gap = {ids[0], ids[1], ids[2], ids[0], ids[1], ids[2]};
}
else{
gap = {ids[0], ids[1], ids[2], ids[3], ids[1], ids[0], ids[3], ids[2]};
}
vector<int> legit;
for(int i : path){
legit.push_back(i);
}
for(int i : gap){
legit.push_back(i);
}
reverse(path.begin(), path.end());
for(int i : path){
legit.push_back(i);
}
return legit;
}
return false;
}
for(int i = 0; i < M; i ++){
tree[U[i]].push_back({V[i], 0});
}
vector<bool> vis(N, 0);
dfs(0, tree, vis);
if(!loop){
return false;
}
vector<int> arr;
int id = find(lpath.begin(), lpath.end(), lpath.back()) - lpath.begin();
int sz = lpath.size();
vector<int> pre, lap1, lap2, lap1id, lap2id, preid;
for(int i = 0; i <= id; i ++){
pre.push_back(lpath[i]);
}
for(int j = id; j < sz; j ++){
lap1.push_back(lpath[j]);
lap2.push_back(lpath[j]);
}
map<pair<int, int>, set<int>> pos;
for(int i = 0; i < M; i ++){
pos[{U[i], V[i]}].insert(i);
}
for(int i = 1; i < pre.size(); i++){
preid.push_back(*pos[{pre[i-1], pre[i]}].begin());
pos[{pre[i-1], pre[i]}].erase(pos[{pre[i-1], pre[i]}].begin());
}
for(int i = 1; i < lap1.size(); i ++){
lap1id.push_back(*pos[{lap1[i-1], lap1[i]}].begin());
pos[{lap1[i-1], lap1[i]}].erase(pos[{lap1[i-1], lap1[i]}].begin());
}
for(int i = 1; i < lap2.size(); i ++){
lap2id.push_back(*pos[{lap2[i-1], lap2[i]}].begin());
pos[{lap2[i-1], lap2[i]}].erase(pos[{lap2[i-1], lap2[i]}].begin());
}
for(int i : preid){
arr.push_back(i);
}
for(int j : lap1id){
arr.push_back(j);
}
for(int j : lap2id){
arr.push_back(j);
}
reverse(preid.begin(), preid.end());
reverse(lap1id.begin(), lap1id.end());
reverse(lap2id.begin(), lap2id.end());
for(int j : lap1id){
arr.push_back(j);
}
for(int j : lap2id){
arr.push_back(j);
}
for(int i : preid){
arr.push_back(i);
}
//cout << lap1id.size() << " " << lap2id.size() << " " << preid.size() << endl;
return arr;
}
Compilation message
islands.cpp: In function 'std::variant<bool, std::vector<int, std::allocator<int> > > find_journey(int, int, std::vector<int>, std::vector<int>)':
islands.cpp:162:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
162 | for(int i = 1; i < pre.size(); i++){
| ~~^~~~~~~~~~~~
islands.cpp:166:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
166 | for(int i = 1; i < lap1.size(); i ++){
| ~~^~~~~~~~~~~~~
islands.cpp:170:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
170 | for(int i = 1; i < lap2.size(); i ++){
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
37 ms |
5672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
111 ms |
5720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
436 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
3 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
300 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
296 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
256 KB |
Output is correct |
17 |
Correct |
140 ms |
3768 KB |
Output is correct |
18 |
Correct |
109 ms |
3312 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
300 KB |
Output is correct |
22 |
Correct |
1 ms |
304 KB |
Output is correct |
23 |
Correct |
106 ms |
5624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
696 KB |
Output is correct |
3 |
Correct |
143 ms |
14616 KB |
Output is correct |
4 |
Correct |
348 ms |
20876 KB |
Output is correct |
5 |
Correct |
6 ms |
724 KB |
Output is correct |
6 |
Correct |
6 ms |
724 KB |
Output is correct |
7 |
Correct |
0 ms |
300 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
4 ms |
696 KB |
Output is correct |
11 |
Correct |
6 ms |
384 KB |
Output is correct |
12 |
Correct |
7 ms |
772 KB |
Output is correct |
13 |
Correct |
8 ms |
980 KB |
Output is correct |
14 |
Correct |
7 ms |
852 KB |
Output is correct |
15 |
Correct |
10 ms |
980 KB |
Output is correct |
16 |
Correct |
4 ms |
596 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
7 ms |
852 KB |
Output is correct |
19 |
Correct |
4 ms |
564 KB |
Output is correct |
20 |
Correct |
253 ms |
6840 KB |
Output is correct |
21 |
Correct |
353 ms |
22388 KB |
Output is correct |
22 |
Correct |
3 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
4 ms |
724 KB |
Output is correct |
27 |
Correct |
333 ms |
20900 KB |
Output is correct |
28 |
Correct |
329 ms |
21616 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
291 ms |
7704 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
320 ms |
21500 KB |
Output is correct |
33 |
Correct |
249 ms |
15564 KB |
Output is correct |
34 |
Correct |
182 ms |
20172 KB |
Output is correct |
35 |
Correct |
2 ms |
468 KB |
Output is correct |
36 |
Correct |
284 ms |
19256 KB |
Output is correct |
37 |
Correct |
351 ms |
24004 KB |
Output is correct |
38 |
Correct |
2 ms |
340 KB |
Output is correct |
39 |
Correct |
138 ms |
5912 KB |
Output is correct |
40 |
Correct |
4 ms |
596 KB |
Output is correct |
41 |
Correct |
294 ms |
7640 KB |
Output is correct |
42 |
Correct |
338 ms |
21992 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
6 ms |
360 KB |
Output is correct |
45 |
Correct |
4 ms |
724 KB |
Output is correct |
46 |
Correct |
139 ms |
16264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
37 ms |
5672 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
296 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
111 ms |
5720 KB |
Output is correct |
14 |
Correct |
6 ms |
436 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
4 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
3 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
3 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
300 KB |
Output is correct |
25 |
Correct |
3 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
296 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
256 KB |
Output is correct |
30 |
Correct |
140 ms |
3768 KB |
Output is correct |
31 |
Correct |
109 ms |
3312 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
300 KB |
Output is correct |
35 |
Correct |
1 ms |
304 KB |
Output is correct |
36 |
Correct |
106 ms |
5624 KB |
Output is correct |
37 |
Execution timed out |
1119 ms |
1048272 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |