Pagini recente » Cod sursa (job #2896847) | Cod sursa (job #344328) | Cod sursa (job #1377596) | Cod sursa (job #258190) | Cod sursa (job #1971095)
#include <vector>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <set>
#include <stack>
using namespace std;
const int MAXN = 100005;
vector<int>graf[MAXN];
int n, m;
int t[MAXN]; // momentul cand a fost vizitat nodul
int low[MAXN]; // minimul dintre momentul tatalui si momentul nodului
bool este[MAXN]; // Este punct de articulatie?
int componente;
set<int>comp[MAXN];
int timp;
stack<pair<int, int>>st;
ifstream in("biconex.in");
ofstream out("biconex.out");
void citire(){
in>>n>>m;
int a, b;
while(m--){
in>>a>>b;
graf[a].push_back(b);
graf[b].push_back(a);
}
}
void dfs(int nod, int tata){
++timp;
low[nod] = t[nod] = timp;
int copii = 0;
for(auto fiu : graf[nod]){
if(t[fiu] == -1){
++copii;
st.push(make_pair(nod, fiu));
dfs(fiu, nod);
low[nod] = min(low[nod], low[fiu]);
if((tata == -1 && copii >= 2) || tata != -1 && low[fiu] >= t[nod]){
este[nod] = 1;
while(st.top().first != nod && st.top().second != fiu){
comp[componente].insert(st.top().first);
comp[componente].insert(st.top().second);
st.pop();
}
comp[componente].insert(nod);
comp[componente].insert(fiu);
st.pop();
++componente;
}
} else if(fiu != tata) {
low[nod] = min(low[nod], t[fiu]);
st.push(make_pair(nod, fiu));
}
}
}
int main(){
citire();
memset(t, -1, sizeof(t));
for(int nod = 1; nod <= n ; ++nod){
if(t[nod] == -1)
dfs(nod, -1);
bool isempty = 1;
while(!st.empty()){
isempty = 0;
comp[componente].insert(st.top().first);
comp[componente].insert(st.top().second);
st.pop();
}
if(!isempty){
++componente;
}
}
out<<componente<<'\n';
}