Cod sursa(job #2878720)

Utilizator Max_CalincuMaxim Calincu Max_Calincu Data 27 martie 2022 14:54:21
Problema Componente tare conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.74 kb
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define pi pair<ll, ll>
#define sz(x) (int)((x).size())
#define all(a) (a).begin(), (a).end()

/*const ll maxn = 1e5;
int f[maxn],nf[maxn],inv[maxn];
const int M=998244353;
void init(){
    inv[1]=1; for (int i=2;i<maxn;i++) inv[i]=M-1ll*(M/i)*inv[M%i]%M;
    f[0]=nf[0]=1; for (int i=1;i<maxn;i++) f[i]=1ll*f[i-1]*i%M,nf[i]=1ll*nf[i-1]*inv[i]%M;
}
int C(int x,int y){return 1ll*f[x]*nf[y]%M*nf[x-y]%M;} */

const ll mod = 1e9+7;
ll n, k, m, mi, ma;
const ll N = 1e5 + 5;

stack<ll> s;
vector<ll> a[N];
vector<ll> v(N, 0);
ll cur;
vector<ll> id(N, -1), low(N);
ll ans;

void dfs(ll x){
	v[x] = 1;
	id[x] = cur;
	cur++;
	low[x] = id[x];
	s.push(x);
	for(int i = 0; i<a[x].size(); i++){
		ll y = a[x][i];
		if(v[y] == 2) continue;
		if(v[y] == 0) dfs(y);
		if(v[y] == 1) low[x] = min(low[x], low[y]);
	}
	if(low[x] != id[x]) return;
	ans++;
	while(s.top() != x){
		ll y = s.top();
		s.pop();
		v[y] = 2;
		low[y] = low[x];
	}
	s.pop();	
	v[x] = 2;
}


void solve(){
	
	cin >> n >> m;
	ans = 0;
	ifstream cin("ctc.in");
	ofstream cout("ctc.out");
	cur = 1;
	for(int i = 0; i<m; i++){
		ll x, y;
		cin >> x >> y;
		x--; y--;
		a[x].pb(y);
	}
	for(int i = 0; i<n; i++) if(v[i] == 0) dfs(i);
	cout << ans << "\n";
	vector<pi> b;
	for(int i = 0; i<n; i++) b.pb({low[i], i + 1});
	sort(all(b));
	for(int i = 0; i<n; i++){
		cout << b[i].s;
		if(i == n - 1) continue;
		if(b[i + 1].f != b[i].f) cout << "\n"; else cout << " ";
	}
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);

	//init();
	
	int t=1;
	//cin >> t;
	while(t--) solve();
	
	return 0;
}