Pagini recente » Cod sursa (job #868408) | Cod sursa (job #1380248) | Cod sursa (job #1908255) | Cod sursa (job #2413214) | Cod sursa (job #2595803)
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
#include <stack>
using namespace std;
ifstream fin;
ofstream fout;
bitset <100001> visited;
vector <vector<int>> neighbours (100001);
vector <int> level (100001);
vector <int> minimum_accessible (100001);
stack <int> biconnected_stack;
vector <vector<int>> biconnected_components;
int nodes, edges, j, k, task;
void dfs_search (int where_to_start, int parent){
visited[where_to_start] = true;
level[where_to_start] = level[parent] + 1;
minimum_accessible[where_to_start] = where_to_start;
biconnected_stack.push(where_to_start);
for (vector<int>::iterator it = neighbours[where_to_start].begin(); it != neighbours[where_to_start].end(); it++)
if (*it != parent){
if (!visited[*it]){
dfs_search(*it, where_to_start);
if (minimum_accessible[*it] < minimum_accessible[where_to_start])
minimum_accessible[where_to_start] = minimum_accessible[*it];
if (level[where_to_start] <= minimum_accessible[*it]){
vector <int> aux;
while (biconnected_stack.top() != *it)
aux.push_back(biconnected_stack.top()) , biconnected_stack.pop();
biconnected_stack.pop();
aux.push_back(*it);
aux.push_back(where_to_start);
biconnected_components.push_back(aux);
}
}
else if (level[*it] < minimum_accessible[where_to_start])
minimum_accessible[where_to_start] = level[*it];
}
}
int main (void){
fin.open("biconex.in");
fin>>nodes>>edges;
for (int i=1; i<=edges; i++){
fin>>j>>k;
neighbours[j].push_back(k);
neighbours[k].push_back(j);
}
fin.close();
minimum_accessible[1] = 1;
level[1] = 1;
visited[1] = true;
for (vector<int>::iterator it = neighbours[1].begin(); it != neighbours[1].end(); it++)
if (!visited[*it]){
biconnected_stack.push(1);
dfs_search(*it, 1);
if (biconnected_stack.size() > 1){
vector <int> aux;
while(!biconnected_stack.empty())
aux.push_back(biconnected_stack.top()), biconnected_stack.pop();
biconnected_components.push_back(aux);
}
else while (!biconnected_stack.empty()) biconnected_stack.pop();
}
fout.open("biconex.out");
fout<<biconnected_components.size()<<"\n";
for (vector <vector<int>>::iterator it1= biconnected_components.begin(); it1 != biconnected_components.end(); it1++){
for (vector<int>::iterator it2 = it1->begin(); it2 != it1->end(); it2++)
fout<<*it2<<" ";
fout<<"\n";
}
fout.close();
return 0;
}