Skip to content

Commit

Permalink
✅ graphs, bridges
Browse files Browse the repository at this point in the history
  • Loading branch information
iagorrr committed Dec 21, 2023
1 parent b1ece79 commit ff4201b
Showing 1 changed file with 108 additions and 0 deletions.
108 changes: 108 additions & 0 deletions submissions/CSES/Necessary Roads.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.cpp"
#else
#define dbg(...) 42
#endif
#define endl '\n'
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define len(__x) (int)__x.size()
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using vll = vector<ll>;
using pll = pair<ll, ll>;
using vll2d = vector<vll>;
using vi = vector<int>;
using vi2d = vector<vi>;
using pii = pair<int, int>;
using vii = vector<pii>;
using vc = vector<char>;
#define all(a) a.begin(), a.end()
#define pb(___x) push_back(___x)
#define mp(___a, ___b) make_pair(___a, ___b)
#define eb(___x) emplace_back(___x)
int lg2(ll x) {
return __builtin_clzll(1) - __builtin_clzll(x);
}

// vector<string> dir({"LU", "U", "RU", "R", "RD", "D",
// "LD", "L"}); int dx[] = {-1, -1, -1, 0, 1, 1, 1, 0}; int
// dy[] = {-1, 0, 1, 1, 1, 0, -1, -1};
vector<string> dir({"U", "R", "D", "L"});
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

const ll oo = 1e18;
int T(1);
const int MAXN(1'000'000);

int tin[MAXN], low[MAXN], timer, N;
char vis[MAXN];
vi2d G(MAXN);
vector<pii> bridges;

void dfs(int u, int p = -1) {
vis[u] = true;
tin[u] = low[u] = timer++;

for (auto v : G[u]) {
if (v == p) continue;
if (vis[v]) {
low[u] = min(low[u], tin[v]);
} else {
dfs(v, u);
low[u] = min(low[u], low[v]);
if (low[v] > tin[u]) {
bridges.emplace_back(u, v);
}
}
}
}
void findBridges() {
timer = 0;

memset(vis, 0, sizeof(vis));
memset(tin, -1, sizeof(tin));
memset(low, -1, sizeof(low));
bridges.clear();

for (int i = 0; i < N; i++) {
if (not vis[i]) dfs(i);
}
}

int M;

auto solve() {
cin >> N >> M;
while (M--) {
int u, v;
cin >> u >> v;
u--, v--;
G[u].emplace_back(v);
G[v].emplace_back(u);
}

findBridges();
cout << len(bridges) << endl;
for (auto &[a, b] : bridges) {
cout << a + 1 << ' ' << b + 1 << endl;
}
}

int32_t main(void) {
#ifndef LOCAL
fastio;
#endif

// cin >> T;

for (int i = 1; i <= T; i++) {
solve();
}
}

0 comments on commit ff4201b

Please sign in to comment.