Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Bridge Tree
- // O(n)
- struct NODE {
- int v, e;
- };
- vector <NODE> G[MAXN];
- bool isBridge[MAXN],
- vis[MAXN];
- int timer,
- dis[MAXN],
- low[MAXN];
- void FIND_BRIDGE(int u, int p = -1) {
- vis[u] = 1;
- dis[u] = low[u] = timer++;
- for (NODE E : G[u]) {
- int v = E.v;
- int e = E.e;
- if (v == p)
- continue;
- if (!vis[v]) {
- FIND_BRIDGE(v, u);
- low[u] = min(low[u], low[v]);
- if (dis[u] < low[v])
- isBridge[e] = 1;
- }
- else {
- low[u] = min(low[u], dis[v]);
- }
- }
- }
- int compo;
- bool used[MAXN];
- int compo_num[MAXN];
- vector <int> TREE[MAXN];
- void BRIDGE_TREE(int src) {
- used[src] = 1;
- int cur_compo = compo;
- compo_num[src] = cur_compo;
- queue <int> PQ; // for BFS
- PQ.push(src);
- while (!PQ.empty()) {
- int u = PQ.front();
- PQ.pop();
- for (NODE E : G[u]) {
- int v = E.v;
- int e = E.e;
- if (used[v])
- continue;
- if (isBridge[e]) {
- compo++;
- TREE[cur_compo].push_back(compo);
- TREE[compo].push_back(cur_compo);
- BRIDGE_TREE(v);
- }
- else {
- PQ.push(v);
- used[v] = 1;
- compo_num[v] = cur_compo; // v's component number
- }
- }
- }
- }
- int main() {
- // Take input
- for (int i = 0; i < tNode; i++) { // 0 <= node_num < tNode
- if (!vis[i])
- FIND_BRIDGE(i);
- }
- for (int i = 0; i < tNode; i++) {
- if (!used[i]) {
- BRIDGE_TREE(i);
- compo++;
- }
- }
- // Output
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement