-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
224df13
commit 58fc65d
Showing
11 changed files
with
197 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* Eulerian Circuit (directed) */ | ||
// Hierholzer’s Algorithm | ||
|
||
/* | ||
CPH: | ||
https://cses.fi/book/book.pdf#page=183 | ||
Hello world: | ||
https://cses.fi/problemset/task/1693 | ||
*/ | ||
|
||
vector<int> indeg(n), outdeg(n); | ||
vector<vector<int>> g(n); | ||
for(i = 0; i < m; i++) { | ||
int u, v; cin >> u >> v; | ||
outdeg[--u]++; indeg[--v]++; | ||
g[u].push_back(v); | ||
} | ||
|
||
bool bad = false; | ||
for(i = 1; i < n-1; i++) | ||
bad |= outdeg[i] ^ indeg[i]; | ||
|
||
bad |= outdeg[0]-indeg[0] ^ 1; | ||
bad |= indeg[n-1]-outdeg[n-1] ^ 1; | ||
|
||
if(bad) | ||
// IMPOSSIBLE | ||
|
||
vector<int> tour; | ||
tour.reserve(m+1); | ||
Y([&](auto dfs, int v) -> void { | ||
while(outdeg[v]-- > 0) | ||
dfs(g[v][outdeg[v]]); | ||
tour.push_back(v); | ||
})(0); | ||
|
||
for(i = 0; i < n; i++) | ||
if(outdeg[i] > 0) | ||
// IMPOSSIBLE | ||
|
||
reverse(tour.begin(), tour.end()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* Eulerian Circuit (undirected) */ | ||
// Hierholzer’s Algorithm | ||
|
||
/* | ||
CPH: | ||
https://cses.fi/book/book.pdf#page=183 | ||
Hello world: | ||
https://cses.fi/problemset/task/1691 | ||
*/ | ||
|
||
struct edge_t { | ||
int u{-1}, v{-1}; bool done{false}; | ||
edge_t() = default; | ||
edge_t(int u, int v) : u(u), v(v) {} | ||
}; | ||
|
||
vector<int> deg(n); | ||
vector<edge_t> edges; | ||
edges.reserve(m); | ||
vector<vector<edge_t*>> g(n); | ||
|
||
for(i = 0; i < m; i++) { | ||
int u, v; cin >> u >> v; | ||
deg[--u]++; deg[--v]++; | ||
edges.emplace_back(u, v); | ||
g[u].push_back(&edges.back()); | ||
g[v].push_back(&edges.back()); | ||
} | ||
|
||
bool bad = false; | ||
for(i = 0; i < n; i++) | ||
bad |= deg[i] & 1; | ||
|
||
if(bad) | ||
// IMPOSSIBLE | ||
|
||
vector<int> tour; | ||
tour.reserve(m+1); | ||
Y([&](auto dfs, int v) -> void { | ||
while(deg[v]-- > 0) { | ||
auto e = g[v][deg[v]]; | ||
if(!e->done) | ||
e->done = true, dfs(e->u ^ e->v ^ v); | ||
} tour.push_back(v); | ||
})(0); | ||
|
||
for(i = 0; i < n; i++) | ||
if(deg[i] > 0) | ||
// IMPOSSIBLE | ||
|
||
reverse(tour.begin(), tour.end()); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,24 @@ | ||
/* Floyd-Warshall's all pair shortest paths */ | ||
// (https://cp-algorithms.com/graph/all-pair-shortest-path-floyd-warshall.html) | ||
|
||
int d[N][N]; | ||
vector<int> ans; | ||
/* | ||
https://github.com/the-hyp0cr1t3/CC/blob/master/Beginner%20Topics/%5BS5%5D%20Do%20you%20understand%20the%20graphity%20of%20this%20situation/%5BEP%202%5D%20Shortest%20paths/%5BPt%203%5D%20Floyd-Warshall.md | ||
*/ | ||
|
||
for(i = 0; i < n; i++) { | ||
for(j = 0; j < n; j++) { | ||
d[i][j] = INF; | ||
p[i][j] = -1; | ||
} | ||
d[i][i] = 0; | ||
p[i][i] = 0; | ||
} | ||
auto chmin = [](auto& A, auto&& B) { return B < A? A = B, true : false; }; | ||
|
||
for(i = 0; i < m; i++) { | ||
int u, v, cost; | ||
cin >> u >> v >> cost; | ||
u--, v--; | ||
d[u][v] = d[v][u] = cost; | ||
p[u][v] = v; | ||
p[v][u] = u; | ||
} | ||
vector d(n, vector<int>(n, INF)), path(n, vector<int>(n, -1)); | ||
for(auto [u, v, w]: edges) | ||
d[u][v] = w, path[u][v] = v; | ||
// d[i][i] = 0; | ||
|
||
void warshall() { | ||
for(int k = 0; k < n; k++) { | ||
for(int i = 0; i < n; i++) { | ||
for(int j = 0; j < n; j++) { | ||
if(d[i][k]^INF and d[k][j]^INF and (d[i][k]+d[k][j] < d[i][j])) { | ||
d[i][j] = d[i][k] + d[k][j]; | ||
p[i][j] = p[i][k]; | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
void path(int u, int v) { | ||
ans.pb(u+1); | ||
if(u == v) return; | ||
path(p[u][v], v); | ||
} | ||
for(int k = 0; k < n; k++) | ||
for(int i = 0; i < n; i++) | ||
for(int j = 0; j < n; j++) | ||
if(chmin(d[i][j], d[i][k] + d[k][j])) | ||
path[i][j] = path[i][k]; | ||
|
||
vector<int> ans{ u }; | ||
while(u ^ v) { | ||
u = path[u][v]; | ||
ans.push_back(u); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.