이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
#define all(x) x.begin(), x.end()
#define pb push_back
#define sz(x) (int)(x.size())
#define ll long long
#define fi first
#define se second
#define lbd lower_bound
#define ubd upper_bound
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;
const double eps = 1e-10;
const long long INF = 1e18;
const int N = 2e5 + 10;
struct Euler_Path {
int n, src;
bool circuit;
vector<bool> vis;
vector<set<int>> adj;
vector<int> deg, path;
void init(int _n, int x = 0, bool ok = 1) {
n = _n;
src = x;
circuit = ok;
adj.resize(n);
vis.resize(n);
deg.resize(n);
}
void add_edge(int u, int v) {
adj[u].insert(v);
adj[v].insert(u);
deg[u]++, deg[v]++;
}
void dfs(int u) {
vis[u] = 1;
for (auto v : adj[u]) {
if (!vis[v]) dfs(v);
}
}
bool is_connected() {
dfs(src);
for (int i = 0; i < n; i++) {
if (!adj[i].empty() && !vis[i]) return false;
}
return true;
}
void euler_dfs(int u) {
while (!adj[u].empty()) {
auto v = *adj[u].begin();
adj[u].erase(adj[u].begin());
adj[v].erase(u);
euler_dfs(v);
}
path.pb(u);
}
vector<int> euler_path() {
if (!is_connected()) return path;
int cnt = 0;
for (int i = 0; i < n; i++) {
if (deg[i] & 1) {
src = i;
cnt++;
}
}
if (circuit) {
if (cnt) return path;
} else {
if ((cnt != 2) && cnt) return path;
}
euler_dfs(src);
return path;
}
} idk;
void solve() {
int n, m;
cin >> n >> m;
idk.init(n, 0, 1);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
--u, --v;
idk.add_edge(u, v);
}
vector<int> ans = idk.euler_path();
assert(!ans.empty());
vector<int> cycles;
bool vis[n] = {};
vector<vector<int>> fin;
for (int it : ans) {
if (vis[it]) {
vector<int> curr;
curr.pb(it);
while (cycles.back() != it) {
curr.pb(cycles.back());
vis[cycles.back()] = 0;
cycles.pop_back();
}
fin.pb(curr);
} else {
vis[it] = 1;
cycles.pb(it);
}
}
for (auto v : fin) {
cout << sz(v) << ' ';
for (auto it : v) cout << it + 1 << ' ';
cout << '\n';
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
solve();
cout << '\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |