// #pragma GCC optimize(2)
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <random>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#define IO \
ios::sync_with_stdio(false); \
// cout.tie(0);
using namespace std;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 3e6 + 10;
const int maxm = 1e6 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);
// int dis[4][2] = {1, 0, 0, -1, 0, 1, -1, 0};
// int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
struct Edge
{
int next, to;
} e[maxn << 1];
int head[maxn], dfn[maxn], low[maxn], sta[maxn];
int k, top, tim;
int dcc = 0;
int n, m;
vector<int> ans[maxn];
stack<int> s;
void add(int u, int v)
{
e[k].next = head[u];
e[k].to = v;
head[u] = k++;
}
void Tarjan(int u, int fa)
{
dfn[u] = low[u] = ++tim;
s.push(u);
if (head[u] == -1)
{
ans[++dcc].push_back(u);
return;
}
for (int i = head[u]; i != -1; i = e[i].next)
{
int v = e[i].to;
if (v == fa)
continue;
if (!dfn[v])
{
Tarjan(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u])
{
dcc++;
int pre;
do
{
pre = s.top();
s.pop();
ans[dcc].push_back(pre);
} while (pre != v);
ans[dcc].push_back(u);
}
}
else
low[u] = min(low[u], dfn[v]);
}
return;
}
int main()
{
#ifdef WXY
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
IO;
cin >> n >> m;
int x, y;
memset(head, -1, sizeof head);
for (int i = 0; i < m; i++)
{
cin >> x >> y;
add(x, y);
add(y, x);
}
for (int i = 1; i <= n; i++)
if (!dfn[i])
Tarjan(i, 0);
for (int i = 1; i <= dcc; i++)
{
for (int j = 0; j < ans[i].size(); j++)
cout << ans[i][j] << " ";
cout << endl;
}
return 0;
}
05-05
167
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交