Cod sursa(job #2878726)

Utilizator Max_CalincuMaxim Calincu Max_Calincu Data 27 martie 2022 15:01:16
Problema Componente tare conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.78 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> lowlink(N), v(N, 0);
ll coun;
vector<ll> a[N];
ll cur;

void dfs(int x){
	lowlink[x] = cur;
	ll e = cur;
	cur++;
	s.push(x);
	v[x] = 1;
	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);
		lowlink[x] = min(lowlink[x], lowlink[y]);
	}	
	if(lowlink[x] != e) return;
	coun++;
	v[x] = 2;
	while(s.top() != x){
		v[s.top()] = 2;
		lowlink[s.top()] = lowlink[x];
		s.pop();
	}
	s.pop();
}

void solve(){
	
	ifstream cin("ctc.in");
	ofstream cout("ctc.out");
	coun = 0;
	cur = 0;
	cin >> n >> m;
	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]) continue;
		dfs(i);
	}
	cout << coun << "\n";
	vector<pi> b;
	for(int i = 0; i<n; i++){
		b.pb({lowlink[i] , i + 1});
	}
	sort(all(b));
	for(int i = 0; i<n; i++){
		cout << b[i].s;
		if(i == n - 1){
			cout << " ";
			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;
}