이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define pii pair<int, int>
variant<bool, vector<int>> n2(int n, int M, vector<int> U, vector<int> V) {
vector<int> u0, u1;
for (int i = 0; i < M; i++) {
if (U[i]) u1.push_back(i);
else u0.push_back(i);
}
if (u1.size() > 0 && u0.size() > 1) {
vector<int> ans = {u0[0], u1[0], u0[1], u0[0], u1[0], u0[1]};
return ans;
} else {
return false;
}
}
void dfs(int v, int p, vector<vector<pii>> &g, vector<bool> &uu, vector<int> &ps) {
if (uu[v]) return;
uu[v] = 1;
ps[v] = p;
for (auto [u, _] : g[v]) {
dfs(u, v, g, uu, ps);
}
}
variant<bool, vector<int>> undirected(int n, int M, vector<int> U, vector<int> V) {
vector<vector<pii>> g(n);
vector<vector<vector<int>>> gh(n, vector<vector<int>>(n));
for (int i = 0; i < M; i++) {
g[U[i]].push_back({V[i], i});
gh[U[i]][V[i]].push_back(i);
}
if (g[0].size() > 1) {
int u = g[0][0].first, v = g[0][1].first;
if (u == v) {
vector<int> ans = {g[0][0].second, g[u][0].second, g[0][1].second,
g[0][0].second, g[u][0].second, g[0][1].second};
return ans;
}
vector<int> ans = {g[0][0].second, g[u][0].second, g[0][1].second, g[v][0].second,
g[u][0].second, g[0][0].second, g[v][0].second, g[0][1].second};
return ans;
}
vector<bool> uu(n);
vector<int> p(n);
dfs(0, 0, g, uu, p);
for (int i = 0; i < n; i++) {
if (g[i].size() > 2 && uu[i]) {
vector<int> ans, path;
int v = i;
while (v != 0) {
path.push_back(v);
v = p[v];
}
path.push_back(0);
reverse(path.begin(), path.end());
for (int i = 0; i < (int) path.size() - 1; i++) {
ans.push_back(gh[path[i]][path[i + 1]][0]);
}
reverse(path.begin(), path.end());
vector<int> xy;
for (auto [u, _] : g[i]) {
if (u != path[1]) xy.push_back(u);
}
int x = xy[0], y = xy[1];
if (x == y) {
ans.push_back(gh[i][x][0]);
ans.push_back(gh[x][i][0]);
ans.push_back(gh[i][x][1]);
ans.push_back(gh[i][x][0]);
ans.push_back(gh[x][i][0]);
ans.push_back(gh[i][x][1]);
} else {
ans.push_back(gh[i][x][0]);
ans.push_back(gh[x][i][0]);
ans.push_back(gh[i][y][0]);
ans.push_back(gh[y][i][0]);
ans.push_back(gh[x][i][0]);
ans.push_back(gh[i][x][0]);
ans.push_back(gh[y][i][0]);
ans.push_back(gh[i][y][0]);
}
for (int i = 0; i < (int) path.size() - 1; i++) {
ans.push_back(gh[path[i + 1]][path[i]][0]);
}
return ans;
}
}
return false;
}
variant<bool, vector<int>> find_journey(int n, int M, vector<int> U, vector<int> V) {
if (n == 2) return n2(n, M, U, V);
return undirected(n, M, U, V);
}
#ifdef ONLINE_JUDGE
signed main() {
//variant<bool, vector<int>> ans = find_journey(5, 8, {0, 1, 1, 1, 2, 3, 4, 4},
// {4, 4, 2, 3, 1, 1, 1, 0});
variant<bool, vector<int>> ans =
find_journey(3, 6, {0, 1, 1, 1, 2, 2}, {1, 0, 2, 2, 1, 1});
for (int x : get<vector<int>>(ans)) cout << x << " ";
cout << "\n";
}
#endif
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |