제출 #825062

#제출 시각아이디문제언어결과실행 시간메모리
825062Liudas수천개의 섬 (IOI22_islands)C++17
39.40 / 100
1098 ms1050972 KiB
#include "islands.h" #include <cassert> #include <cstdio> #include <variant> #include <vector> #include <map> #include <set> #include <queue> #include <unordered_set> #include <algorithm> #include <iostream> using namespace std; bool loop = false; vector<int> path, lpath, childs; void dfs(int node, vector<queue<pair<int, int>>> &tree, vector<bool> &vis){ if(vis[node] && !loop){ path.push_back(node); loop = true; lpath = path; return; } if(loop || vis[node])return; vis[node] = true; path.push_back(node); while(tree[node].size()){ auto &[l, r] = tree[node].front(); tree[node].pop(); 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) { childs.resize(N); 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; vector<int> counts(N); for(int i = 0; i < M; i ++){ counts[U[i]]++; counts[V[i]]++; } for(int i = 0; i < N; i ++){ N2 &= (counts[i] == 2 * N - 2); } if(N2 && N <= 1000){ 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; } bool good = true; for(int i = 0; i < M-1; i ++){ good = V[i] != V[i + 1]; } if(good && N <= 1000){ vector<vector<pair<int,int>>> tree(N); 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; } vector<queue<pair<int,int>>> tree(N); for(int i = 0; i < M; i ++){ tree[U[i]].push({V[i], 0}); } for(int i = 0; i < N; i ++){ childs[i] = tree[i].size(); } vector<bool> vis(N, 0); dfs(0, tree, vis); if(!loop){ return false; } vector<int> arr; //cout << lap1id.size() << " " << lap2id.size() << " " << preid.size() << endl; return arr; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...