제출 #540505

#제출 시각아이디문제언어결과실행 시간메모리
540505Shelter어르신 집배원 (BOI14_postmen)C++14
55 / 100
581 ms85392 KiB

#include <bits/stdc++.h>
 
#define len(x) (int)(x).size()
#define range(i, x, y) for(int i=x; i<y; i++)
#define sws ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define vint vector<int>
#define vll vector<long long>
#define vstr vector<string>
#define ll long long
#define F first
#define S second
#define pb push_back
#define pf push_front
#define del(a, n) (a).erase(a.begin()+(n))
#define mp make_pair
#define all(a) (a.begin(), a.end())
const int N = 1e6 + 1;
const int M = 1e6 + 1;
const int MAX = 1e6 + 1;
using namespace std;

struct Edge {
    int target, id;
	
    Edge(int _target, int _id): target(_target), id(_id) {}
};

// vector<Edge> adj[N];
vll adj[N];
bool used_edge[M]; 
list<int> euler_walk(int u) {
    list<int> ans;
    ans.push_back(u);
    while (!adj[u].empty()) {
        int v = adj[u].back();
        adj[u].pop_back();
        adj[v].erase(find(adj[v].begin(), adj[v].end(), u));
        u = v;
        ans.push_back(u);
    }
    for (auto it = ++ans.begin(); it != ans.end(); ++it) {
        auto t = euler_walk(*it);
        t.pop_back();  
        ans.splice(it, t);
    }
    
    return ans;
}

vint sol;
vint res;
bool dead[MAX];
 
 
bool vs[MAX];
void decompose(list<int> ans) { 
	stack<int> cur; 
	vector<vint> re; 
	for(auto u : ans) { 
		if(!vs[u]) { 
			cur.push(u); 
			vs[u] = 1; 
		}
		else { 
			vint cyc; 
			while(vs[u]) { 
				cyc.pb(cur.top()); 
				vs[cur.top()] = 0; 
				cur.pop(); 
			}
			for(auto x : cyc){
                cout << x << " ";
            }
            cout << endl;
			cur.push(u); 
			vs[u] = 1; 
		}
	}
}
 
int main(){
    sws
    int n, m;
    cin >> n >> m;
    for(int i=0; i<m; i++){
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    list<int> ans = euler_walk(1);
    decompose(ans);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...