From e0e8d0ca8fdd12692ef513d7afd83a8ccf179d12 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 20 Sep 2022 19:21:30 +0800 Subject: [PATCH] P3520 [POI2011] SMI-Garbage https://www.luogu.com.cn/record/87053341 --- Luogu/P3520/P3520.cpp | 91 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 Luogu/P3520/P3520.cpp diff --git a/Luogu/P3520/P3520.cpp b/Luogu/P3520/P3520.cpp new file mode 100644 index 00000000..b8545982 --- /dev/null +++ b/Luogu/P3520/P3520.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e5 + 5, + M = 1e6 + 5; + +int n, m, d[N], root; +bool vis[N], in_stack[N], edge_used[M]; +std::stack st; +std::vector> g[N]; +std::vector> ans; + +void dfs(int u) { + vis[u] = true; + + for (auto e : g[u]) { + int v = e.first, + id = e.second; + + if (edge_used[id]) continue; + edge_used[id] = true; + + dfs(v); + } + + if (in_stack[u]) { + std::vector res{u}; + + while (st.top() != u) { + res.emplace_back(st.top()); + in_stack[st.top()] = false; + st.pop(); + } + + res.emplace_back(u); + ans.emplace_back(res); + } else { + st.push(u); + in_stack[u] = true; + } +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m; + + for (int i = 1, a, b, s, t; i <= m; i++) { + cin >> a >> b >> s >> t; + + if (s ^ t) { + g[a].emplace_back(b, i); + g[b].emplace_back(a, i); + + d[a]++, d[b]++; + } + } + + for (int i = 1; i <= n; i++) { + if (d[i] % 2 != 0) { + cout << "NIE" << endl; + + exit(0); + } + } + + for (int i = 1; i <= n; i++) { + if (!vis[i]) dfs(i); + } + + cout << ans.size() << endl; + + for (auto& item : ans) { + cout << item.size() - 1 << ' '; + + for (int& x : item) { + cout << x << ' '; + } + + cout << endl; + } + + return 0; +}