Pagini recente » Cod sursa (job #1159123) | Monitorul de evaluare | Cod sursa (job #1928661) | Cod sursa (job #2155866) | Cod sursa (job #903542)
Cod sursa(job #903542)
#include <cstdio>
#include <cstdlib>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
#define Nmax 100010
#define PI pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
int N, M, X, Y, Level[Nmax], Low[Nmax];
vector<int> G[Nmax], NowB;
vector<vector<int> > Biconnected;
stack<PI> S;
void GetComp(int X, int Y)
{
NowB.clear();
PI Now;
do
{
Now = S.top(); S.pop();
NowB.pb(Now.s);
}while(Now.f != X && Now.s != Y);
NowB.pb(Now.f);
Biconnected.pb(NowB);
}
void DFS(int Node, int Father = 0)
{
Low[Node] = Level[Node];
vector<int> :: iterator it;
for(it = G[Node].begin(); it != G[Node].end(); ++it)
{
if(*it == Father) continue;
if(Level[*it] == 0)
{
S.push(mp(Node, *it));
Level[*it] = Level[Node] + 1;
DFS(*it, Node);
Low[Node] = min(Low[Node], Low[*it]);
if(Low[*it] >= Level[Node]) GetComp(Node, *it);
}else Low[Node] = min(Low[Node], Low[*it]);
}
}
int main()
{
freopen("biconex.in", "r", stdin);
freopen("biconex.out", "w", stdout);
int i, j;
scanf("%i %i", &N, &M);
for(i = 1; i <= M; i++)
{
scanf("%i %i", &X, &Y);
G[X].pb(Y);
G[Y].pb(X);
}
for(i = 1; i <= N; i++)
if(Level[i] == 0)
{
Level[i] = 1;
DFS(i);
}
printf("%i\n", Biconnected.size());
for(i = 0; i < Biconnected.size(); printf("\n"), i ++)
for(j = 0; j < Biconnected[i].size(); j ++)
printf("%i ", Biconnected[i][j]);
return 0;
}