Pagini recente » Cod sursa (job #2749979) | Cod sursa (job #1520314) | Cod sursa (job #826515) | Cod sursa (job #1362158) | Cod sursa (job #1166664)
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
const int NMax = 100010, MMax = 200010;
int N, M;
int low[NMax], nivel[NMax];
int nrbic;
vector <int> G[NMax];
vector <int> ans[NMax];
stack <pair <int, int> > st;
void Read()
{
ifstream f("biconex.in");
f >> N >> M;
for (int i = 1; i <= M; ++ i)
{
int x, y; f >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
f.close();
}
inline void GetComp(const int node, const int fiu)
{
++nrbic;
pair <int, int> aux;
do
{
aux = st.top();
st.pop();
ans[nrbic].push_back(aux.second);
} while (!st.empty() && aux.first != node && aux.second != fiu);
ans[nrbic].push_back(aux.first);
}
inline void DFS(const int node, const int father)
{
low[node] = nivel[node];
for (vector <int> :: iterator it = G[node].begin(); it != G[node].end(); ++ it)
{
if (*it != father)
{
if (nivel[*it] == 0)
{
nivel[*it] = nivel[node] + 1;
st.push(make_pair(node, *it));
DFS(*it, node);
low[node] = min(low[node], low[*it]);
if (low[*it] >= nivel[node])
GetComp(node, *it);
}
else
low[node] = min(low[node], nivel[*it]);
}
}
}
void Solve()
{
for (int i = 1; i <= N; ++ i)
if (!nivel[i])
{
nivel[i] = 1;
DFS(i, 0);
}
}
void Write()
{
ofstream g("biconex.out");
g << nrbic << "\n";
for (int i = 1; i <= nrbic; ++ i, g << "\n")
for (vector <int> :: iterator it = ans[i].begin(); it != ans[i].end(); ++ it)
g << *it << " ";
g.close();
}
int main()
{
Read();
Solve();
Write();
return 0;
}