Pagini recente » Profil ionanghelina | Cod sursa (job #1181928)
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <stack>
#include <algorithm>
#include <list>
#include <bitset>
#include <ctime>
#include <climits>
#include <cassert>
#include <iomanip>
using namespace std;
const string file = "ctc";
const string inputF = file + ".in";
const string outputF = file + ".out";
const double epsilon = 1e-7;
#define LL long long
#define ULL unsigned long long
#define MOD1 666013
#define MOD2 666019
#define MOD3 1999999973
#define base 73
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<string> vs;
typedef pair<int, int> ii;
typedef pair<long long, long long> ll;
typedef vector<ii> vii;
typedef vector<ll> vll;
#define all(V) V.begin(), V.end()
#define allr(V) V.rbegin(), V.rend()
#define for_c_it(container, it) for (auto it : container)
#define present(container, element) (container.find(element) != container.end())
#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define zeroes(x) ((x ^ (x - 1)) & x)
#define maxn 200001
int N, M, depth, nrcomp;
vvi graph;
vvi comp;
vi index, lowlink, viz;
stack<int> st;
void tarjan(int from) {
index[from] = lowlink[from] = depth; ++depth;
st.push(from);
viz[from] = 0;
for (auto x: graph[from]) {
if (viz[x] == -1) {
tarjan(x);
lowlink[from] = min(lowlink[from], lowlink[x]);
}
else if (viz[x] == 0) {
lowlink[from] = min(lowlink[from], index[x]);
}
}
if (index[from] == lowlink[from]) {
comp.pb(vector<int>());
int node;
do {
comp[nrcomp].pb(node = st.top());
st.pop();
viz[node] = 1;
} while(!st.empty() && node != from);
++nrcomp;
}
}
int main() {
#ifndef INFOARENA
freopen("input.txt", "r", stdin);
#else
freopen(inputF.c_str(), "r", stdin);
freopen(outputF.c_str(), "w", stdout);
#endif
int i, x, y;
scanf("%d %d", &N, &M);
graph.resize(N);
viz.resize(N, -1);
index.resize(N);
lowlink.resize(N);
for (i = 0; i < M; ++i) {
scanf("%d %d", &x, &y);
--x; --y;
graph[x].pb(y);
}
for (i = 0; i < N; ++i) {
if (viz[i] == -1) {
tarjan(i);
}
}
printf("%d\n", nrcomp);
for (auto x: comp) {
for (auto y: x) {
printf("%d ", y + 1);
}
printf("\n");
}
return 0;
}