Pagini recente » Cod sursa (job #859823) | Cod sursa (job #729505) | Cod sursa (job #39578) | Cod sursa (job #1832310) | Cod sursa (job #2605394)
#include <stdio.h>
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int INF = 0x3f3f3f3f;
ifstream fin ("ctc.in");
ofstream fout ("ctc.out");
const int Nmax = 100555;
int counter = 0;
int ind[Nmax], lowkey[Nmax];
stack<int> st;
char inStack[Nmax];
vi G[Nmax];
int ans = 0;
vector<vi> ctc;
void dfs(int nod) {
if (ind[nod] != INF) { return; }
ind[nod] = lowkey[nod] = ++counter;
st.push(nod); inStack[nod] = 1;
for(auto y: G[nod]) {
if (ind[y] == INF) {
dfs(y);
}
if (inStack[y]) {
lowkey[nod] = min(lowkey[nod], lowkey[y]);
}
}
if (ind[nod] == lowkey[nod]) {
ctc.resize(++ans);
vi &comp = ctc.back();
while(!st.empty()) {
int x = st.top(); st.pop();
inStack[x] = 0;
comp.push_back(x);
if (x == nod) {
break;
}
}
}
}
int main(void) {
// freopen("ctc.in", "r", stdin);
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
memset(ind, 0x3f, sizeof(ind));
memset(lowkey, 0x3f, sizeof(ind));
int N, M, a, b;
fin >> N >> M;
rep(i, M) {
fin >> a >> b;
--a, --b;
G[a].push_back(b);
}
rep(i, N) {
if (ind[i] == INF) {
dfs(i);
}
}
fout << ans << '\n';
for(const auto& comp: ctc) {
for(auto x: comp) {
fout << x+1 << " ";
}
fout << '\n';
}
return 0;
}