제출 #997179

#제출 시각아이디문제언어결과실행 시간메모리
997179mkola어르신 집배원 (BOI14_postmen)C++14
100 / 100
279 ms69804 KiB
#include<bits/stdc++.h> using namespace std; #define pb push_back #define sz size #define all(x) begin(x), end(x) #define ll long long #define INF 1e18 #define MOD (int)(1e9 + 7) int N, M, cnt[500005]; vector<pair<int, int>> adj[500005]; vector<int> cycle; vector<vector<int>> ans; bool seen[500005]; void dfs(int v) { while(adj[v].sz()) { pair<int, int> p = adj[v].back(); adj[v].pop_back(); int u = p.first, id = p.second; if(!seen[id]) { seen[id] = true; dfs(u); } } cycle.pb(v); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> N >> M; for(int i = 0; i < M; ++i) { int a, b; cin >> a >> b; adj[a].pb({b, i}); adj[b].pb({a, i}); } dfs(1); // 1 5 8 10 9 2 3 6 7 8 4 7 5 4 3 1 stack<int> st; for(int i = 0; i < (int)cycle.sz(); ++i) { int v = cycle[i]; cnt[v]++; if(cnt[v] > 1) { vector<int> cyc; while(st.top() != v) { cyc.pb(st.top()); cnt[st.top()]--; st.pop(); } st.pop(); cnt[v]--; cyc.pb(v); ans.pb(cyc); } st.push(v); } for(auto const &p : ans) { for(int v : p) cout << v << ' '; cout << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...