Pagini recente » Cod sursa (job #2611654) | Cod sursa (job #1803317) | Cod sursa (job #485671) | Cod sursa (job #59102) | Cod sursa (job #1691200)
#include<fstream>
#include<vector>
#include<stack>
using namespace std;
int n, m,nrComp;
int k,i, j,depth[100005],low[100005],parent[100005];
vector<vector<int>> graph;
vector<vector<int>> graphComp;
stack<pair<int,int>> st;
vector<bool> visited;
void makeBiconexComp(int vertex,int node)
{
int kv, kn;
do
{
kv = st.top().first;
kn = st.top().second;
graphComp[nrComp].push_back(st.top().second);
st.pop();
} while (kv != vertex && kn != node);
graphComp[nrComp].push_back(vertex);
++nrComp;
}
void dfsBiconex(int vertex,int deep)
{
depth[vertex] = low[vertex] = deep;
visited[vertex] = true;
for (auto node:graph[vertex])
{
if (!visited[node])
{
st.push({ vertex,node });
parent[node] = vertex;
dfsBiconex(node, ++deep);
if (low[node] >= depth[vertex])
makeBiconexComp(vertex,node);
low[vertex] = low[vertex] < low[node] ? low[vertex] : low[node];
}
else if (node != parent[vertex])
{
low[vertex] = low[vertex] < low[node] ? low[vertex] : low[node];
}
}
}
int main()
{
ifstream fin("biconex.in");
ofstream fout("biconex.out");
fin >> n >> m;
graph.resize(n);
graphComp.resize(n);
visited.resize(n, false);
int x, y;
for (int i = 0;i < m;++i)
{
fin >> x >> y;
--x;
--y;
graph[x].push_back(y);
graph[y].push_back(x);
}
dfsBiconex(0,1);
fout << nrComp << "\n";
for (i = 0;i < nrComp;++i)
{
for (auto j : graphComp[i])
fout << j+1 << " ";
fout << "\n";
}
fin.close();
fout.close();
return 0;
}