Pagini recente » Cod sursa (job #2594246) | Cod sursa (job #2716767) | Cod sursa (job #2579113) | Cod sursa (job #831966) | Cod sursa (job #1059379)
#include <fstream>
#include <vector>
#include <bitset>
#include <stack>
#include <set>
using namespace std;
const char infile[] = "biconex.in";
const char outfile[] = "biconex.out";
ifstream cin(infile);
ofstream cout(outfile);
const int MAXN = 100005;
const int oo = 0x3f3f3f3f;
typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;
const inline int min(const int &a, const int &b) { if( a > b ) return b; return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b; return a; }
const inline void Get_min(int &a, const int b) { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b) { if( a < b ) a = b; }
int DFlevel[MAXN], lowLink[MAXN], N, M, S, D;
stack < pair<int, int> > myStack;
vector < set<int> > biconnectedComponents;
Graph G;
inline void extractComponents(const int &Node, const int &Ancestor) {
set <int> actCon;
int x, y;
do {
x = myStack.top().first;
y = myStack.top().second;
myStack.pop();
actCon.insert(x);
actCon.insert(y);
} while(x != Node || y != Ancestor);
biconnectedComponents.push_back(actCon);
}
void DFs(const int &Node, const int &Father, const int &actLevel) {
DFlevel[Node] = lowLink[Node] = actLevel;
//cout << Da << '\n';
for(It it = G[Node].begin(), fin = G[Node].end(); it != fin ; ++ it) {
if(*it == Father)
continue;
if(!DFlevel[*it]) {
myStack.push(make_pair(Node, *it));
DFs(*it, Node, actLevel + 1);
lowLink[Node] = min(lowLink[Node], lowLink[*it]);
if(lowLink[*it] >= DFlevel[Node]) {
extractComponents(Node, *it);
}
}
else lowLink[Node] = min(lowLink[Node], DFlevel[*it]);
}
}
int main() {
//cin >> S >> D;
cin >> N >> M;
for(int i = 1 ; i <= M ; ++ i) {
int x, y;
cin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
for(int i = 1 ; i <= N ; ++ i)
DFs(i, 0, 1);
cout << biconnectedComponents.size() << '\n';
for(vector <set <int> > :: iterator it = biconnectedComponents.begin(), fin = biconnectedComponents.end();
it != fin ; ++ it, cout << '\n')
for(set<int> :: iterator i = it->begin(), fi = it->end(); i != fi ; ++ i)
cout << *i << ' ';
cin.close();
cout.close();
return 0;
}