Submission #136378

#TimeUsernameProblemLanguageResultExecution timeMemory
136378tdwnSenior Postmen (BOI14_postmen)C++17
55 / 100
560 ms82552 KiB
#include <bits/stdc++.h> #define ll long long #define pb push_back #define pf push_front #define mp make_pair using namespace std; const int maxn = 500100; class edge { public: int nxt_node; list<edge>::iterator reverse_edge; edge(int _node) { nxt_node = _node; } }; list<edge>adj[maxn]; int n, m; vector<int>euler_tour; bool visited[maxn]; void solve(int node) { while(adj[node].size() > 0) { edge x = adj[node].front(); int nxt = x.nxt_node; adj[nxt].erase(x.reverse_edge); adj[node].pop_front(); solve(nxt); } euler_tour.pb(node); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin>>n>>m; int a, b; for(int i=0;i<m;i++) { cin>>a>>b; adj[a].pf(edge(b)); list<edge>::iterator it_a = adj[a].begin(); adj[b].pf(edge(a)); list<edge>::iterator it_b = adj[b].begin(); it_a->reverse_edge = it_b; it_b->reverse_edge = it_a; } solve(1); vector<int>seq; for(int i:euler_tour) { //cout<<i<<" "; if(!visited[i]) { seq.pb(i); visited[i] = true; } else { // go back in seq while you find i while(seq.size() > 0 && seq.back() != i) { visited[seq.back()] = false; cout<<seq.back()<<" "; seq.pop_back(); } cout<<i<<"\n"; } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...