Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define NguyenDangQuan the_author
- #define my_heart love_you_to_the_moon_and_back
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- using ld = long double;
- using ull = unsigned long long;
- const int N = 5e4 + 5;
- vector<int> adj[N], s;
- int n, m, l(0), lt(0);
- bool vis[N], on[N];
- int low[N], id[N];
- void Read(){
- cin >> n >> m;
- while(m--){
- int u, v;
- cin >> u >> v;
- adj[u].push_back(v);
- }
- }
- void dfs(int v){
- vis[v] = 1;
- id[v] = low[v] = ++l;
- s.push_back(v);
- on[v] = 1;
- for(auto i : adj[v])
- if(!vis[i]){
- dfs(i);
- low[v] = min(low[v], low[i]);
- }
- else if(on[i])
- low[v] = min(low[v], id[i]);
- if(low[v] == id[v]){
- ++lt;
- int u;
- do{
- u = s.back();
- s.pop_back();
- on[u] = 0;
- cout << u << " ";
- }while(u != v);
- cout << "\n";
- }
- }
- void Solve(){
- for(int i = 1; i <= n; ++i)
- if(!vis[i])
- dfs(i);
- cout << lt;
- }
- main(){
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- Read();
- Solve();
- }
Add Comment
Please, Sign In to add comment