forked from Ashishgup1/Competitive-Coding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bridge Tree in Graph.cpp
67 lines (63 loc) · 1.33 KB
/
Bridge Tree in Graph.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
int tim=0, grp=1;
int u[N], v[N], comp[N];
bool vis[N], vis2[N], isBridge[M];
int tin[N], tout[N], minAncestor[N];
queue<int> Q[N];
vector<pair<int, int> > g[N];
vector<int> tree[N], vertices[N]; //Tree stores Bridge Tree, vertices stores the nodes in each component
void dfs(int k, int par)
{
vis[k]=1;
tin[k]=++tim;
minAncestor[k]=tin[k];
for(auto it:g[k])
{
if(it.first==par)
continue;
if(vis[it.first])
{
minAncestor[k]=min(minAncestor[k], tin[it.first]);
continue;
}
dfs(it.first, k);
minAncestor[k]=min(minAncestor[k], minAncestor[it.first]);
if(minAncestor[it.first]>tin[k])
isBridge[it.second]=1;
}
tout[k]=tim;
}
void dfs2(int k)
{
int comp=grp;
Q[comp].push(k);
vis2[k]=1;
while(!Q[comp].empty())
{
int u=Q[comp].front();
Q[comp].pop();
vertices[comp].push_back(u);
for(auto it:g[u])
{
int v=it.first;
int edgeidx=it.second;
if(vis2[v])
continue;
if(isBridge[edgeidx])
{
grp++;
tree[comp].push_back(grp);
tree[grp].push_back(comp);
dfs2(v);
}
else
{
Q[comp].push(v);
vis2[v]=1;
}
}
}
}
//Problem 1: http://codeforces.com/contest/1000/problem/E
//Solution 1: http://codeforces.com/contest/1000/submission/40743431
//Problem 2: http://codeforces.com/contest/178/problem/B3
//Solution 2: http://codeforces.com/contest/178/submission/40744889