# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
980219 | nextgenxing | Thousands Islands (IOI22_islands) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <variant>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define F0R(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define F0Rd(i, n) for(int i = (n)-1; i >= 0; i--)
#define FORd(i, a, b) for(int i = (b)-1; i >= (a); i--)
#define ff first
#define ss second
const int MAX_N = 2e5+5;
const ll MOD = 1e9+7;
vector<pii> adj[MAX_N];
bool dfs(int curr, int par, vector<int> &path){
if(!adj[curr].size()) return false;
if(curr){
if(adj[curr].size() == 1) return false;
if(adj[curr].size() > 2){
if(adj[curr][0].ff == par){
path.push_back(adj[curr][1].ss), path.push_back(adj[curr][1].ss^1);
path.push_back(adj[curr][2].ss), path.push_back(adj[curr][2].ss^1);
path.push_back(adj[curr][1].ss^1), path.push_back(adj[curr][1].ss);
path.push_back(adj[curr][2].ss^1), path.push_back(adj[curr][2].ss);
} else if(adj[curr][1].ff == par){
path.push_back(adj[curr][0].ss), path.push_back(adj[curr][0].ss^1);
path.push_back(adj[curr][2].ss), path.push_back(adj[curr][2].ss^1);
path.push_back(adj[curr][0].ss^1), path.push_back(adj[curr][0].ss);
path.push_back(adj[curr][2].ss^1), path.push_back(adj[curr][2].ss);
} else{
path.push_back(adj[curr][1].ss), path.push_back(adj[curr][1].ss^1);
path.push_back(adj[curr][0].ss), path.push_back(adj[curr][0].ss^1);
path.push_back(adj[curr][1].ss^1), path.push_back(adj[curr][1].ss);
path.push_back(adj[curr][0].ss^1), path.push_back(adj[curr][0].ss);
}
return true;
}
bool ans;
if(adj[curr][0].ff == par){
path.push_back(adj[curr][1].ss);
ans = dfs(adj[curr][1].ff, curr, path);
path.push_back(adj[curr][1].ss);
} else{
path.push_back(adj[curr][0].ss);
ans = dfs(adj[curr][0].ff, curr, path);
path.push_back(adj[curr][0].ss);
}
return ans;
} else{
if(adj[curr].size() == 1){
path.push_back(adj[curr][0].ss);
bool ans = dfs(adj[curr][0].ff, curr, path);
path.push_back(adj[curr][0].ss);
return ans;
}
if(adj[curr].size() > 1){
path.push_back(adj[curr][1].ss), path.push_back(adj[curr][1].ss^1);
path.push_back(adj[curr][0].ss), path.push_back(adj[curr][0].ss^1);
path.push_back(adj[curr][1].ss^1), path.push_back(adj[curr][1].ss);
path.push_back(adj[curr][0].ss^1), path.push_back(adj[curr][0].ss);
return true;
}
}
}
variant<bool, vector<int>> find_journey(int n, int m, vector<int> u, vector<int> v){
if(n == 2){
vector<int> num1, num2;
F0R(i, m){
if(u[i]) num2.push_back(i);
else num1.push_back(i);
}
if(num1.size() < 2) return false;
if(num2.size() < 1) return false;
vector<int> ans;
ans.push_back(num1[0]), ans.push_back(num2[0]), ans.push_back(num1[1]);
ans.push_back(num1[0]), ans.push_back(num2[0]), ans.push_back(num1[1]);
return ans;
}
if(n <= 400 && m == n*(n-1)){
int a, b, c, d;
F0R(i, m){
if(u[i] == 0 && v[i] == 1) a = i;
if(u[i] == 1 && v[i] == 0) b = i;
if(u[i] == 0 && v[i] == 2) c = i;
if(u[i] == 2 && v[i] == 0) d = i;
}
vector<int> ans;
ans.push_back(a), ans.push_back(b), ans.push_back(c), ans.push_back(d);
ans.push_back(b), ans.push_back(a), ans.push_back(d), ans.push_back(c);
return ans;
}
if(n <= 1000){
F0R(i, m) adj[u[i]].push_back({v[i], i});
vector<int> ans;
if(!dfs(0, -1, ans)) return false;
return ans;
}
}
int main() {
int N, M;
assert(2 == scanf("%d %d", &N, &M));
std::vector<int> U(M), V(M);
for (int i = 0; i < M; ++i) {
assert(2 == scanf("%d %d", &U[i], &V[i]));
}
std::variant<bool, std::vector<int>> result = find_journey(N, M, U, V);
if (result.index() == 0) {
printf("0\n");
if (std::get<bool>(result)) {
printf("1\n");
} else {
printf("0\n");
}
} else {
printf("1\n");
std::vector<int> &canoes = std::get<std::vector<int>>(result);
printf("%d\n", static_cast<int>(canoes.size()));
for (int i = 0; i < static_cast<int>(canoes.size()); ++i) {
if (i > 0) {
printf(" ");
}
printf("%d", canoes[i]);
}
printf("\n");
}
return 0;
}