Pagini recente » Cod sursa (job #777117) | Cod sursa (job #2626036) | Cod sursa (job #2026973) | Cod sursa (job #930023) | Cod sursa (job #2122449)
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
ofstream out("biconex.out");
ifstream in("biconex.in");
ofstream tout("test.out");
const int N_MAX = 100000;
vector<int> neighbours[1 + N_MAX];
vector<vector<int>> components;
stack<pair<int, int>> dfs;
int n, depth[1 + N_MAX], height[1 + N_MAX], dad[1 + N_MAX];
bool visited[1 + N_MAX];
void read()
{
int m;
in >> n >> m;
for(int i = 0; i < m; i++)
{
int x, y;
in >> x >> y;
neighbours[x].push_back(y);
neighbours[y].push_back(x);
}
depth[1] = 1;
height[1] = 1;
}
void cache(int x, int y)
{
vector<int> curcomp;
int tx, ty;
do
{
tx = dfs.top().first;
ty = dfs.top().second;
dfs.pop();
curcomp.push_back(tx);
curcomp.push_back(ty);
}while(tx != x | ty != y);
components.push_back(curcomp);
}
void DFS(int node)
{
for(int m : neighbours[node])
{
if(dad[node] == m)
continue;
if(depth[m])
height[node] = min(height[node], height[m]);
else
{
dfs.push(make_pair(node,m));
dad[m] = node;
depth[m] = depth[node] + 1;
height[m] = depth[m];
DFS(m);
height[node] = min(height[node], height[m]);
if(height[m] >= depth[node])
cache(node, m);
}
}
}
void print()
{
out << components.size() << "\n";
for(vector<int> m : components)
{
sort(m.begin(),m.end());
auto it = unique(m.begin(),m.end());
m.resize(distance(m.begin(),it));
for(int r : m)
out << r << " ";
out << "\n";
}
}
int main()
{
read();
DFS(1);
print();
return 0;
}