Pagini recente » Cod sursa (job #1637486) | Cod sursa (job #1358021) | Cod sursa (job #812558) | Cod sursa (job #369932) | Cod sursa (job #1214599)
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <deque>
using namespace std;
const char infile[] = "biconex.in";
const char outfile[] = "biconex.out";
ifstream fin(infile);
ofstream fout(outfile);
const int MAXN = 100005;
const int oo = 0x3f3f3f3f;
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; }
/// BICONE XXX
class Graph {
public:
Graph() {
}
void build(int n) {
g.resize(n);
level.resize(n);
lowlink.resize(n);
this->n = n;
}
Graph(int n) {
g.resize(n);
level.resize(n);
lowlink.resize(n);
this->n = n;
}
void addEdge(int x, int y) {
g[x].push_back(y);
g[y].push_back(x);
}
set<int> extractBCC(int x, int y) {
int Tx, Ty;
set <int> ret;
do {
Tx = st.top().first;
Ty = st.top().second;
st.pop();
ret.insert(Tx);
ret.insert(Ty);
} while(Tx != x || Ty != y);
return ret;
}
void dfs(int node, int actlevel, int father) {
level[node] = lowlink[node] = actlevel;
for(It it = g[node].begin(), fin = g[node].end(); it != fin ; ++ it) {
if(*it == father)
continue;
if(!level[*it]) {
st.push(make_pair(node, *it));
dfs(*it, actlevel + 1, node);
lowlink[node] = min(lowlink[node], lowlink[*it]);
if(level[node] <= lowlink[*it])
bcc.push_back(extractBCC(node, *it));
} else
lowlink[node] = min(lowlink[node], level[*it]);
}
}
void buildBCC() {
dfs(0, 1, -1);
}
vector <set <int> > getBCC() {
buildBCC();
return bcc;
}
private:
vector <vector <int> > g;
vector <int> level;
vector <int> lowlink;
vector <set <int> > bcc;
stack <pair<int, int> > st;
int n;
} G;
int main() {
cin.sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen(infile, "r", stdin);
freopen(outfile, "w", stdout);
#endif
int n, m;
fin >> n >> m;
G = Graph(n);
while(m --) {
int x, y;
fin >> x >> y;
-- x; -- y;
G.addEdge(x, y);
}
vector <set <int> > bcc = G.getBCC();
fout << bcc.size() << '\n';
for(auto comp: bcc) {
for(auto it : comp)
fout << it + 1 << ' ';
fout << '\n';
}
fin.close();
fout.close();
return 0;
}