Skip to content

Commit ee93640

Browse files
committed
making bridge tree added
1 parent 789f333 commit ee93640

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

Graph/Making Bridge Tree.cpp

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#include<bits/stdc++.h>
2+
#include<ext/pb_ds/assoc_container.hpp>
3+
#include<ext/pb_ds/tree_policy.hpp>
4+
#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
5+
#define Fileio freopen("output.txt","w",stdout);freopen("input.txt","r",stdin);
6+
#define all(v) v.begin(),v.end()
7+
#define rall(v) v.rbegin(),v.rend()
8+
#define MEM(a,x) memset(a,x,sizeof(a))
9+
#define SZ(v) v.size()
10+
#define nl "\n"
11+
#define bug cout<<"bug"<<nl;
12+
#define pi acos(-1.0)
13+
#define ll long long
14+
#define pb push_back
15+
#define mp make_pair
16+
#define pii pair< int,int >
17+
#define pll pair< ll,ll >
18+
#define vii vector< int >
19+
#define vll vector< ll >
20+
#define vpi vector< pii >
21+
#define vpl vector<pll>
22+
#define MX 100005
23+
#define EPS 1e-12
24+
#define ss second
25+
#define ff first
26+
using namespace std;
27+
using namespace __gnu_pbds;
28+
29+
template<typename T>
30+
using ordered_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
31+
template<typename T>
32+
using ordered_multiset=tree<T, null_type, less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;
33+
34+
inline ll powr(int a,int b){ll res=1;while(b){if(b&1) res*=a;a*=a;b/=2;}return res;}
35+
int cases=1;
36+
37+
#ifdef ppqq
38+
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
39+
template < typename Arg1 >
40+
void __f(const char* name, Arg1&& arg1){
41+
cerr << name << " is " << arg1 << std::endl;
42+
}
43+
template < typename Arg1, typename... Args>
44+
void __f(const char* names, Arg1&& arg1, Args&&... args){
45+
const char* comma = strchr(names+1, ',');
46+
cerr.write(names, comma - names) << " is " << arg1 <<" ";
47+
__f(comma+1, args...);
48+
}
49+
#else
50+
#define debug(...)
51+
#endif
52+
53+
///******************************************START******************************************
54+
55+
///Source : https://codeforces.com/blog/entry/83980
56+
57+
vector<pair<int,int>> adj[100005];
58+
vector<int>bridge_tree[100005],in_time,low;
59+
vector<pair<int,int>>edges;
60+
vector<bool> vis;
61+
bool isBridge[100005];
62+
int timer,n,m,grp[100005],id;
63+
void dfs(int u,int p)
64+
{
65+
in_time[u]=low[u]=timer++;
66+
vis[u]=true;
67+
68+
for(auto it: adj[u])
69+
{
70+
if(it.ff==p) continue;
71+
if(vis[it.ff])
72+
low[u]=min(in_time[it.ff],low[u]);
73+
else
74+
{
75+
dfs(it.ff,u);
76+
low[u]=min(low[u],low[it.ff]);
77+
if(low[it.ff]>in_time[u])
78+
isBridge[it.ss]=true;
79+
}
80+
}
81+
}
82+
void findBridges()
83+
{
84+
in_time.assign(n+2,0);
85+
low.assign(n+2,0);
86+
vis.assign(n+2,false);
87+
timer=1;
88+
for(int i=1;i<=n;i++)
89+
if(!vis[i]) dfs(i,-1);
90+
}
91+
void dfs2(int u,int p)
92+
{
93+
vis[u]=true;
94+
grp[u]=id;
95+
for(auto it: adj[u])
96+
{
97+
if(vis[it.ff] || isBridge[it.ss]) continue; ///avoiding if its a bridge
98+
dfs2(it.ff,u);
99+
}
100+
}
101+
void givingGroupNumber()
102+
{
103+
vis.clear();vis.assign(n+2,false);
104+
for(int i=1;i<=n;i++)
105+
{
106+
if(!vis[i])
107+
{
108+
id++;
109+
dfs2(i,-1);
110+
}
111+
}
112+
}
113+
void makeBridgeTree()
114+
{
115+
findBridges();
116+
givingGroupNumber();
117+
118+
for(int i=1;i<=m;i++)
119+
{
120+
if(isBridge[i])
121+
{
122+
bridge_tree[grp[edges[i-1].ff]].pb(grp[edges[i-1].ss]);
123+
bridge_tree[grp[edges[i-1].ss]].pb(grp[edges[i-1].ff]);
124+
}
125+
}
126+
}
127+
int main()
128+
{
129+
FIO;
130+
cin>>n>>m;
131+
for(int i=1,u,v;i<=m;i++)
132+
{
133+
cin>>u>>v;
134+
adj[u].pb({v,i});
135+
adj[v].pb({u,i});
136+
edges.pb({u,v});
137+
}
138+
139+
makeBridgeTree();
140+
141+
}

0 commit comments

Comments
 (0)