Pagini recente » Cod sursa (job #503711) | Cod sursa (job #1787896) | Cod sursa (job #1872980) | Cod sursa (job #2447459) | Cod sursa (job #2533774)
#include <bits/stdc++.h>
using namespace std;
ifstream f("pctart.in");
ofstream g("pctart.out");
constexpr int NMAX = 100003;
stack <pair<int, int>> nodes;
vector<int> G[NMAX];
bool sel[NMAX], C[NMAX];
int nivel[NMAX], T[NMAX], Low[NMAX], rad, nr, N, M, x, y;
stringstream buffer;
void afis(int u, int v) {
while(1)
{
int x = nodes.top().first;
int y = nodes.top().second;
nodes.pop();
buffer << x << ' ';
buffer << y << ' ';
if(x == u || y == v)
break;
}
return;
}
void df(int nod) {
sel[nod] = true; Low[nod] = nivel[nod];
for(auto fiu : G[nod])
if(!sel[fiu]) {
nodes.push({nod, fiu});
T[fiu] = nod;
nivel[fiu] = nivel[nod] + 1;
if(nod == rad) nr++;
df(fiu);
if(Low[fiu] < Low[nod]) Low[nod] = Low[fiu];
if(Low[fiu] >= nivel[nod] && nod != rad)
C[nod] = true, afis(nod, fiu);
} else if(T[nod] != fiu && Low[nod] > nivel[fiu]) {
Low[nod] = nivel[fiu];
}
}
int main()
{
f >> N >> M;
for(int i = 1; i <= M; i++)
{
f >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
for(int i = 1; i <= N; i++)
if(!sel[i])
{
nivel[i] = 1;
nr = 0;
rad = i;
df(i);
if(nr > 1) C[i] = true;
}
g << buffer.str() << "\n";
return 0;
}