Difference between revisions of "Eulerian Path"
Jump to navigation
Jump to search
(Created page with "<syntaxhighlight lang='cpp'> struct Edge; typedef list<Edge>::iterator iter; struct Edge { int next_vertex; iter reverse_edge; Edge(int next_vertex) : next_verte...") |
|||
Line 1: | Line 1: | ||
<syntaxhighlight lang='cpp'> | <syntaxhighlight lang='cpp'> | ||
− | + | int S[N << 1], top; | |
− | + | Edge edges[N << 1]; | |
+ | set<int> G[N]; | ||
− | + | void DFS(int u) { | |
− | + | S[top++] = u; | |
− | + | for (int eid: G[u]) { | |
− | + | int v = edges[eid].get_other(u); | |
− | + | G[u].erase(eid); | |
− | + | G[v].erase(eid); | |
− | + | DFS(v); | |
− | + | return; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | int | ||
− | |||
− | |||
− | |||
} | } | ||
− | |||
} | } | ||
− | void | + | void fleury(int start) { |
− | + | int u = start; | |
− | + | top = 0; path.clear(); | |
− | + | S[top++] = u; | |
− | + | while (top) { | |
− | + | u = S[--top]; | |
− | + | if (!G[u].empty()) | |
+ | DFS(u); | ||
+ | else path.push_back(u); | ||
+ | } | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 15:21, 25 July 2018
int S[N << 1], top;
Edge edges[N << 1];
set<int> G[N];
void DFS(int u) {
S[top++] = u;
for (int eid: G[u]) {
int v = edges[eid].get_other(u);
G[u].erase(eid);
G[v].erase(eid);
DFS(v);
return;
}
}
void fleury(int start) {
int u = start;
top = 0; path.clear();
S[top++] = u;
while (top) {
u = S[--top];
if (!G[u].empty())
DFS(u);
else path.push_back(u);
}
}